Last active
July 18, 2019 19:11
-
-
Save michelkana/aa703ddcd0056b39d74b0957c3b85e9f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 keras | |
from keras.models import Sequential | |
from keras.layers import Dense, Input | |
from keras.utils import to_categorical | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import accuracy_score | |
# turn the x,y coordinates into a predictors matrix | |
X = np.array([x, y]).T | |
# turn the labels into 1-hot encodings | |
Y = np.array(labels).reshape(-1,1) | |
Y= to_categorical(Y) | |
# split the data into training and validation sets | |
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5, | |
shuffle=True, | |
random_state=999) | |
# create a network with 2 layers | |
model = Sequential() | |
model.add(Dense(50, input_shape=(2,), activation='relu')) | |
model.add(Dense(3, activation='softmax')) | |
model.summary() | |
# train the network | |
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) | |
history = model.fit(X_train,Y_train, | |
batch_size=5, | |
epochs=20, | |
shuffle=True, | |
verbose=0, | |
validation_data=(X_test, Y_test)) | |
# perform predictions | |
Y_test_pred = model_cl.predict_proba(X_test) | |
Y_train_pred = model_cl.predict_proba(X_train) | |
Y_test_label = np.argmax(Y_test,axis=1) | |
Y_train_label = np.argmax(Y_train,axis=1) | |
Y_test_pred_label = np.argmax(Y_test_pred,axis=1) | |
Y_train_pred_label = np.argmax(Y_train_pred,axis=1) | |
# plot predictions | |
fig, ax = plt.subplots(1,2,figsize=(15,5)) | |
for cloud in clouds: | |
i = np.where(Y_test_label==cloud['label'], True, False) | |
ax[0].scatter(X_test[i][:,0], X_test[i][:,1], marker=cloud['marker'], color=cloud['color'], label=cloud['label']) | |
j = np.where(Y_test_pred_label==cloud['label'], True, False) | |
ax[1].scatter(X_test[j][:,0], X_test[j][:,1], marker=cloud['marker'], color=cloud['color'], label=cloud['label']) | |
ax[0].set_xlabel('x') | |
ax[0].set_ylabel('y') | |
ax[0].legend() | |
ax[0].set_title('test data with original labels') | |
ax[0].legend(); | |
ax[1].set_xlabel('x') | |
ax[1].set_ylabel('y') | |
ax[1].legend() | |
ax[1].set_title('test data with predicted labels'); | |
# print accuracy | |
print('Test accuracy: {0:0.2%}'.format(accuracy_score(Y_test_label, Y_test_pred_label))) | |
print('Training accuracy: {0:0.2%}'.format(accuracy_score(Y_train_label, Y_train_pred_label))) | |
ax[1].legend(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment