Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support one-vs-rest logistic regression #36

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 26 additions & 9 deletions sklearn_pmml_model/linear_model/implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,32 @@ def __init__(self, pmml):

# Import coefficients and intercepts
model = self.root.find('RegressionModel')

if model is None:
raise Exception('PMML model does not contain RegressionModel.')

tables = [
table for table in model.findall('RegressionTable')
if table.find('NumericPredictor') is not None
]
mining_model = self.root.find('MiningModel')
tables = []

if mining_model is not None and self.n_classes_ > 2:
self.multi_class = 'ovr'
segmentation = mining_model.find('Segmentation')

if segmentation.get('multipleModelMethod') not in ['modelChain']:
raise Exception('PMML model for multi-class logistic regression should use modelChain method.')

# Parse segments
segments = segmentation.findall('Segment')
valid_segments = [segment for segment in segments if segment.find('True') is not None]
models = [segment.find('RegressionModel') for segment in valid_segments]

tables = [
models[i].find('RegressionTable') for i in range(self.n_classes_)
]
elif model is not None:
self.multi_class = 'auto'
tables = [
table for table in model.findall('RegressionTable')
if table.find('NumericPredictor') is not None
]
else:
raise Exception('PMML model does not contain RegressionModel or Segmentation.')

self.coef_ = [
_get_coefficients(self, table)
Expand All @@ -113,7 +131,6 @@ def __init__(self, pmml):

self.coef_ = np.array(self.coef_)
self.intercept_ = np.array(self.intercept_)
self.multi_class = 'auto'
self.solver = 'lbfgs'

def fit(self, x, y):
Expand Down
119 changes: 117 additions & 2 deletions tests/linear_model/test_linear_model.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from unittest import TestCase

from sklearn.datasets import load_iris

import sklearn_pmml_model
from sklearn.linear_model import LinearRegression, LogisticRegression, Ridge, RidgeClassifier, Lasso, ElasticNet
from sklearn_pmml_model.linear_model import PMMLLinearRegression, PMMLLogisticRegression, PMMLRidge, PMMLRidgeClassifier, PMMLLasso, PMMLElasticNet
import pandas as pd
import numpy as np
from os import path
from os import path, remove
from io import StringIO
from sklearn2pmml.pipeline import PMMLPipeline
from sklearn2pmml import sklearn2pmml


BASE_DIR = path.dirname(sklearn_pmml_model.__file__)
Expand Down Expand Up @@ -76,7 +81,7 @@ def test_invalid_model(self):
</PMML>
"""))

assert str(cm.exception) == 'PMML model does not contain RegressionModel.'
assert str(cm.exception) == 'PMML model does not contain RegressionModel or Segmentation.'

def test_nonlinear_model(self):
with self.assertRaises(Exception) as cm:
Expand Down Expand Up @@ -104,6 +109,29 @@ def test_nonlinear_model(self):

assert str(cm.exception) == 'PMML model is not linear.'

def test_non_modelchain_segmentation(self):
with self.assertRaises(Exception) as cm:
PMMLLogisticRegression(pmml=StringIO("""
<PMML xmlns="http://www.dmg.org/PMML-4_3" version="4.3">
<DataDictionary>
<DataField name="Class" optype="categorical" dataType="string">
<Value value="setosa"/>
<Value value="versicolor"/>
<Value value="virginica"/>
</DataField>
<DataField name="a" optype="continuous" dataType="double"/>
</DataDictionary>
<MiningSchema>
<MiningField name="Class" usageType="target"/>
</MiningSchema>
<MiningModel>
<Segmentation multipleModelMethod="notModelChain" />
</MiningModel>
</PMML>
"""))

assert str(cm.exception) == 'PMML model for multi-class logistic regression should use modelChain method.'


class TestLinearRegressionIntegration(TestCase):
def setUp(self):
Expand Down Expand Up @@ -147,6 +175,9 @@ def setUp(self):
pmml = path.join(BASE_DIR, '../models/linear-model-lmc.pmml')
self.clf = PMMLLogisticRegression(pmml)

self.ref = LogisticRegression()
self.ref.fit(Xte, yte)

def test_predict_proba(self):
Xte, _ = self.test
ref = np.array([
Expand Down Expand Up @@ -210,6 +241,90 @@ def test_score(self):
ref = 0.8076923076923077
assert np.allclose(ref, self.clf.score(Xte, yte))

def test_sklearn2pmml(self):
# Export to PMML
pipeline = PMMLPipeline([
("classifier", self.ref)
])
pipeline.fit(self.test[0], self.test[1])
sklearn2pmml(pipeline, "lmc-sklearn2pmml.pmml", with_repr = True)

try:
# Import PMML
model = PMMLLogisticRegression(pmml='lmc-sklearn2pmml.pmml')

# Verify classification
Xenc, _ = self.test
assert np.allclose(
self.ref.predict_proba(Xenc),
model.predict_proba(Xenc)
)

finally:
remove("lmc-sklearn2pmml.pmml")

def test_sklearn2pmml_multiclass_multinomial(self):
data = load_iris(as_frame=True)

X = data.data
y = data.target
y.name = "Class"

ref = LogisticRegression()
ref.fit(X, y)

# Export to PMML
pipeline = PMMLPipeline([
("classifier", ref)
])
pipeline.fit(X, y)
sklearn2pmml(pipeline, "lmc-sklearn2pmml.pmml", with_repr=True)

try:
# Import PMML
model = PMMLLogisticRegression(pmml='lmc-sklearn2pmml.pmml')

# Verify classification
assert np.allclose(
ref.predict_proba(X),
model.predict_proba(X)
)

finally:
remove("lmc-sklearn2pmml.pmml")

def test_sklearn2pmml_multiclass_ovr(self):
data = load_iris(as_frame=True)

X = data.data
y = data.target
y.name = "Class"

ref = LogisticRegression(
multi_class='ovr'
)
ref.fit(X, y)

# Export to PMML
pipeline = PMMLPipeline([
("classifier", ref)
])
pipeline.fit(X, y)
sklearn2pmml(pipeline, "lmc-sklearn2pmml.pmml", with_repr=True)

try:
# Import PMML
model = PMMLLogisticRegression(pmml='lmc-sklearn2pmml.pmml')

# Verify classification
assert np.allclose(
ref.predict_proba(X),
model.predict_proba(X)
)

finally:
remove("lmc-sklearn2pmml.pmml")

def test_fit_exception(self):
with self.assertRaises(Exception) as cm:
self.clf.fit(np.array([[]]), np.array([]))
Expand Down