print('****Minibatch Gradient Descent****')
print('\n--Training--\n')
hyperparam = {'eta': 0.3,
              'epochs': 300,
              'minibatches': 1,
              'adaptive': 0.99}
print('\nHyperparamters\n')
for k, v in hyperparam.items():
    print(k, '\t', v)
print('\nNumber of Training Examples: ', X_train.shape[0], '\n')

h_thetaf, cost = glm.fit(lr.J,
                         lr.gradJ,
                         hyperparam,
                         scaledtrain_data)
plot_cost(cost)
h_thetad = scale.denormalize(h_thetaf)
print('Coefficients\t', h_thetaf)
for i, h_theta in enumerate(h_thetad):
    print('h_theta' + str(i), '\t', h_theta)
yp_train = glm.predict(identity, X_train, h_thetaf)
plot_errors(y_train, yp_train)
corr_train = metrics.r2(X_train, y_train, h_thetaf)
print('R**2\t', corr_train)

print('\n--Testing--')
yp_test = glm.predict(identity, X_test, h_thetaf)
plot_errors(y_test, yp_test)
corr_test = metrics.r2(X_test, y_test, h_thetaf)
print('R**2\t', corr_test)
Exemple #2
0
# Fit the training data
h_theta0 = [1., 1., 1.]
print('****Gradient Descent****\n')
print('--Training--\n')
h_thetaf, cost = glm.fit(logr.logistic_log_likelihood,
                         logr.grad_logistic,
                         h_theta0,
                         scaled_train,
                         eta=0.03,
                         it_max=500,
                         gf='gd')

logr.plot_cost(cost)
print(h_thetaf)

probs_train = glm.predict(logr.logistic, scaledX_train, h_thetaf)
yp_train = logr.logistic_classes(probs_train)
logistic_table(probs_train, yp_train, y_train)
print('--Testing--\n')
# Use the training statistics to scale the test data
X_test, y_test = zip(*test_data)
scaledX_test = transform(X_test)
scaled_test = zip(scaledX_test, y_test)
probs_test = glm.predict(logr.logistic, scaledX_test, h_thetaf)
yp_test = logr.logistic_classes(probs_test)
logistic_table(probs_test, yp_test, y_test)
print('\n\n****Stochastic Gradient Descent****')
print('\n--Training--')
h_thetaf, cost = glm.fit(logr.logistic_log_likelihood_i,
                         logr.logistic_log_gradient_i,
                         h_theta0,
y_test = np.array(y_test)
scaledX_test = scale.transform(Z_test)

hyperparam = {'eta': 0.5,
              'epochs': 1000,
              'minibatches': 4,
              'adaptive': 0.98}

weightsf, cost = glm.fit(lr.SMC,
                         lr.gradSMC,
                         hyperparam,
                         zip(scaledX_train, y_train))

# Print out the results
print('****Training****\n')
prediction = glm.predict(lr.softmax, scaledX_train, weightsf)
classes = lr.classify(prediction)
print('\nWeight Matrix\n', weightsf)
print('\nError\n', cost[-1])

logistic_table(prediction, classes, np.argmax(np.array(y_train), axis=1))
score = MScores(y_train, encode_labels(np.array(classes), 3))
print('Precision: ', score.precision())
print('Recall: ', score.recall())
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.semilogy(range(0, len(cost)), cost)
plt.show()
plt.clf()

print('****Testing****\n')
prediction = glm.predict(lr.softmax, scaledX_test, weightsf)
Exemple #4
0
# Scale the testing data using the same scaling parameters
# used for the training data
Z_test, y_test = zip(*test_data)
X_test = transform(Z_test)

h_theta0 = [0., 0., 0., 0., 0.]
print('****Gradient Descent****')
h_thetaf, cost = glm.fit(lr.J,
                         lr.gradJ,
                         h_theta0,
                         eta=0.3,
                         it_max=5000,
                         gf='gd')(scaledtrain_data)
lr.plot_cost(cost)
h_thetad = scale.denormalize(h_thetaf)
yp_train = glm.predict(identity, X_train, h_thetaf)

print('\n--Training--')
print('Coefficients\t', h_thetaf)
print(
    tabulate(list(zip(yp_train, y_train)),
             headers=['yp', 'yi'],
             tablefmt='fancy_grid'))

print('Coefficients\t', h_thetad)
for i, h_theta in enumerate(h_thetad):
    print('h_theta' + unicode(i), '\t', h_theta)

corr_train = metrics.r2(X_train, y_train, h_thetaf)
print('R**2\t', corr_train)
scaled_test = zip(scaledX_test, y_test)
print('****Gradient Descent****\n')
h_theta0 = [1., 1., 1.]
h_thetaf, cost = glm.fit(logr.logistic_log_likelihood,
                         logr.grad_logistic, 
                         h_theta0, 
                         scaled_train,
                         eta=0.1,
                         it_max=1000,
                         gf='gd')
print('--Training--\n')
print(h_thetaf)
h_thetad = scale.denormalize(h_thetaf)
print(h_thetad)
logr.plot_cost(cost)
probs_train = glm.predict(logr.logistic, scaledX_train, h_thetaf)
yp_train = logr.logistic_classes(probs_train)
logistic_table(probs_train, yp_train, y_train)
print('--Testing--\n')
probs_test = glm.predict(logr.logistic, scaledX_test, h_thetaf)
yp_test = logr.logistic_classes(probs_test)
logistic_table(probs_test, yp_test, y_test)
score = metrics.Scores(y_test, yp_test)
print('True Positives\t', score.tp)
print('False Positives\t', score.fp)
print('False Negatives\t', score.fn)
print('True Negatives\t', score.tn)
print('Precision: ', score.precision())
print('Recall: ', score.recall())

print('****Stochastic Gradient Descent****\n')