Skip to content

Instantly share code, notes, and snippets.

@pkmital
Last active September 23, 2015 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkmital/77d8c768d9cf66cef7c0 to your computer and use it in GitHub Desktop.
Save pkmital/77d8c768d9cf66cef7c0 to your computer and use it in GitHub Desktop.
Pratyush
# Example using Decision Trees
import numpy as np
import matplotlib.pylab as plt
%matplotlib inline
all_data = []
with open("data.txt") as input_file:
for line in input_file:
all_data.append([item.replace('\r\n', '') for item in line.split('\t')[1:]])
X = np.vstack(all_data[1:11]).astype(float).transpose() # inputs
y = np.vstack(all_data[11:]).astype(float).transpose() # outputs
print X.shape, y.shape
from sklearn import cross_validation
from sklearn import preprocessing
# X = preprocessing.StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=.5)
from sklearn.tree import DecisionTreeRegressor
for depth in xrange(1, X.shape[1]):
clf = DecisionTreeRegressor(max_depth=depth).fit(X_train, y_train)
print 'Tree depth:', depth, 'Coefficient of determination, R^2: ', clf.score(X_test, y_test)
# Example using Neural Network
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
all_data = []
with open("data.txt") as input_file:
for line in input_file:
all_data.append([item.replace('\r\n', '') for item in line.split('\t')[1:]])
X = np.vstack(all_data[1:11]).astype(float).transpose() # inputs
y = np.vstack(all_data[11:]).astype(float).transpose() # outputs
X = preprocessing.StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=.8)
batch_size = 1
n_input = 10
n_hidden = 2
n_output = 24
model = Sequential()
model.add(Dense(n_input, n_hidden, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.1))
# model.add(Dense(n_hidden, n_hidden, init='uniform'))
# model.add(Activation('tanh'))
# model.add(Dropout(0.1))
model.add(Dense(n_hidden, n_output, init='uniform'))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=False)
model.compile(loss='mean_squared_error', optimizer=sgd)
model.fit(X_train, y_train, nb_epoch=10, batch_size=batch_size, verbose=2, show_accuracy=True)
score = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=2, show_accuracy=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment