Skip to content

Commit

Permalink
add multitask regressors to Lazy*Classifier
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymoudiki committed Oct 9, 2024
1 parent cb13953 commit ddc5350
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 8 deletions.
21 changes: 21 additions & 0 deletions examples/multitask_regressor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mlsauce as ms
from sklearn.datasets import load_iris
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import HuberRegressor, LinearRegression

data = load_iris()
X = data.data
y = data.target
y_encoded = OneHotEncoder().fit_transform(y.reshape(-1, 1)).toarray()

obj = ms.MultiTaskRegressor(regr=HuberRegressor())

obj.fit(X, y_encoded)

print(obj.predict(X))

obj2 = LinearRegression()

obj2.fit(X, y_encoded)

print(obj2.predict(X))
36 changes: 36 additions & 0 deletions mlsauce.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Metadata-Version: 2.1
Name: mlsauce
Version: 0.22.0
Summary: Miscellaneous Statistical/Machine Learning tools
Maintainer: T. Moudiki
Maintainer-email: [email protected]
License: BSD3 Clause Clear
Platform: linux
Platform: macosx
Platform: windows
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Requires-Python: >=3.5
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: Cython
Requires-Dist: joblib
Requires-Dist: matplotlib
Requires-Dist: nnetsauce
Requires-Dist: pandas
Requires-Dist: requests
Requires-Dist: scikit-learn
Requires-Dist: scipy
Requires-Dist: tqdm
Provides-Extra: alldeps
Requires-Dist: numpy>=1.13.0; extra == "alldeps"
Requires-Dist: scipy>=0.19.0; extra == "alldeps"

Miscellaneous Statistical/Machine Learning tools
75 changes: 75 additions & 0 deletions mlsauce.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
LICENSE
README.md
setup.cfg
setup.py
mlsauce/__init__.py
mlsauce/_config.py
mlsauce/setup.py
mlsauce.egg-info/PKG-INFO
mlsauce.egg-info/SOURCES.txt
mlsauce.egg-info/dependency_links.txt
mlsauce.egg-info/not-zip-safe
mlsauce.egg-info/requires.txt
mlsauce.egg-info/top_level.txt
mlsauce/adaopt/__init__.py
mlsauce/adaopt/_adaopt.py
mlsauce/adaopt/_adaoptc.c
mlsauce/adaopt/setup.py
mlsauce/adaopt/setup2.py
mlsauce/adaopt/utils_adaopt.py
mlsauce/booster/__init__.py
mlsauce/booster/_booster_classifier.py
mlsauce/booster/_booster_regressor.py
mlsauce/booster/_boosterc.c
mlsauce/booster/setup.py
mlsauce/booster/setup2.py
mlsauce/booster/utils_booster.py
mlsauce/datasets/__init__.py
mlsauce/datasets/dowload.py
mlsauce/elasticnet/__init__.py
mlsauce/elasticnet/_enet.py
mlsauce/elasticnet/enet.py
mlsauce/elasticnet/utils_enet.py
mlsauce/lasso/__init__.py
mlsauce/lasso/_lasso.py
mlsauce/lasso/_lassoc.c
mlsauce/lasso/setup.py
mlsauce/lasso/setup2.py
mlsauce/lasso/utils_lasso.py
mlsauce/lazybooster/__init__.py
mlsauce/lazybooster/config.py
mlsauce/lazybooster/lazyboosterclassif.py
mlsauce/lazybooster/lazyboosterregression.py
mlsauce/multitaskregressor/__init__.py
mlsauce/multitaskregressor/mtaskregr.py
mlsauce/nonconformist/__init__.py
mlsauce/nonconformist/acp.py
mlsauce/nonconformist/base.py
mlsauce/nonconformist/cp.py
mlsauce/nonconformist/evaluation.py
mlsauce/nonconformist/icp.py
mlsauce/nonconformist/nc.py
mlsauce/nonconformist/util.py
mlsauce/predictioninterval/__init__.py
mlsauce/predictioninterval/predictioninterval.py
mlsauce/ridge/__init__.py
mlsauce/ridge/_ridge.py
mlsauce/ridge/_ridgec.c
mlsauce/ridge/setup.py
mlsauce/ridge/setup2.py
mlsauce/ridge/utils_ridge.py
mlsauce/stump/__init__.py
mlsauce/stump/_stump_classifier.py
mlsauce/stump/_stumpc.c
mlsauce/stump/setup.py
mlsauce/stump/setup2.py
mlsauce/stump/utils_stump.py
mlsauce/utils/__init__.py
mlsauce/utils/get_beta.py
mlsauce/utils/progress_bar.py
mlsauce/utils/memoryuse/__init__.py
mlsauce/utils/memoryuse/mem_usage.py
mlsauce/utils/misc/__init__.py
mlsauce/utils/misc/misc.py
mlsauce/utils/sampling/__init__.py
mlsauce/utils/sampling/rowsubsampling.py
1 change: 1 addition & 0 deletions mlsauce.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions mlsauce.egg-info/not-zip-safe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 14 additions & 0 deletions mlsauce.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
numpy
Cython
joblib
matplotlib
nnetsauce
pandas
requests
scikit-learn
scipy
tqdm

[alldeps]
numpy>=1.13.0
scipy>=0.19.0
1 change: 1 addition & 0 deletions mlsauce.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlsauce
2 changes: 2 additions & 0 deletions mlsauce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
GenericBoostingRegressor,
)
from .lazybooster import LazyBoostingClassifier, LazyBoostingRegressor
from .multitaskregressor import MultiTaskRegressor
from .datasets import download
from .elasticnet import ElasticNetRegressor
from .lasso import LassoRegressor
Expand All @@ -83,6 +84,7 @@
"RidgeRegressor",
"LazyBoostingClassifier",
"LazyBoostingRegressor",
"MultiTaskRegressor",
# Other imports
# "corrtarget_encoder",
"download",
Expand Down
26 changes: 21 additions & 5 deletions mlsauce/lazybooster/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import nnetsauce as ns
from functools import partial
from sklearn.base import RegressorMixin
from ..multitaskregressor.mtaskregr import MultiTaskRegressor
from sklearn.utils import all_estimators
from ..utils import is_multitask_estimator

removed_regressors = [
"AdaBoostRegressor",
Expand All @@ -16,11 +18,11 @@
"IsotonicRegression",
"MLPRegressor",
# "KernelRidge",
"MultiOutputRegressor",
"MultiTaskElasticNet",
"MultiTaskElasticNetCV",
"MultiTaskLasso",
"MultiTaskLassoCV",
#"MultiOutputRegressor",
#"MultiTaskElasticNet",
#"MultiTaskElasticNetCV",
#"MultiTaskLasso",
#"MultiTaskLassoCV",
# "NuSVR",
"OrthogonalMatchingPursuit",
"OrthogonalMatchingPursuitCV",
Expand All @@ -43,3 +45,17 @@
and (est[0] not in removed_regressors)
)
]

MTASKREGRESSORS = [
(
"GenericBooster(MultiTask(" + est[0] + "))",
partial(MultiTaskRegressor, regr=est[1]()),
)
for est in all_estimators()
if (
issubclass(est[1], RegressorMixin)
and (est[0] not in removed_regressors)
and (est[0][:5] != "Multi")
and is_multitask_estimator(est[1]()) == False
)
]
12 changes: 10 additions & 2 deletions mlsauce/lazybooster/lazyboosterclassif.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
roc_auc_score,
f1_score,
)
from .config import REGRESSORS
from .config import REGRESSORS, MTASKREGRESSORS
from ..booster import GenericBoostingClassifier
from ..multitaskregressor import MultiTaskRegressor

import warnings

Expand Down Expand Up @@ -331,7 +332,7 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
print(exception)

if self.estimators == "all":
self.classifiers = REGRESSORS
self.classifiers = REGRESSORS + MTASKREGRESSORS
else:
self.classifiers = [
("GBoostClassifier(" + est[0] + ")", est[1]())
Expand All @@ -340,6 +341,13 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
issubclass(est[1], RegressorMixin)
and (est[0] in self.estimators)
)
] + [
("GBoostClassifier(MultiTask(" + est[0] + "))", partial(MultiTaskRegressor, regr=est[1]()))
for est in all_estimators()
if (
issubclass(est[1], RegressorMixin)
and (est[0] in self.estimators)
)
]

if self.preprocess is True:
Expand Down
3 changes: 3 additions & 0 deletions mlsauce/multitaskregressor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .mtaskregr import MultiTaskRegressor

__all__ = ['MultiTaskRegressor']
71 changes: 71 additions & 0 deletions mlsauce/multitaskregressor/mtaskregr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import numpy as np
import pandas as pd
from copy import deepcopy
from sklearn.base import BaseEstimator, RegressorMixin
from ..utils import is_multitask_estimator

class MultiTaskRegressor(BaseEstimator, RegressorMixin):
"""
A class for multi-task regression
Parameters
----------
regr: object
A regressor object
Attributes
----------
objs: list
A list containing the fitted regressor objects
"""
def __init__(self, regr):
assert is_multitask_estimator(regr) == False, "The regressor is already a multi-task regressor"
self.regr = regr
self.objs = []

def fit(self, X, y):
"""
Fit the regressor
Parameters
----------
X: array-like
The input data
y: array-like
The target values
"""
n_tasks = y.shape[1]
assert n_tasks > 1, "The number of columns in y must be greater than 1"
self.n_outputs_ = n_tasks
try:
for i in range(n_tasks):
self.regr.fit(X, y.iloc[:, i].values)
self.objs.append(deepcopy(self.regr))
except Exception:
for i in range(n_tasks):
self.regr.fit(X, y[:, i])
self.objs.append(deepcopy(self.regr))
return self

def predict(self, X):
"""
Predict the target values
Parameters
----------
X: array-like
The input data
Returns
-------
y_pred: array-like
The predicted target values
"""
assert len(self.objs) > 0, "The regressor has not been fitted yet"
y_pred = np.zeros((X.shape[0], self.n_outputs_))
for i in range(self.n_outputs_):
y_pred[:, i] = self.objs[i].predict(X)
return y_pred
2 changes: 2 additions & 0 deletions mlsauce/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
is_float,
is_factor,
check_and_install,
is_multitask_estimator
)
from .progress_bar import Progbar
from .get_beta import get_beta
Expand All @@ -20,4 +21,5 @@
"Progbar",
"get_beta",
"check_and_install",
"is_multitask_estimator"
]
2 changes: 2 additions & 0 deletions mlsauce/utils/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
is_float,
is_factor,
check_and_install,
is_multitask_estimator
)


Expand All @@ -15,4 +16,5 @@
"is_float",
"is_factor",
"check_and_install",
"is_multitask_estimator"
]
2 changes: 2 additions & 0 deletions mlsauce/utils/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def install_package(package_name):
[sys.executable, "-m", "pip", "install", package_name]
)

def is_multitask_estimator(estimator):
return estimator._get_tags().get('multioutput', False)

def check_and_install(package_name):
"""Check if a package is installed; if not, install it."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
MAINTAINER_EMAIL = '[email protected]'
LICENSE = 'BSD3 Clause Clear'

__version__ = '0.21.0'
__version__ = '0.22.0'

VERSION = __version__

Expand Down

0 comments on commit ddc5350

Please sign in to comment.