예제 #1
0
train_data, test_data = train_test_split(zip(Xpp, y), 0.33)
# Scale the data
X_train, y_train = zip(*train_data)
scale = Scaler()
scale.fit(X_train)
transform = compose(prepend_x0, scale.transform)
scaledX_train = transform(X_train)
scaled_train = zip(scaledX_train, y_train)
# 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)
예제 #2
0
Z_test, y_test = zip(*test_data)
X_test = scale.transform(Z_test)

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)
예제 #3
0
scale = Scaler()
Z_train, y_train = zip(*train_data)
scale.fit(Z_train)
transform = compose(prepend_x0, scale.transform)
X_train = transform(Z_train)
scaledtrain_data = zip(X_train, y_train)
# 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):
예제 #4
0
Z_train, y_train = zip(*train_data)
y_train = np.array(y_train)
scale = Scaler()
scale.fit(Z_train)
scaledX_train = scale.transform(Z_train)
Z_test, y_test = zip(*test_data)
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)
예제 #5
0
train_data, test_data = train_test_split(massaged_data, 0.33)
Z_train, y_train = zip(*train_data)       
scale = Scaler()
scale.fit(Z_train)
transform = compose(prepend_x0, scale.transform)
scaledX_train = transform(Z_train)
scaled_train = zip(scaledX_train, y_train)
Z_test, y_test = zip(*test_data)
scaledX_test = transform(Z_test)
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)
예제 #6
0
scale = Scaler()
Z_train, y_train = zip(*train_data)
scale.fit(Z_train)
transform = compose(prepend_x0, scale.transform)
X_train = transform(Z_train)
scaledtrain_data = zip(X_train, y_train)
# 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):
예제 #7
0
scale = Scaler()
scale.fit(Z_train)
scaledX_train = scale.transform(Z_train)
scaled_train = list(zip(scaledX_train, y_train))
Z_test, y_test = zip(*test_data)
scaledX_test = scale.transform(Z_test)
scaled_test = list(zip(scaledX_test, y_test))
# Initialize the patameters

print('****Minibatch Gradient Descent****\n')
hyperparam = {'eta': 0.1,
              'epochs': 500,
              'minibatches': 1,
              'adaptive': 1.0}
h_thetaf, cost = glm.fit(logr.LLL,
                         logr.gradL,
                         hyperparam,
                         scaled_train)
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.classify(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.classify(probs_test)
logistic_table(probs_test, yp_test, y_test)
score = metrics.Scores(y_test, yp_test)
print('True Positives\t', score.tp)