Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
jameschapman19 committed Jan 29, 2023
1 parent 71c7285 commit 426280a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 117 deletions.
2 changes: 1 addition & 1 deletion skprox/linear_model/_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def fit(self, X, y, sample_weight=None):
n_samples, n_features = X.shape
if y.ndim == 1:
self.ndim = 1
self.dims = (n_features,)
self.dims = (n_features,1)
else:
self.ndim = y.shape[1]
self.dims = (n_features, y.shape[1])
Expand Down
73 changes: 15 additions & 58 deletions skprox/test/test_logistic.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import numpy as np

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from skprox.linear_model import RegularisedLogisticRegression


def test_L0_logistic():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y, t = make_regression(
X, y = make_classification(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(

x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
reg = RegularisedLogisticRegression(proximal="L0", sigma=0.1, max_iter=10000)

def test_L0_logistic():


reg = RegularisedLogisticRegression(proximal="L0", sigma=0.01, max_iter=10000)
reg.fit(x_train, y_train)
# test L0 has some zero coefficients
assert np.sum(reg.coef_ == 0) > 0
Expand All @@ -27,28 +26,14 @@ def test_L0_logistic():
assert np.sum(reg.coef_ != 0) > 0

# test L0 has the same number of coefficients as the number of targets
assert reg.coef_.shape[0] == 5
assert reg.coef_.shape[0] == 1

# test L0 has the same number of coefficients as the number of features
assert reg.coef_.shape[1] == 200


def test_L1_regression():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
reg = RegularisedLogisticRegression(proximal="L1", sigma=0.1, max_iter=10000)
reg = RegularisedLogisticRegression(proximal="L1", sigma=0.01, max_iter=10000)
reg.fit(x_train, y_train)
# test L1 has some zero coefficients
assert np.sum(reg.coef_ == 0) > 0
Expand All @@ -57,54 +42,26 @@ def test_L1_regression():
assert np.sum(reg.coef_ != 0) > 0

# test L1 has the same number of coefficients as the number of targets
assert reg.coef_.shape[0] == 5
assert reg.coef_.shape[0] == 1

# test L1 has the same number of coefficients as the number of features
assert reg.coef_.shape[1] == 200


def test_nuclearball_regression():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
reg = RegularisedLogisticRegression(proximal="NuclearBall", radius=10, max_iter=10000)
reg.fit(x_train, y_train)

# test nuclearball has norm of coefficients about the same as the radius
assert np.isclose(np.linalg.norm(reg.coef_), 10, atol=0.1)

# test nuclear has the same number of coefficients as the number of targets
assert reg.coef_.shape[0] == 5
assert reg.coef_.shape[0] == 1

# test nuclear has the same number of coefficients as the number of features
assert reg.coef_.shape[1] == 200

def test_gridsearch():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
from sklearn.model_selection import GridSearchCV

reg = GridSearchCV(
Expand All @@ -115,7 +72,7 @@ def test_gridsearch():
reg.fit(x_train, y_train)

# test gridsearch has the same number of coefficients as the number of targets
assert reg.best_estimator_.coef_.shape[0] == 5
assert reg.best_estimator_.coef_.shape[0] == 1

# test gridsearch has the same number of coefficients as the number of features
assert reg.best_estimator_.coef_.shape[1] == 200
75 changes: 17 additions & 58 deletions skprox/test/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import numpy as np

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from skprox.linear_model import RegularisedLinearRegression

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)

def test_L0_regression():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)

def test_L0_logistic():
reg = RegularisedLinearRegression(proximal="L0", sigma=0.1, max_iter=10000)
reg.fit(x_train, y_train)
# test L0 has some zero coefficients
Expand All @@ -34,20 +34,6 @@ def test_L0_regression():


def test_L1_regression():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
reg = RegularisedLinearRegression(proximal="L1", sigma=0.1, max_iter=10000)
reg.fit(x_train, y_train)
# test L1 has some zero coefficients
Expand All @@ -64,20 +50,6 @@ def test_L1_regression():


def test_nuclearball_regression():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
reg = RegularisedLinearRegression(proximal="NuclearBall", radius=10, max_iter=10000)
reg.fit(x_train, y_train)

Expand All @@ -90,26 +62,13 @@ def test_nuclearball_regression():
# test nuclear has the same number of coefficients as the number of features
assert reg.coef_.shape[1] == 200


def test_gridsearch():
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

X, y, t = make_regression(
n_samples=100,
n_features=200,
n_informative=10,
n_targets=5,
random_state=1,
coef=True,
)
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
from sklearn.model_selection import GridSearchCV

reg = GridSearchCV(
RegularisedLinearRegression(max_iter=1000),
param_grid={"proximal": ["L0", "L1"], "sigma": [1e-1,1,10]},
param_grid={"proximal": ["L0", "L1"], "sigma": [1e-1, 1, 10]},
cv=5,
)
reg.fit(x_train, y_train)
Expand Down

0 comments on commit 426280a

Please sign in to comment.