Watson Studio, Watson ML, Watson OpenScale

 View Only
  • 1.  K-means PMML model to Python

    Posted Fri December 16, 2022 09:57 AM
    I first created a K means model in Modeler and then I created a model in Python with Scikit learn library. So when I compared the results of both models they were different, the question is ¿is there anyway to use de PMML exportation of the model in Python? or ¿How can I replicate the model of K means from Moderler in Python?

    Eduardo García


  • 2.  RE: K-means PMML model to Python

    User Group Leader
    Posted Wed June 21, 2023 07:31 AM

    Here's an example of how you can export the K-means model from Modeler and import it into Python using pmml-sklearn:

    1. Export the K-means model from IBM SPSS Modeler as a PMML file.
    2. Install the pmml-sklearn library using pip install pmml-sklearn.
    3. Load the PMML file and use it to create the K-means model in Python.
    from sklearn_pmml import sklearn2pmml from sklearn_pmml.convert import estimator_to_pmml from sklearn.cluster import KMeans # Load your data # ... # Create the K-means model kmeans = KMeans(n_clusters=3, init='k-means++', random_state=0) # Fit the model to your data kmeans.fit(data) # Export the K-means model to PMML pmml_file = "kmeans_model.pmml" sklearn2pmml(kmeans, pmml_file, with_repr=True) # Import the K-means model from PMML from sklearn_pmml import PMMLPipeline pipeline = PMMLPipeline.from_pmml(pmml_file) # Get the cluster labels for your data points labels = pipeline.predict(data) # Get the cluster centers centers = pipeline.named_steps['kmeans'].cluster_centers_

    Hope this helps !

    Youssef Sbai Idrissi
    Software Engineer