Skip to content

Commit

Permalink
few utnittests (#46)
Browse files Browse the repository at this point in the history
* few utnittests
  • Loading branch information
janoPig committed Jan 16, 2024
1 parent 087314b commit c7b7cad
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
14 changes: 5 additions & 9 deletions HROCH/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,9 @@ class SymbolicClassifier(OneVsRestClassifier):
----------
kwargs : Any
Parameters passed to [NonlinearLogisticRegressor](https://janopig.github.io/HROCH/HROCH.html#NonlinearLogisticRegressor) estimator
verbose : int
Verbosity level for OneVsRestClassifier
"""
def __init__(self, verbose=0, **kwargs):
super().__init__(estimator=NonlinearLogisticRegressor(**kwargs), verbose=verbose)
def __init__(self, **kwargs):
super().__init__(estimator=NonlinearLogisticRegressor(**kwargs))

def fit(self, X: numpy.ndarray, y: numpy.ndarray):
"""
Expand All @@ -307,7 +303,7 @@ def fit(self, X: numpy.ndarray, y: numpy.ndarray):
Fitted estimator.
"""

super(OneVsRestClassifier, self).fit(X, y)
super().fit(X, y)
return self

def predict(self, X: numpy.ndarray):
Expand All @@ -324,7 +320,7 @@ def predict(self, X: numpy.ndarray):
y : numpy.ndarray of shape (n_samples,)
The predicted classes.
"""
return super(OneVsRestClassifier, self).predict(X)
return super().predict(X)

def predict_proba(self, X: numpy.ndarray):
"""
Expand All @@ -340,4 +336,4 @@ def predict_proba(self, X: numpy.ndarray):
The class probabilities of the input samples. The order of the
classes corresponds to that in the attribute :term:`classes_`.
"""
return super(OneVsRestClassifier, self).predict_proba(X)
return super().predict_proba(X)
18 changes: 8 additions & 10 deletions HROCH/fuzzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .hroch import SymbolicSolver
from sklearn.base import ClassifierMixin
from sklearn.metrics import log_loss, make_scorer
from sklearn.utils import compute_class_weight
import numpy as numpy
from typing import Iterable

Expand Down Expand Up @@ -190,6 +191,8 @@ def fit(self, X: numpy.ndarray, y: numpy.ndarray, sample_weight=None, check_inpu

self.classes_ = numpy.unique(y)
self.n_classes_ = len(self.classes_)

self.class_weight_ = compute_class_weight(self.class_weight, classes=self.classes_, y=y)

super(FuzzyRegressor, self).fit(X, y, sample_weight=sample_weight, check_input=check_input)
return self
Expand Down Expand Up @@ -249,14 +252,9 @@ class FuzzyClassifier(OneVsRestClassifier):
----------
kwargs : Any
Parameters passed to [FuzzyRegressor](https://janopig.github.io/HROCH/HROCH.html#FuzzyRegressor) estimator
verbose : int
Verbosity level for OneVsRestClassifier
"""
def __init__(self, verbose=0, **kwargs):
super().__init__(estimator=FuzzyRegressor(**kwargs), verbose=verbose)

def __init__(self, **kwargs):
super().__init__(estimator=FuzzyRegressor(**kwargs))

def fit(self, X: numpy.ndarray, y: numpy.ndarray):
"""
Expand All @@ -277,7 +275,7 @@ def fit(self, X: numpy.ndarray, y: numpy.ndarray):
Fitted estimator.
"""

super(OneVsRestClassifier, self).fit(X, y)
super().fit(X, y)
return self

def predict(self, X: numpy.ndarray):
Expand All @@ -294,7 +292,7 @@ def predict(self, X: numpy.ndarray):
y : numpy.ndarray of shape (n_samples,)
The predicted classes.
"""
return super(OneVsRestClassifier, self).predict(X)
return super().predict(X)

def predict_proba(self, X: numpy.ndarray):
"""
Expand All @@ -310,4 +308,4 @@ def predict_proba(self, X: numpy.ndarray):
The class probabilities of the input samples. The order of the
classes corresponds to that in the attribute :term:`classes_`.
"""
return super(OneVsRestClassifier, self).predict_proba(X)
return super().predict_proba(X)
43 changes: 41 additions & 2 deletions test/sklearn_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import HROCH
from HROCH import SymbolicRegressor, NonlinearLogisticRegressor
from HROCH import SymbolicRegressor, NonlinearLogisticRegressor, SymbolicClassifier, FuzzyRegressor, FuzzyClassifier
import unittest
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from sklearn.metrics import r2_score, log_loss, make_scorer, roc_auc_score


class TestSklearn(unittest.TestCase):
Expand Down Expand Up @@ -68,3 +68,42 @@ def test_weights(self):
np.testing.assert_array_almost_equal(y_swd, y, decimal=6)
self.assertGreater(np.sum(np.abs(y_cw - y)), 0.001)
np.testing.assert_array_almost_equal(y_cw, y_sw, decimal=6)

def test_classifier(self):
params = [{'num_threads': 1, 'time_limit': 0.0,'iter_limit': 1000, 'random_state': 42},
{'num_threads': 2, 'time_limit': 0.0,'iter_limit': 1000, 'random_state': 42},]
classifiers = [NonlinearLogisticRegressor, SymbolicClassifier, FuzzyRegressor, FuzzyClassifier]
for p in params:
for clf in classifiers:
model = clf(**p)
model.fit(self.X_train, self.y_train)
y = model.predict(self.X_test)
yp = model.predict_proba(self.X_test)
np.testing.assert_equal(y.shape, self.y_test.shape)
np.testing.assert_equal(yp.shape, (self.y_test.shape[0], 2))

def test_classifier_cv(self):
cv = {'n':2, 'cv_params':{}, 'select':'mean', 'opt_params':{'method': 'L-BFGS-B'}, 'opt_metric':make_scorer(log_loss, greater_is_better=False, needs_proba=True)}
params = [{'num_threads': 1, 'time_limit': 0.0,'iter_limit': 1000, 'random_state': 42, 'cv_params' : cv},
{'num_threads': 2, 'time_limit': 0.0,'iter_limit': 1000, 'random_state': 42, 'cv_params' : cv},]
classifiers = [NonlinearLogisticRegressor, SymbolicClassifier, FuzzyRegressor, FuzzyClassifier]
for p in params:
for clf in classifiers:
model = clf(**p)
model.fit(self.X_train, self.y_train)
y = model.predict(self.X_test)
yp = model.predict_proba(self.X_test)
np.testing.assert_equal(y.shape, self.y_test.shape)
np.testing.assert_equal(yp.shape, (self.y_test.shape[0], 2))
if clf in [SymbolicClassifier, FuzzyClassifier]:
self.assertEqual(len(model.estimators_), 1)
model = model.estimators_[0]
equations = model.get_models()[:2]
for eq in equations:
self.assertTrue(hasattr(eq, 'cv_score'))
eq.fit(self.X_train, self.y_train)
y = eq.predict(self.X_test)
yp = eq.predict_proba(self.X_test)
np.testing.assert_equal(y.shape, self.y_test.shape)
np.testing.assert_equal(yp.shape, (self.y_test.shape[0], 2))

0 comments on commit c7b7cad

Please sign in to comment.