use machine learning to recognize robot's posture (following the example in scikit-learn-intro.ipynb )
We have colleceted data before, you need to add new data if you want to add new posture.
pickle
, e.g. pickle.load(open('Back'))
, the data is a list of feature dataPerception.imu
) and others are joint angles.
In [51]:
%pylab inline
import pickle
from os import listdir, path
import numpy as np
from sklearn import svm, metrics
ROBOT_POSE_DATA_DIR = 'robot_pose_data'
In [52]:
classes = listdir(ROBOT_POSE_DATA_DIR)
print classes
In [84]:
def load_pose_data(i):
'''load pose data from file'''
data = []
target = []
# YOUR CODE HERE
filename = path.join(ROBOT_POSE_DATA_DIR, classes[i])
data = pickle.load(open(filename))
target = [i] * len(data)
return data, target
In [61]:
# load all the data
all_data = []
all_target = []
# YOUR CODE HERE
print 'total number of data', len(all_data)
In [86]:
# shuffule data
permutation = np.random.permutation(len(all_data))
n_training_data = int(len(all_data) * 0.7)
training_data = permutation[:n_training_data]
In [56]:
clf = svm.SVC(gamma=0.001, C=100.)
In [57]:
# YOUR CODE HERE
Out[57]:
In [58]:
clf.predict(all_data[-1]), all_target[-1]
Out[58]:
In [81]:
def evaluate(expected, predicted):
print("Classification report:\n%s\n" % metrics.classification_report(expected, predicted))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
In [82]:
expected = []
predicted = []
# YOUR CODE HERE
evaluate(expected, predicted)
In [83]:
expected = []
predicted = []
# YOUR CODE HERE
evaluate(expected, predicted)
In [68]:
import pickle
ROBOT_POSE_CLF = 'robot_pose.pkl'
pickle.dump(clf, open(ROBOT_POSE_CLF, 'w'))
Then, in the application we can load the trained classifier again.
In [71]:
clf2 = pickle.load(open(ROBOT_POSE_CLF))
clf2.predict(all_data[-1]), all_target[-1]
Out[71]:
In [ ]: