Created
January 12, 2017 12:03
-
-
Save Gananath/5b2f4640fdf96cd87a81a4206a0d3fe8 to your computer and use it in GitHub Desktop.
Neural network trained in kaggles lower back pain dataset
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
#This is a fun project for kaggles Lower Back Pain dataset | |
#https://www.kaggle.com/sammy123/lower-back-pain-symptoms-dataset | |
#Used a simple multi layer perceptron to train | |
#contact: https://gananath.github.io | |
import pandas as pd | |
import numpy as np | |
from keras.models import Sequential | |
from keras.layers import Dense,Dropout | |
from sklearn.cross_validation import train_test_split | |
from keras.utils import np_utils | |
#seed for random number reproduction | |
np.random.seed(2016) | |
#import dataset | |
data = pd.read_csv("/home/gananath/Desktop/Dataset_spine.csv",header=0) | |
data.head() | |
#mapping the data | |
d = {'Abnormal' : 1, 'Normal' : 0} | |
data['Class_att'] = data['Class_att'].map(d) | |
data = data.rename(columns={'Col1' : 'pelvic_incidence','Col2' : 'pelvic_tilt','Col3' : 'lumbar_lordosis_angle', | |
'Col4' : 'sacral_slope','Col5': 'pelvic_radius','Col6' : 'degree_spondylolisthesis', | |
'Col7' : 'pelvic_slope','Col8' : 'Direct_tilt','Col9': 'thoracic_slope', | |
'Col10' : 'cervical_tilt','Col11' : 'sacrum_angle','Col12' : 'scoliosis_slope'}) | |
del data['Unnamed: 13'] | |
data=data.reindex(np.random.permutation(data.index)) | |
data.head() | |
features = list(data.columns[:12]) | |
features | |
X = data[features] | |
y = data["Class_att"] | |
# one hot encode outputs | |
y=np_utils.to_categorical(y) | |
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) | |
#model building | |
model = Sequential() | |
model.add(Dense(X_train.shape[1], input_dim=X_train.shape[1], init='normal', activation='relu')) | |
model.add(Dense(20, init='normal', activation='relu')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(2,init='normal', activation='softmax')) | |
#compiling model | |
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy']) | |
model.summary() | |
history=model.fit(X_train.as_matrix(), y_train,validation_data=(X_test.as_matrix(),y_test), shuffle=True, nb_epoch=100, batch_size=15) | |
print "Accuracy: " + str(np.mean(history.history['val_acc'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment