Skip to content

Commit

Permalink
bump v0.15.1
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymoudiki committed Apr 26, 2024
1 parent a3a9341 commit a6d3c61
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 81 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# version 0.15.0
# version 0.15.1

- Can use pandas DataFrame in `LSBoostRegressor`, `LSBoostClassifier` and `AdaOpt`

Expand Down
2 changes: 1 addition & 1 deletion mlsauce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer.
# 'X.Y.dev0' is the canonical version of 'X.Y.dev'
#
__version__ = "0.10.0"
# __version__ = "0.10.0"


# On OSX, we can get a runtime error due to multiple OpenMP libraries loaded
Expand Down
16 changes: 7 additions & 9 deletions mlsauce/adaopt/_adaopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
from tqdm import tqdm
from ..utils import subsample
from ..utils import cluster
from ..utils import cythonize_file

try:
from . import _adaoptc as adaoptc
except ImportError:
import pyximport

pyximport.install()
cythonize_file("_adaoptc.pyx")
import _adaoptc


Expand Down Expand Up @@ -203,8 +202,8 @@ def fit(self, X, y, **kwargs):
n_classes = len(np.unique(y_))

assert n == len(y_), "must have X.shape[0] == len(y)"
try:

try:

res = adaoptc.fit_adaopt(
X=np.asarray(X_).astype(np.float64),
Expand All @@ -220,7 +219,7 @@ def fit(self, X, y, **kwargs):
gamma=self.gamma,
tolerance=self.tolerance,
)

except ValueError:

res = _adaoptc.fit_adaopt(
Expand Down Expand Up @@ -306,7 +305,7 @@ def predict_proba(self, X, **kwargs):
n_test = X.shape[0]

if self.n_jobs is None:
try:
try:
return adaoptc.predict_proba_adaopt(
X_test=np.asarray(X, order="C").astype(np.float64),
scaled_X_train=np.asarray(
Expand Down Expand Up @@ -348,7 +347,7 @@ def predict_proba(self, X, **kwargs):

scaled_X_test = X / norm(X, ord=2, axis=1)[:, None]

try:
try:

if self.type_dist == "euclidean":

Expand Down Expand Up @@ -538,7 +537,6 @@ def multiproc_func(i, *args):
probs=probs_test_i, weights=weights_test_i
)


if self.verbose == 1:
res = Parallel(n_jobs=self.n_jobs, prefer="threads")(
(multiproc_func)(m) for m in tqdm(range(n_test))
Expand Down
19 changes: 9 additions & 10 deletions mlsauce/booster/_booster_classifier.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
try:
from . import _boosterc as boosterc
except ImportError:
import pyximport
pyximport.install()
import _boosterc

import numpy as np
import pandas as pd
import platform
import warnings
from sklearn.preprocessing import PolynomialFeatures
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin

from ..utils import cluster

from ..utils import cythonize_file
try:
from . import _boosterc as boosterc
except ModuleNotFoundError:
cythonize_file("_boosterc.pyx")
import _boosterc

class LSBoostClassifier(BaseEstimator, ClassifierMixin):
"""LSBoost classifier.
Expand Down Expand Up @@ -192,7 +191,7 @@ def fit(self, X, y, **kwargs):
)
X = np.column_stack((X, clustered_X))

try:
try:
self.obj = boosterc.fit_booster_classifier(
np.asarray(X, order="C"),
np.asarray(y, order="C"),
Expand Down Expand Up @@ -291,7 +290,7 @@ def predict_proba(self, X, **kwargs):
),
)
)
try:
try:
return boosterc.predict_proba_booster_classifier(
self.obj, np.asarray(X, order="C")
)
Expand Down
19 changes: 8 additions & 11 deletions mlsauce/booster/_booster_regressor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
try:
from . import _boosterc as boosterc
except ImportError:
import pyximport

pyximport.install()
import _boosterc
import numpy as np
import pandas as pd
import platform
Expand All @@ -14,8 +7,12 @@
from sklearn.preprocessing import PolynomialFeatures
from . import _boosterc as boosterc
from ..predictioninterval import PredictionInterval
from ..utils import cluster

from ..utils import cluster, cythonize_file
try:
from . import _boosterc as boosterc
except ModuleNotFoundError:
cythonize_file("_boosterc.pyx")
import _boosterc

class LSBoostRegressor(BaseEstimator, RegressorMixin):
"""LSBoost regressor.
Expand Down Expand Up @@ -210,7 +207,7 @@ def fit(self, X, y, **kwargs):
)
X = np.column_stack((X, clustered_X))

try:
try:
self.obj = boosterc.fit_booster_regressor(
X=np.asarray(X, order="C"),
y=np.asarray(y, order="C"),
Expand Down Expand Up @@ -320,7 +317,7 @@ def predict(self, X, level=95, method=None, **kwargs):
preds = self.pi.predict(X, return_pi=True)
return preds

try:
try:
return boosterc.predict_booster_regressor(
self.obj, np.asarray(X, order="C")
)
Expand Down
2 changes: 1 addition & 1 deletion mlsauce/lasso/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
try:
from ._lasso import LassoRegressor
except ImportError:
except ModuleNotFoundError:
pass

__all__ = ["LassoRegressor"]
22 changes: 9 additions & 13 deletions mlsauce/lasso/_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
from sklearn.base import BaseEstimator
from sklearn.base import RegressorMixin
from numpy.linalg import inv

try:
from . import _lassoc as mo
except ImportError:
import pyximport

pyximport.install()
import _lassoc
from ..utils import get_beta
from ..utils import get_beta, cythonize_file

if platform.system() in ("Linux", "Darwin"):
import jax.numpy as jnp
from jax import device_put
from jax.numpy.linalg import inv as jinv

try:
from . import _lassoc as mo
except ModuleNotFoundError:
cythonize_file("_lassoc.pyx")
import _lassoc


class LassoRegressor(BaseEstimator, RegressorMixin):
"""Lasso.
Expand Down Expand Up @@ -79,7 +77,7 @@ def fit(self, X, y, **kwargs):
self: object.
"""
try:
try:

self.ym, centered_y = mo.center_response(y)
self.xm = X.mean(axis=0)
Expand Down Expand Up @@ -141,7 +139,7 @@ def fit(self, X, y, **kwargs):
)
self.beta = res[0]
return self

except ValueError:

self.ym, centered_y = _lassoc.center_response(y)
Expand Down Expand Up @@ -205,8 +203,6 @@ def fit(self, X, y, **kwargs):
self.beta = res[0]
return self



def predict(self, X, **kwargs):
"""Predict test data X.
Expand Down
2 changes: 1 addition & 1 deletion mlsauce/ridge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
try:
from ._ridge import RidgeRegressor
except ImportError:
except ModuleNotFoundError:
pass

__all__ = ["RidgeRegressor"]
46 changes: 25 additions & 21 deletions mlsauce/ridge/_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
from sklearn.base import RegressorMixin
from numpy.linalg import inv

try:
from . import _ridgec as mo
except ImportError:
import pyximport

pyximport.install()
import _ridgec
from ..utils import get_beta
from ..utils import get_beta, cythonize_file

if platform.system() in ("Linux", "Darwin"):
import jax.numpy as jnp
from jax import device_put
from jax.numpy.linalg import inv as jinv

try:
from . import _ridgec as mo
except ModuleNotFoundError:
cythonize_file("_ridgec.pyx")
import _ridgec


class RidgeRegressor(BaseEstimator, RegressorMixin):
"""Ridge.
Expand Down Expand Up @@ -70,7 +69,7 @@ def fit(self, X, y, **kwargs):
self: object.
"""
try:
try:
self.ym, centered_y = mo.center_response(y)
except ValueError:
self.ym, centered_y = _ridgec.center_response(y)
Expand Down Expand Up @@ -99,21 +98,25 @@ def fit(self, X, y, **kwargs):
# self.beta, _, _, _ = np.linalg.lstsq(X_, y_, rcond=None)
self.beta = get_beta(X_, y_)
except Exception:
try:
try:
x = inv(
mo.crossprod(X_) + self.reg_lambda * np.eye(X_.shape[1])
mo.crossprod(X_)
+ self.reg_lambda * np.eye(X_.shape[1])
)
hat_matrix = mo.tcrossprod(x, X_)
self.beta = mo.safe_sparse_dot(hat_matrix, centered_y)
except ValueError:
x = inv(
_ridgec.crossprod(X_) + self.reg_lambda * np.eye(X_.shape[1])
_ridgec.crossprod(X_)
+ self.reg_lambda * np.eye(X_.shape[1])
)
hat_matrix = _ridgec.tcrossprod(x, X_)
self.beta = _ridgec.safe_sparse_dot(hat_matrix, centered_y)
self.beta = _ridgec.safe_sparse_dot(
hat_matrix, centered_y
)
return self

try:
try:
x = jinv(
mo.crossprod(X_, backend=self.backend)
+ self.reg_lambda * jnp.eye(X_.shape[1])
Expand Down Expand Up @@ -151,7 +154,7 @@ def predict(self, X, **kwargs):
"""
X_ = (X - self.xm[None, :]) / self.xsd[None, :]

try:
try:
if self.backend == "cpu":
if isinstance(self.ym, float):
return self.ym + mo.safe_sparse_dot(X_, self.beta)
Expand All @@ -170,11 +173,12 @@ def predict(self, X, **kwargs):
if isinstance(self.ym, float):
return self.ym + _ridgec.safe_sparse_dot(X_, self.beta)
return self.ym[None, :] + _ridgec.safe_sparse_dot(X_, self.beta)

# if self.backend in ("gpu", "tpu"):
if isinstance(self.ym, float):
return self.ym + _ridgec.safe_sparse_dot(X_, self.beta, backend=self.backend)
return self.ym[None, :] + _ridgec.safe_sparse_dot(X_, self.beta, backend=self.backend)



return self.ym + _ridgec.safe_sparse_dot(
X_, self.beta, backend=self.backend
)
return self.ym[None, :] + _ridgec.safe_sparse_dot(
X_, self.beta, backend=self.backend
)
2 changes: 1 addition & 1 deletion mlsauce/stump/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
try:
from ._stump_classifier import StumpClassifier
except ImportError:
except ModuleNotFoundError:
pass

__all__ = ["StumpClassifier"]
13 changes: 6 additions & 7 deletions mlsauce/stump/_stump_classifier.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from ..utils import cythonize_file

try:
from . import _stumpc as stumpc
except ImportError:
import pyximport

pyximport.install()
except ModuleNotFoundError:
cythonize_file("_stumpc.pyx")
import _stumpc


Expand Down Expand Up @@ -45,7 +44,7 @@ def fit(self, X, y, sample_weight=None, **kwargs):
"""

if sample_weight is None:
try:
try:
self.obj = stumpc.fit_stump_classifier(
X=np.asarray(X, order="C"),
y=np.asarray(y, order="C"),
Expand All @@ -60,7 +59,7 @@ def fit(self, X, y, sample_weight=None, **kwargs):

return self

try:
try:
self.obj = stumpc.fit_stump_classifier(
X=np.asarray(X, order="C"),
y=np.asarray(y, order="C"),
Expand Down Expand Up @@ -112,7 +111,7 @@ def predict_proba(self, X, **kwargs):
probability estimates for test data: {array-like}
"""
try:
try:
return stumpc.predict_proba_stump_classifier(
self.obj, np.asarray(X, order="C")
)
Expand Down
3 changes: 2 additions & 1 deletion mlsauce/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from .sampling.rowsubsampling import subsample
from .misc.misc import cluster, merge_two_dicts, flatten, is_float, is_factor
from .misc.misc import cluster, cythonize_file, merge_two_dicts, flatten, is_float, is_factor
from .progress_bar import Progbar
from .get_beta import get_beta

__all__ = [
"cluster",
"cythonize_file",
"subsample",
"merge_two_dicts",
"flatten",
Expand Down
Loading

0 comments on commit a6d3c61

Please sign in to comment.