def test_multi_logistic_regression_gd_acc(): lr = SoftmaxRegression(epochs=200, eta=0.005, minibatches=1, random_seed=1) lr.fit(X, y) assert (y == lr.predict(X)).all()
def test_binary_logistic_regression_gd(): t = np.array([[0.13, -0.12], [-3.07, 3.05]]) lr = SoftmaxRegression(epochs=200, eta=0.005, minibatches=1, random_seed=1) lr.fit(X_bin, y_bin) np.testing.assert_almost_equal(lr.w_, t, 2) assert (y_bin == lr.predict(X_bin)).all()
def test_binary_logistic_regression_sgd(): t = np.array([[-0.68, 0.68], [-3.2, 3.2]]) lr = SoftmaxRegression(epochs=200, eta=0.005, minibatches=len(y_bin), random_seed=1) lr.fit(X_bin, y_bin) # 0, 1 class np.testing.assert_almost_equal(lr.w_, t, 2) assert (y_bin == lr.predict(X_bin)).all()
def test_binary_logistic_regression_sgd(): t = np.array([[0.13, -0.12], [-3.06, 3.05]]) lr = SoftmaxRegression(epochs=200, eta=0.005, minibatches=len(y_bin), random_seed=1) lr.fit(X_bin, y_bin) # 0, 1 class np.testing.assert_almost_equal(lr.w_, t, 2) assert (y_bin == lr.predict(X_bin)).all()
def test_binary_l2_regularization_gd(): t = np.array([[-0.17, 0.17], [-2.26, 2.26]]) lr = SoftmaxRegression(epochs=200, eta=0.005, l2=1.0, minibatches=1, random_seed=1) lr.fit(X_bin, y_bin) np.testing.assert_almost_equal(lr.w_, t, 2) assert (y_bin == lr.predict(X_bin)).all()
def test_binary_logistic_regression_gd(): t = np.array([[-0.2, 0.2], [-3.09, 3.09]]) lr = SoftmaxRegression(epochs=200, eta=0.005, minibatches=1, random_seed=1) lr.fit(X_bin, y_bin) np.testing.assert_almost_equal(lr.w_, t, 2) assert((y_bin == lr.predict(X_bin)).all())
def test_binary_l2_regularization_gd(): lr = SoftmaxRegression(eta=0.005, epochs=200, minibatches=1, l2_lambda=1.0, random_seed=1) lr.fit(X_bin, y_bin) y_pred = lr.predict(X_bin) expect_weights = np.array([[-0.316, 0.317], [-2.265, 2.265]]) np.testing.assert_almost_equal(lr.w_, expect_weights, 3) acc = sum(y_pred == y_bin) / len(y_bin) assert acc == 1.0
def test_binary_l2_regularization_gd(): lr = SoftmaxRegression(eta=0.005, epochs=200, minibatches=1, l2_lambda=1.0, random_seed=1) lr.fit(X_bin, y_bin) y_pred = lr.predict(X_bin) expect_weights = np.array([[-0.316, 0.317], [-2.265, 2.265]]) np.testing.assert_almost_equal(lr.w_, expect_weights, 3) acc = sum(y_pred == y_bin) / len(y_bin) assert(acc == 1.0)
from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report, confusion_matrix from mlxtend.classifier import SoftmaxRegression from sklearn import datasets iris = datasets.load_iris() #X = iris.data[:, [2, 3]] X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) ############# softmax Regresion ############### # Fitting softmax regression to the tranning set foft_regressor = SoftmaxRegression() foft_regressor.fit(X_train, y_train) # Predicting the test set result y_pred = foft_regressor.predict(X_test) print("############ softmax Regression ############") print(confusion_matrix(y_test, y_pred)) print(classification_report(y_test, y_pred))