Skip to content

Instantly share code, notes, and snippets.

@cnh
Created November 15, 2017 11:18
Show Gist options
  • Save cnh/7dc65b9469ee8261102061a416f805eb to your computer and use it in GitHub Desktop.
Save cnh/7dc65b9469ee8261102061a416f805eb to your computer and use it in GitHub Desktop.
#import data manipulation modules
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import train_test_split
from keras.models import Sequential, load_model #to save , load and persist models
from keras.layers import Activation
from keras.optimizers import SGD
from keras.layers import Dense, Sequential
from keras.utils import np_utils
#10nov2017, 0951ist
from keras.util import to_categorical
from imutils import paths
#for saving model params, weights, persist, cumulative, blah blah
import h5py
#read data and data wrangling
#metf_n_data = pd.read_csv("./data/METF_normalized.csv")
#added a row for response
#metf = metformin, n = normalize, r= raw dat, from csv, before doing transpose(T) & other manipulations
metf_n_r_data = pd.read_csv("./data/metf_n_resp.csv")
thiog_n_data = pd.read_csv("./data/THIOG_normalized.csv")
#transpose metf_n_r_data, features in columns, samples(patient data) as rows
metf_n_r_t_data = metf_n_r_data.T
#drop the target column, and save rest of the sample data, as
#predictors = metf_n_data.drop(['response'], axis=1).as_matrix()
#create a copy of metf_n_r_t_data (its too long a name !!!, plus experiments) in df0
df0 = metf_n_r_t_data.copy(deep=True)
df_no_r = df0.drop(df0.columns[[0]])
predictors = df_no_r.as_matrix()
target = to_categorical(metf_n_r_data.response)
#build the model
#todo : in v1, explore more complex models other than sequential
model = Sequential()
#input layer
# pick top 50/100 features, not all 1600
model.add(Dense(100, activation='relu'))
#2 hidden layers
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
#output layer, 2 classes, softmax activation function
model.add(Dense(2,activation='softmax'))
#added by ashish, 10nov2017, 1257ist
'''
def get_new_model(input_shape = input_shape):
model = Sequential()
#input layer,pick top 50/100 features, not all 1600
model.add(Dense(100, activation='relu'))
#2 hidden layers
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
#output layer, 2 classes, softmax activation function
model.add(Dense(2,activation='softmax'))
return model
'''
'''
model.add(Dense(units=1000, input_dim=100))
model.add(Activation('relu'))
#add hidden layer1
model.add(Dense(units=10))
model.add(Activation('softmax'))
#add hidden layer2
model.add(Dense(units=10))
model.add(Activation('softmax'))
'''
#model.compile(loss=keras.losses.categorical_crossentropy,
# optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))
#since our usecase is classification (0===nr, 1===responder)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])
#for loop, over lr(learning rate), to generate models
'''
lr_to_test = [0.000001, 0.01,1]
for lr in lr_to_test:
model = get_new_model()
my_optimizer = SGD(lr=lr)
model.compile(optimizer=my_optimizer, loss='categorical_crossentropy')
model.fit(predictors,target)
'''
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
#model.fit(x_train, y_train, epochs=5, batch_size=32)
#model.train_on_batch(x_batch, y_batch)
#loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
#classes = model.predict(x_test, batch_size=128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment