def test_fit_single_model_with_2_class_proba(self): layer_model = Layer([LogisticRegression(solver='liblinear')], proba=True) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.array([1, 1, 0, 0]) result = layer_model.fit(X, y) assert result.shape == (4, 2)
def test_predict_single_model_with_preprocess(self): layer_model = Layer([LinearRegression()], [MinMaxScaler()]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 layer_model.fit(X, y) result = layer_model.predict(np.array([[3, 5]])) assert result.shape == (1, 1) assert np.allclose(result, np.array([[16]]))
def test_fir_single_model_with_preprocess(self): layer_model = Layer([LinearRegression()], [MinMaxScaler()]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 # X and y are linearly related, predictions will be almost perfect result = layer_model.fit(X, y) assert result.shape == (4, 1) assert np.allclose(result.flatten(), y)
def test_fit_single_model_with_multi_class_proba(self): layer_model = Layer( [LogisticRegression(solver='lbfgs', multi_class='multinomial')], proba=True) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.array([1, 1, 0, 2]) result = layer_model.fit(X, y) assert result.shape == (4, 3)
def test_predict_multiple_model_with_2_class_proba(self): layer_model = Layer([LogisticRegression(solver='liblinear'), LogisticRegression(solver='liblinear')], proba=[True,False]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.array([1, 1, 0, 0]) layer_model.fit(X, y) result = layer_model.predict(np.array([[3, 5], [2, 5]])) assert result.shape == (2,3)
def test_predict_multiple_model(self): layer_model = Layer([LinearRegression(), LinearRegression()], [None, MinMaxScaler()]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 layer_model.fit(X, y) result = layer_model.predict(np.array([[3, 5]])) assert result.shape == (1,2) assert np.allclose(result, np.array([[16, 16]]))
def test_using_proba_without_predict_proba_method(self): with pytest.warns(Warning) as record: layer_model = Layer([LinearRegression()], proba=True) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 layer_model.fit(X, y) result = layer_model.predict(np.array([[3, 5], [3, 5]])) assert result.shape == (2, 1) assert np.allclose(result, np.array([[16], [16]])) assert record
def test_fit_multiple_models(self): layer_model = Layer([LinearRegression(), LinearRegression()], [None, MinMaxScaler()]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 # X and y are linearly related, predictions will be almost perfect result = layer_model.fit(X, y) assert result.shape == (4,2) assert np.allclose(result[:,0], y) assert np.allclose(result[:,1], y)
def test_copy_function_only_model(self): curLayer = Layer([LinearRegression(), LogisticRegression()]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 curLayer.fit(X, y) curLayer2 = curLayer.copy() gotError = False try: curLayer2.predict(np.array([[3, 5]])) except (NotFittedError): gotError = True assert gotError, "Model failed the copy Test: When copying, a deep copy was produced"
def test_stack_copy_function_only_model(self): first_layer = Layer([LinearRegression(), LogisticRegression()]) second_layer = Layer([LinearRegression()]) model = Stack([first_layer, second_layer]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 model.fit(X, y) model2 = model.copy() gotError = False try: model2.predict([1, 2]) except (NotFittedError): gotError = True assert gotError, "Model failed the copy Test: When copying, a deep copy was produced"
import pytest import numpy as np from sklearn.linear_model import LinearRegression from sklearn.preprocessing import MinMaxScaler from sklearn.linear_model import LogisticRegression from sklearn.model_selection import KFold from picknmix import Layer, Stack layer_width2_reg = Layer([LinearRegression(), LinearRegression()], preprocessors = [None, MinMaxScaler()]) layer_width2_clf = Layer([LogisticRegression(solver='liblinear'), LogisticRegression(solver='liblinear')], proba=[True,False]) layer_width1_reg = Layer([LinearRegression()]) layer_width1_clf = Layer([LogisticRegression(solver='liblinear')]) class TestStack: def test_fit_predict_1_layer_reg(self): model = Stack([layer_width1_reg]) X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 model.fit(X, y) result = model.predict(np.array([[3, 5],[3, 5]])) assert result.shape == (2,) assert np.allclose(result, np.array([16, 16])) def test_fit_predict_1_layer_clf(self): model = Stack([layer_width1_clf]) X = np.array([[1, 1], [1, 1], [0, 0], [0, 0]]) y = np.array([1, 1, 0, 0])
def test_different_numbers_of_preprocessor_and_models(self): with pytest.raises(Exception): assert Layer( [LinearRegression(), LinearRegression()], [MinMaxScaler()])