chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
from sklearn.metrics import roc_auc_score
|
||||
|
||||
from mla.ensemble import RandomForestClassifier
|
||||
from mla.ensemble.gbm import GradientBoostingClassifier
|
||||
from mla.knn import KNNClassifier
|
||||
from mla.linear_models import LogisticRegression
|
||||
from mla.metrics import accuracy
|
||||
from mla.naive_bayes import NaiveBayesClassifier
|
||||
from mla.neuralnet import NeuralNet
|
||||
from mla.neuralnet.constraints import MaxNorm
|
||||
from mla.neuralnet.layers import Activation, Dense, Dropout
|
||||
from mla.neuralnet.optimizers import Adadelta
|
||||
from mla.neuralnet.parameters import Parameters
|
||||
from mla.neuralnet.regularizers import L2
|
||||
from mla.svm.kernerls import RBF, Linear
|
||||
from mla.svm.svm import SVM
|
||||
from mla.utils import one_hot
|
||||
|
||||
try:
|
||||
from sklearn.model_selection import train_test_split
|
||||
except ImportError:
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.datasets import make_classification
|
||||
|
||||
# Generate a random regression problem
|
||||
X, y = make_classification(
|
||||
n_samples=750,
|
||||
n_features=10,
|
||||
n_informative=8,
|
||||
random_state=1111,
|
||||
n_classes=2,
|
||||
class_sep=2.5,
|
||||
n_redundant=0,
|
||||
)
|
||||
X_train, X_test, y_train, y_test = train_test_split(
|
||||
X, y, test_size=0.12, random_state=1111
|
||||
)
|
||||
|
||||
|
||||
# All classifiers except convnet, RNN, LSTM.
|
||||
|
||||
|
||||
def test_linear_model():
|
||||
model = LogisticRegression(lr=0.01, max_iters=500, penalty="l1", C=0.01)
|
||||
model.fit(X_train, y_train)
|
||||
predictions = model.predict(X_test)
|
||||
assert roc_auc_score(y_test, predictions) >= 0.95
|
||||
|
||||
|
||||
def test_random_forest():
|
||||
model = RandomForestClassifier(n_estimators=10, max_depth=4)
|
||||
model.fit(X_train, y_train)
|
||||
predictions = model.predict(X_test)[:, 1]
|
||||
assert roc_auc_score(y_test, predictions) >= 0.95
|
||||
|
||||
|
||||
def test_svm_classification():
|
||||
y_signed_train = (y_train * 2) - 1
|
||||
y_signed_test = (y_test * 2) - 1
|
||||
|
||||
for kernel in [RBF(gamma=0.05), Linear()]:
|
||||
model = SVM(max_iter=500, kernel=kernel)
|
||||
model.fit(X_train, y_signed_train)
|
||||
predictions = model.predict(X_test)
|
||||
assert accuracy(y_signed_test, predictions) >= 0.8
|
||||
|
||||
|
||||
def test_mlp():
|
||||
y_train_onehot = one_hot(y_train)
|
||||
y_test_onehot = one_hot(y_test)
|
||||
|
||||
model = NeuralNet(
|
||||
layers=[
|
||||
Dense(256, Parameters(init="uniform", regularizers={"W": L2(0.05)})),
|
||||
Activation("relu"),
|
||||
Dropout(0.5),
|
||||
Dense(128, Parameters(init="normal", constraints={"W": MaxNorm()})),
|
||||
Activation("relu"),
|
||||
Dense(2),
|
||||
Activation("softmax"),
|
||||
],
|
||||
loss="categorical_crossentropy",
|
||||
optimizer=Adadelta(),
|
||||
metric="accuracy",
|
||||
batch_size=64,
|
||||
max_epochs=25,
|
||||
)
|
||||
model.fit(X_train, y_train_onehot)
|
||||
predictions = model.predict(X_test)
|
||||
assert roc_auc_score(y_test_onehot[:, 0], predictions[:, 0]) >= 0.95
|
||||
|
||||
|
||||
def test_gbm():
|
||||
model = GradientBoostingClassifier(
|
||||
n_estimators=25, max_depth=3, max_features=5, learning_rate=0.1
|
||||
)
|
||||
model.fit(X_train, y_train)
|
||||
predictions = model.predict(X_test)
|
||||
assert roc_auc_score(y_test, predictions) >= 0.95
|
||||
|
||||
|
||||
def test_naive_bayes():
|
||||
model = NaiveBayesClassifier()
|
||||
model.fit(X_train, y_train)
|
||||
predictions = model.predict(X_test)[:, 1]
|
||||
assert roc_auc_score(y_test, predictions) >= 0.95
|
||||
|
||||
|
||||
def test_knn():
|
||||
clf = KNNClassifier(k=5)
|
||||
|
||||
clf.fit(X_train, y_train)
|
||||
predictions = clf.predict(X_test)
|
||||
assert accuracy(y_test, predictions) >= 0.95
|
||||
@@ -0,0 +1,46 @@
|
||||
# coding=utf-8
|
||||
import pytest
|
||||
from sklearn.datasets import make_classification
|
||||
from sklearn.metrics import roc_auc_score
|
||||
|
||||
try:
|
||||
from sklearn.model_selection import train_test_split
|
||||
except ImportError:
|
||||
from sklearn.cross_validation import train_test_split
|
||||
|
||||
from mla.ensemble import RandomForestClassifier
|
||||
from mla.pca import PCA
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dataset():
|
||||
# Generate a random binary classification problem.
|
||||
return make_classification(
|
||||
n_samples=1000,
|
||||
n_features=100,
|
||||
n_informative=75,
|
||||
random_state=1111,
|
||||
n_classes=2,
|
||||
class_sep=2.5,
|
||||
)
|
||||
|
||||
|
||||
# TODO: fix
|
||||
@pytest.mark.skip()
|
||||
def test_PCA(dataset):
|
||||
X, y = dataset
|
||||
X_train, X_test, y_train, y_test = train_test_split(
|
||||
X, y, test_size=0.25, random_state=1111
|
||||
)
|
||||
p = PCA(50, solver="eigen")
|
||||
|
||||
# fit PCA with training set, not the entire dataset
|
||||
p.fit(X_train)
|
||||
X_train_reduced = p.transform(X_train)
|
||||
X_test_reduced = p.transform(X_test)
|
||||
|
||||
model = RandomForestClassifier(n_estimators=25, max_depth=5)
|
||||
model.fit(X_train_reduced, y_train)
|
||||
predictions = model.predict(X_test_reduced)[:, 1]
|
||||
score = roc_auc_score(y_test, predictions)
|
||||
assert score >= 0.75
|
||||
@@ -0,0 +1,61 @@
|
||||
try:
|
||||
from sklearn.model_selection import train_test_split
|
||||
except ImportError:
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.datasets import make_regression
|
||||
|
||||
from mla.knn import KNNRegressor
|
||||
from mla.linear_models import LinearRegression
|
||||
from mla.metrics.metrics import mean_squared_error
|
||||
from mla.neuralnet import NeuralNet
|
||||
from mla.neuralnet.layers import Activation, Dense
|
||||
from mla.neuralnet.optimizers import Adam
|
||||
from mla.neuralnet.parameters import Parameters
|
||||
|
||||
# Generate a random regression problem
|
||||
X, y = make_regression(
|
||||
n_samples=1000,
|
||||
n_features=10,
|
||||
n_informative=10,
|
||||
n_targets=1,
|
||||
noise=0.05,
|
||||
random_state=1111,
|
||||
bias=0.5,
|
||||
)
|
||||
X_train, X_test, y_train, y_test = train_test_split(
|
||||
X, y, test_size=0.25, random_state=1111
|
||||
)
|
||||
|
||||
|
||||
def test_linear():
|
||||
model = LinearRegression(lr=0.01, max_iters=2000, penalty="l2", C=0.003)
|
||||
model.fit(X_train, y_train)
|
||||
predictions = model.predict(X_test)
|
||||
assert mean_squared_error(y_test, predictions) < 0.25
|
||||
|
||||
|
||||
def test_mlp():
|
||||
model = NeuralNet(
|
||||
layers=[
|
||||
Dense(16, Parameters(init="normal")),
|
||||
Activation("linear"),
|
||||
Dense(8, Parameters(init="normal")),
|
||||
Activation("linear"),
|
||||
Dense(1),
|
||||
],
|
||||
loss="mse",
|
||||
optimizer=Adam(),
|
||||
metric="mse",
|
||||
batch_size=64,
|
||||
max_epochs=150,
|
||||
)
|
||||
model.fit(X_train, y_train)
|
||||
predictions = model.predict(X_test)
|
||||
assert mean_squared_error(y_test, predictions.flatten()) < 1.0
|
||||
|
||||
|
||||
def test_knn():
|
||||
model = KNNRegressor(k=5)
|
||||
model.fit(X_train, y_train)
|
||||
predictions = model.predict(X_test)
|
||||
assert mean_squared_error(y_test, predictions) < 10000
|
||||
Reference in New Issue
Block a user