def test_fail_minibatches(): lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=13, random_seed=1) lr.fit(X, y) assert ((y == lr.predict(X)).all())
def test_binary_logistic_regression_gd(): t = np.array([[-0.28, 0.95], [-2.23, 2.4]]) lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=1, random_seed=1) lr.fit(X_bin, y_bin) np.testing.assert_almost_equal(lr.weights_, t, 2) assert (y_bin == lr.predict(X_bin)).all()
def test_multi_logistic_regression_gd_acc(): lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=1, random_seed=1) lr.fit(X, y) assert((y == lr.predict(X)).all())
def test_fail_minibatches(): lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=13, random_seed=1) lr.fit(X, y) assert((y == lr.predict(X)).all())
def test_score_function(): lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=1, random_seed=1) lr.fit(X, y) acc = lr.score(X, y) assert acc == 1.0, acc
def test_train_acc(): lr = TfSoftmaxRegression(epochs=3, eta=0.5, minibatches=1, random_seed=1) lr.fit(X, y) exp = [0.47, 0.65, 0.67] np.testing.assert_almost_equal(exp, lr.train_acc_, decimal=2)
def test_init_params(): t = np.array([[-0.28, 0.95], [-2.23, 2.4]]) lr = TfSoftmaxRegression(epochs=50, eta=0.5, minibatches=1, random_seed=1) lr.fit(X_bin, y_bin) lr.fit(X_bin, y_bin, init_params=False) np.testing.assert_almost_equal(lr.w_, t, 2) assert (y_bin == lr.predict(X_bin)).all()
def test_multi_logistic_regression_gd_weights(): t = np.array([[-0.94, -1.05, 2.73], [-2.17, 2.01, 2.51]]) lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=1, random_seed=1) lr.fit(X, y) np.testing.assert_almost_equal(lr.weights_, t, 2)
def test_binary_logistic_regression_sgd(): t = np.array([[0.35, 0.32], [-7.14, 7.3]]) lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=len(y_bin), random_seed=1) lr.fit(X_bin, y_bin) # 0, 1 class np.testing.assert_almost_equal(lr.weights_, t, 2) assert (y_bin == lr.predict(X_bin)).all()
def test_multi_logistic_probas(): lr = TfSoftmaxRegression(epochs=200, eta=0.75, minibatches=1, random_seed=1) lr.fit(X, y) idx = [0, 50, 149] # sample labels: 0, 1, 2 y_pred = lr.predict_proba(X[idx]) exp = np.array([[0.99, 0.01, 0.0], [0.01, 0.89, 0.1], [0.0, 0.02, 0.98]]) np.testing.assert_almost_equal(y_pred, exp, 2)
def test_binary_logistic_regression_gd(): t = np.array([[-0.28, 0.95], [-2.23, 2.4]]) lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=1, random_seed=1) lr.fit(X_bin, y_bin) np.testing.assert_almost_equal(lr.weights_, t, 2) assert((y_bin == lr.predict(X_bin)).all())
def test_binary_logistic_regression_sgd(): t = np.array([[0.35, 0.32], [-7.14, 7.3]]) lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=len(y_bin), random_seed=1) lr.fit(X_bin, y_bin) # 0, 1 class np.testing.assert_almost_equal(lr.weights_, t, 2) assert((y_bin == lr.predict(X_bin)).all())
def _clf_softmax(trX,teX,trY,teY): print "factors",factors(trX.shape[0]) print "enter no of mini batch" trY=trY.astype(int) teY=teY.astype(int) mini_batch=int(input()) clf = TfSoftmaxRegression(eta=0.75, epochs=100, print_progress=True, minibatches=mini_batch, random_seed=1) clf.fit(trX, trY) pred=clf.predict(teX) print _f_count(teY),"test f count" pred=pred.astype(np.int32) print _f_count(pred),"pred f count" conf_mat=confusion_matrix(teY, pred) process_cm(conf_mat, to_print=True) print precision_score(teY,pred),"Precision Score" print recall_score(teY,pred),"Recall Score" print roc_auc_score(teY,pred), "ROC_AUC"
def _clf_softmax(trX, teX, trY, teY): print "factors", factors(trX.shape[0]) print "enter no of mini batch" trY = trY.astype(int) teY = teY.astype(int) mini_batch = int(input()) clf = TfSoftmaxRegression(eta=0.75, epochs=100, print_progress=True, minibatches=mini_batch, random_seed=1) clf.fit(trX, trY) pred = clf.predict(teX) print _f_count(teY), "test f count" pred = pred.astype(np.int32) print _f_count(pred), "pred f count" conf_mat = confusion_matrix(teY, pred) process_cm(conf_mat, to_print=True) print precision_score(teY, pred), "Precision Score" print recall_score(teY, pred), "Recall Score" print roc_auc_score(teY, pred), "ROC_AUC"
def test_valid_acc(): lr = TfSoftmaxRegression(epochs=3, eta=0.5, minibatches=1, random_seed=1) lr.fit(X, y, X_valid=X[:100], y_valid=y[:100]) exp = [0.5, 0.5, 0.5] np.testing.assert_almost_equal(exp, lr.valid_acc_, decimal=2)
import matplotlib matplotlib.use('TKAgg') from mlxtend.tf_classifier import TfSoftmaxRegression from mlxtend.data import iris_data from mlxtend.evaluate import plot_decision_regions import matplotlib.pyplot as plt X, y = iris_data() X = X[:, [0, 3]] X[:,0] = (X[:,0] - X[:,0].mean()) / X[:, 0].std() X[:,1] = (X[:,1] - X[:,1].mean()) / X[:, 1].std() # Gradient Descent lr = TfSoftmaxRegression(eta = 0.75, epochs=20, print_progress=True, minibatches=len(y), random_seed=1) lr.fit(X, y) plt.plot(range(len(lr.cost_)), lr.cost_) plt.xlabel('Iterations') plt.ylabel('Cost') plt.show()
def test_multi_logistic_regression_gd_acc(): lr = TfSoftmaxRegression(epochs=100, eta=0.5, minibatches=1, random_seed=1) lr.fit(X, y) assert (y == lr.predict(X)).all()
import matplotlib matplotlib.use('TKAgg') from mlxtend.tf_classifier import TfSoftmaxRegression from mlxtend.data import iris_data from mlxtend.evaluate import plot_decision_regions import matplotlib.pyplot as plt X, y = iris_data() X = X[:, [0, 3]] X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std() X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std() # Gradient Descent lr = TfSoftmaxRegression(eta=0.75, epochs=20, print_progress=True, minibatches=len(y), random_seed=1) lr.fit(X, y) plt.plot(range(len(lr.cost_)), lr.cost_) plt.xlabel('Iterations') plt.ylabel('Cost') plt.show()