Last active
November 12, 2017 20:20
-
-
Save mcny/64c0015e567f6f4363677b4a3ed910d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot | |
import numpy | |
from sklearn import datasets | |
from sklearn.model_selection import train_test_split | |
x, y = datasets.make_classification( | |
n_features=2, n_redundant=0, n_informative=2, n_classes=2, | |
random_state=1, n_clusters_per_class=1) | |
rng = numpy.random.RandomState(2) | |
x += 0 * rng.uniform(size=x.shape) | |
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=42) | |
number_of_features = x_train.shape[1] | |
number_of_training_data = x_train.shape[0] | |
number_of_test_data = x_test.shape[0] | |
y = numpy.append(y_train, y_test) | |
number_of_classes = len(numpy.unique(y)) | |
# draw data | |
# h = .02 | |
# X = numpy.vstack([x_train, x_test]) | |
# x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 | |
# y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 | |
# | |
# cm = matplotlib.pyplot.cm.jet | |
# # Plot the training points | |
# matplotlib.pyplot.scatter(x_train[:, 0], x_train[:, 1], c=y_train, cmap=cm, edgecolors='k', label='Training Data') | |
# # and testing points | |
# matplotlib.pyplot.scatter(x_test[:, 0], x_test[:, 1], c=y_test, cmap=cm, edgecolors='k', marker='x', linewidth=3, | |
# label='Test Data') | |
# | |
# matplotlib.pyplot.xlim(x_min, x_max) | |
# matplotlib.pyplot.ylim(y_min, y_max) | |
# matplotlib.pyplot.xticks(()) | |
# matplotlib.pyplot.yticks(()) | |
# matplotlib.pyplot.legend() | |
# matplotlib.pyplot.title(s="synthetic-easy") | |
# matplotlib.pyplot.show() | |
number_of_features = x_train.shape[1] | |
w_vector = numpy.random.rand(number_of_features + 1) | |
x_extended = numpy.hstack([x_train, numpy.ones([x_train.shape[0], 1])]) | |
w_t_x = numpy.dot(x_extended, w_vector) | |
my_sigmoid = numpy.divide(1, numpy.add(1, numpy.exp(-w_t_x))) | |
y_n = my_sigmoid.reshape(-1) | |
loss2 = -(numpy.dot(y_train, numpy.log(y_n)) + numpy.dot(numpy.subtract(1, y_train), numpy.log(numpy.subtract(1, y_n)))) | |
d_loss = loss2 | |
eta = 1.01 | |
while d_loss > 0.05 or d_loss < 0.0: | |
gradient_e = numpy.dot(numpy.subtract(y_n, y_train), x_extended) | |
w_vector = numpy.subtract(w_vector, numpy.multiply(eta, gradient_e)) | |
w_t_x = numpy.dot(x_extended, w_vector) | |
my_sigmoid = numpy.divide(1, numpy.add(1, numpy.exp(-w_t_x))) | |
y_n = my_sigmoid.reshape(-1) | |
y_n[y_n < 0.05] = 0.05 | |
y_n[y_n >= 1] = 0.95 | |
loss1 = loss2 | |
loss2 = -( | |
numpy.dot(y_train, numpy.log(y_n)) + numpy.dot(numpy.subtract(1, y_train), numpy.log(numpy.subtract(1, y_n)))) | |
if abs(loss1 > loss2) < d_loss: | |
eta = 1.01 | |
else: | |
eta = -0.5 | |
d_loss = abs(loss1 - loss2) | |
print(w_vector) | |
assert len(w_vector) == x_train.shape[1] + 1 | |
reshaped_w_vector = numpy.reshape(w_vector, (-1, 1)) | |
x_extended = numpy.hstack([x_train, numpy.ones([x_train.shape[0], 1])]) | |
y_predicted = numpy.ravel(numpy.sign(numpy.dot(x_extended, reshaped_w_vector))) | |
y_predicted_final = numpy.maximum(numpy.zeros(y_predicted.shape), y_predicted) | |
assert len(y_predicted_final) == len(y_train) | |
training_score = numpy.sum(y_predicted_final == y_train) / float(len(y_predicted_final)) | |
print(training_score) | |
x_extended_test = numpy.hstack([x_test, numpy.ones([x_test.shape[0], 1])]) | |
y_predicted_test = numpy.ravel(numpy.sign(numpy.dot(x_extended_test, reshaped_w_vector))) | |
y_predicted_final_test = numpy.maximum(numpy.zeros(y_predicted_test.shape), y_predicted_test) | |
assert len(y_predicted_final_test) == len(y_test) | |
testing_score = numpy.sum(y_predicted_final_test == y_test) / float(len(y_predicted_final_test)) | |
print(testing_score) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment