Exemplo n.º 1
0
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())
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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())
Exemplo n.º 4
0
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())
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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()
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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()
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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())
Exemplo n.º 12
0
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())
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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()
Exemplo n.º 15
0
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"
Exemplo n.º 16
0
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"
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
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()
Exemplo n.º 19
0
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)
Exemplo n.º 20
0
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
Exemplo n.º 21
0
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()
Exemplo n.º 22
0
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()
Exemplo n.º 23
0
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)