Skip to content

Commit

Permalink
Neural network:feed forward and trained_model saved for future use of…
Browse files Browse the repository at this point in the history
… predictions
  • Loading branch information
shahanesanket committed Apr 19, 2017
1 parent 248d795 commit a2423b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions python/feedforward_nn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Train model and make predictions
import numpy as np
import pandas
import h5py
from keras.models import Sequential
from keras.layers import Dense
#from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
#from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
# load dataset
dataframe = pandas.read_csv("../data/scaled_data/scaled_pca.csv")
dataframe = dataframe.sample(frac=1).reset_index(drop=True)
dataset = dataframe.values
X = dataset[:,:-1].astype(float)
Y = dataset[:,-1]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(encoded_Y)

# create model
model = Sequential()
model.add(Dense(12, input_dim=75, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(3, kernel_initializer='normal', activation='sigmoid'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X[:], dummy_y, nb_epoch=1000, batch_size=150, verbose=1)

# calculate predictions
predictions = model.predict(X)
print predictions
predictions = np.argmax(predictions,axis=1)
classes = np.unique(Y)
p = classes[predictions]
# print the accuracy
print sum(Y == p)/float(len(Y))

import pickle
with open('../trained_models/ff_nn_1.pkl','wb') as f:
pickle.dump(model)
Binary file added trained_models/ff_nn_1.h5
Binary file not shown.
Binary file added trained_models/ff_nn_2.h5
Binary file not shown.

0 comments on commit a2423b3

Please sign in to comment.