This code aims to generate an animation from the data from the ann


In [151]:
import numpy as np
from sklearn.neural_network import MLPRegressor
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
import pickle #saving to file
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from sklearn.grid_search import GridSearchCV # looking for parameters
import datetime

Preprocessing data


In [142]:
#this function reads the file 
def read_data(archive, rows, columns):
    data = open(archive, 'r')
    mylist = data.read().split()
    data.close()
    myarray = np.array(mylist).reshape(( rows, columns)).astype(float)
    return myarray

In [146]:
# getting data and preprocessing
data = read_data('../get_data_example/set.txt',72, 12)
#training_set = [0av_left, 1av_right, 2angles_left, 
#                                         3angles_right, 4aa_left, 5aa_right, 678vxyz_left, 91011vxyz_right];
X = data[:, [4, 5, 6, 7, 8, 9, 10, 11]]
print X.shape
#The angles of the knees[left, right]
angle_knees = np.vstack((data[:,2], data[:, 3])).T
print angle_knees.shape

#print pre_X.shape, data.shape
y  = data[:,3] #1
#print y.shape

#getting the time vector for plotting purposes
time_stamp = np.zeros(data.shape[0])
for i in xrange(data.shape[0]):
        time_stamp[i] = i*(1.0/60.0)
        
#print X.shape, time_stamp.shape
X = np.hstack((X, time_stamp.reshape((X.shape[0], 1))))
print X.shape

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
t_test = X_test[:,-1]
t_train = X_train[:, -1]
X_train_std = preprocessing.scale(X_train[:,0:-1])
X_test_std = preprocessing.scale(X_test[:, 0:-1])
print"Test vector parameters", X_test_std.shape


(72, 8)
(72, 2)
(72, 9)
Test vector parameters (29, 8)

In [148]:
#Here comes the way to sort out the data according to one the elements of it
test_sorted = np.hstack(
    (t_test.reshape(X_test_std.shape[0], 1), X_test_std, y_test.reshape(X_test_std.shape[0], 1)))

test_sorted = test_sorted[np.argsort(test_sorted[:,0])] #modified

train_sorted = np.hstack((t_train.reshape(t_train.shape[0], 1), y_train.reshape(y_train.shape[0], 1) ))
train_sorted = train_sorted[np.argsort(train_sorted[:,0])]

In [149]:
#Splitting data for test and for validation
y_validation = y_test[0:int(y_test.shape[0]/2)]
y_test1 = y_test[int(y_test.shape[0]/2):]

X_validation = X_test_std[0:int(y_test.shape[0]/2),:]
X_test_std1 = X_test_std[int(y_test.shape[0]/2):, :]

print y_test.shape, y_validation.shape, y_test1.shape
print X_test_std.shape, X_validation.shape, X_test_std1.shape


(29,) (14,) (15,)
(29, 8) (14, 8) (15, 8)

Redoing the network for the angles: Animation


In [150]:
#Grid search, random state =0: same beginning for all
alpha1 = np.linspace(0.001,0.9, 4).tolist()
momentum1 = np.linspace(0.3,0.9, 4).tolist()
params_dist = {"hidden_layer_sizes":[(10, 15), (15, 20), (15, 10), (15, 7)],
               "activation":['tanh','logistic'],"algorithm":['sgd', 'l-bfgs'], "alpha":alpha1,
               "learning_rate":['constant'],"max_iter":[500], "random_state":[None],
               "verbose": [False], "warm_start":[False], "momentum":momentum1}
grid = GridSearchCV(MLPRegressor(), param_grid=params_dist)
grid.fit(X_validation, y_validation)

print "Best score:", grid.best_score_
print "Best parameter's set found:\n"
print grid.best_params_


Best score: 0.776127432254
Best parameter's set found:

{'warm_start': False, 'verbose': False, 'algorithm': 'l-bfgs', 'hidden_layer_sizes': (15, 20), 'activation': 'tanh', 'max_iter': 500, 'random_state': None, 'alpha': 0.3006666666666667, 'learning_rate': 'constant', 'momentum': 0.9}

In [139]:
new  = MLPRegressor(activation= 'logistic', algorithm= 'l-bfgs', alpha= 0.001, batch_size= 'auto', beta_1= 0.9,
                    beta_2= 0.999, early_stopping= False, epsilon= 1e-08, hidden_layer_sizes= (10, 15),
                    learning_rate= 'constant', learning_rate_init= 0.001, max_iter= 500, momentum= 0.8, 
                    nesterovs_momentum= True, power_t= 0.5, random_state= None, shuffle= False, tol= 1e-09,
                    validation_fraction= 0.1, verbose= False,warm_start= False)

new.fit(X_train_std, y_train)
print new.score(X_test_std, y_test)


0.770817743925

In [155]:
##Getting the precision(IT MUST BE DONE THE LEAST NUMBER OF TIMES AS POSSIBLE)
print "Last used at:", datetime.datetime.now()
print "Accuracy for the test set", grid.score(X_test_std1, y_test1)


Last used at: 2016-09-03 15:37:32.244338
Accuracy for the test set 0.767130014128

In [163]:
##Plotting it 
%matplotlib inline

results = grid.predict(test_sorted[:, 1:-1])

plt.plot(test_sorted[:, 0], results, c='r', label="Predicted") # ( sorted time, results)
plt.plot(train_sorted[:, 0], train_sorted[:,1], c='b', label="Expected" ) #expected
plt.scatter(time_stamp, y, c='k', label="Original")
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=3, mode="expand", borderaxespad=0.)
plt.xlabel("Time(s)")
plt.ylabel("Angle(rad)")

plt.title("MLP results vs Expected values")
plt.show()
print "Accuracy for the validation set", grid.score(X_validation, y_validation)


Accuracy for the validation set 0.824045751024

In [162]:
##save to file/ load from file
save = "no"
if save == "yes":
        f= open("angles.ann", "w")
        f.write(pickle.dumps(grid))
        f.close
        print "Saved to file"
elif save =="load":
    f= open("angles.ann", "r")
    grid= pickle.loads(f.read())
    f.close
    print "Loaded from file"
else:
    print "Nothing to do"


Nothing to do

The animation itself


In [194]:
import math
from mpl_toolkits.mplot3d import Axes3D

pox =[]
poy = []
xn = []
yn = []
for i in  results:
    pox.append(math.cos(i))
    poy.append(math.sin(i))
    
for i in train_sorted[:, 1]:
    xn.append(math.cos(i))
    yn.append(math.sin(i))
    

plt.plot(test_sorted[:, 0], pox, '-.', color= 'r', label="x positions")
plt.plot(test_sorted[:, 0], poy, '-.', color= 'b',label="x positions")
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=1, ncol=2, mode="expand", borderaxespad=0.)
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_wireframe(test_sorted[:, 0], pox, poy)
ax.plot_wireframe(train_sorted[:, 0], xn, yn, color = 'r')
plt.show()



In [ ]: