Search code examples
pythonsklearn-pandaspmml

export model to PMML


I have labeled data, couple categorical variables and two binary target variables.

header for example;

column_1,column_2,column_3,column_4,target_1,target_1 

how do I export it to PMML ? the only example I've found is with unsupervised data

import pandas

iris_df = pandas.read_csv("Iris.csv")

from sklearn2pmml import PMMLPipeline
from sklearn2pmml.decoration import ContinuousDomain
from sklearn_pandas import DataFrameMapper
from sklearn.decomposition import PCA
from sklearn.feature_selection import SelectKBest
from sklearn.preprocessing import Imputer
from sklearn.linear_model import LogisticRegression

iris_pipeline = PMMLPipeline([
    ("mapper", DataFrameMapper([
        (["Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"], [ContinuousDomain(), Imputer()])
    ])),
    ("pca", PCA(n_components = 3)),
    ("selector", SelectKBest(k = 2)),
    ("classifier", LogisticRegression())
])
iris_pipeline.fit(iris_df, iris_df["Species"])

from sklearn2pmml import sklearn2pmml

sklearn2pmml(iris_pipeline, "LogisticRegressionIris.pmml", with_repr = True)

Solution

  • The provided example is about supervised classificication - the y argument of the Pipeline#fit(X, y) method is the label.

    Your case would look like this:

    pipeline = PMMLPipeline(
      ("mapper", DataFrameMapper([
        (feature_column, LabelBinarizer()) for feature_column in ["column_1", "column_2", "column_3", "column_4"] 
      ])),
      ("classifier", LogisticClassification())
    )
    pipeline.fit(df, df["target_1"])