<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Random Forest classifier example
Step1:  Watch the video of the type of activity that was recorded
Step2:  The data is extracted from the .zip file on the UCI website
Step3:  The dataset contains readings from mobile phone accelerometer and gyroscope as subjects perform a range of actions. The raw data is then manipulated to form 560 features from the initial 6 features (accelerometer 3-axis and gyroscope 3-axis).
Step4:  A training set of 7352 readings (X_train) labeled as one of six activities (y_train) is used to train a random forest classifier.
Step5:  A quick sanity check where the training data is classified by the model - should be 100% accurate or very close
Step6:  A second unseen dataset of 2947 datapoints is used to test the classifier.
Step7:  Predictions for the unseen dataset are created.
Step8:  The correct labels are loaded and prediction accuracy is tested
Step9:  The features with an importance >.015 are -
Step10:  The original paper used a SVC from R to classify the data. Here is a comparison in Python using the same type of classifier.
Step11:  The confusion matrix from the SVC in sklearn (Python) was very close to the result obtained by the original authors using R. (see paper https
Step12:  An alternative model using a Random Forest Classifier
<ASSISTANT_TASK:>
Python Code:
from sklearn.ensemble import RandomForestClassifier
Explanation: Random Forest classifier example
End of explanation
from IPython.display import YouTubeVideo, HTML
YouTubeVideo("XOEN9W05_4A")
#The Donald Bren School of Information and Computer Sciences - University of California, Irvine
info_file = 'http://archive.ics.uci.edu/ml/machine-learning-databases/00240/UCI%20HAR%20Dataset.names'
data_zip = 'http://archive.ics.uci.edu/ml/machine-learning-databases/00240/UCI%20HAR%20Dataset.zip'
!curl $info_file
Explanation: Watch the video of the type of activity that was recorded
End of explanation
import requests, zipfile, io
r = requests.get(data_zip)
z = zipfile.ZipFile(io.BytesIO(r.content))
X_train_df = pd.read_csv(z.open('UCI HAR Dataset/train/X_train.txt'), skipinitialspace=True, sep=' ', header=None)
X_train = X_train_df.values
y_train_df = pd.read_csv(z.open('UCI HAR Dataset/train/y_train.txt'), header=None)
y_train = y_train_df[0].values
Explanation: The data is extracted from the .zip file on the UCI website
End of explanation
X_train.shape
y_train.shape
Explanation: The dataset contains readings from mobile phone accelerometer and gyroscope as subjects perform a range of actions. The raw data is then manipulated to form 560 features from the initial 6 features (accelerometer 3-axis and gyroscope 3-axis).
End of explanation
clf = RandomForestClassifier(n_jobs=2,n_estimators=150)
clf.fit(X_train, y_train)
Explanation: A training set of 7352 readings (X_train) labeled as one of six activities (y_train) is used to train a random forest classifier.
End of explanation
from sklearn.metrics import accuracy_score, roc_auc_score
y_predict = clf.predict(X_train)
accuracy = accuracy_score(y_train, y_predict)
print('---'*20)
print('\n')
print("A Random Forest Classifier with 150 estimators was {}% accurate on the test data.".format(round(accuracy*100,2)))
print('\n')
print('---'*20)
Explanation: A quick sanity check where the training data is classified by the model - should be 100% accurate or very close
End of explanation
X_test_df = pd.read_csv(z.open('UCI HAR Dataset/test/X_test.txt'), skipinitialspace=True, sep=' ', header=None)
X_test = X_test_df.values
y_test_df = pd.read_csv(z.open('UCI HAR Dataset/test/y_test.txt'), header=None)
y_test = y_test_df[0].values
X_test.shape
y_test.shape
Explanation: A second unseen dataset of 2947 datapoints is used to test the classifier.
End of explanation
y_predict_test = clf.predict(X_test)
Explanation: Predictions for the unseen dataset are created.
End of explanation
test_score = accuracy_score(y_test, y_predict_test)
print('---'*20)
print('\n')
print('A random Forest Classifier with 150 estimators, trained on the training data, was {}% accurate at labelling the unseen test data.'.format(round(test_score*100,2)))
print('\n')
print('---'*20)
import matplotlib as mpl
order = clf.feature_importances_
order.sort()
mpl.pylab.plot(order)  
importance = pd.DataFrame(clf.feature_importances_)
feature = pd.read_csv(z.open('UCI HAR Dataset/features.txt'),sep=' ',header=None)
feature['importance'] = clf.feature_importances_
feature.columns = [0, 'transform', 'importance']
Explanation: The correct labels are loaded and prediction accuracy is tested
End of explanation
for feat in feature.transform[feature.importance>.015].values:
    print(feat,feature.importance[feature.transform==feat].values)
activity_labels = pd.read_csv(z.open('UCI HAR Dataset/activity_labels.txt'),index_col=0,sep=' ',header = None)
labels_activity = [x.lower() for x in activity_labels[1].tolist()]
labels_activity
from sklearn import metrics
# testing score
score_test = metrics.f1_score(y_test, y_predict_test, pos_label=list(set(y_test)),average = None)
# training score
score_train = metrics.f1_score(y_train, y_predict, pos_label=list(set(y_train)),average = None)
print('\n','metrics.f1_score for each category in training set','\n','----'*10)
for act,dec in zip(labels_activity,score_train):
    print('{} - {}%'.format(act,round(dec*100,2)))
print('\n','metrics.f1_score for each category in test set','\n','----'*10)
for act,dec in zip(labels_activity,score_test):
    print('{} - {}%'.format(act,round(dec*100,2)))
Explanation: The features with an importance >.015 are -
End of explanation
from sklearn import svm, datasets
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (14.0, 10.0)
# Run classifier
classifier = svm.SVC(kernel='linear')
y_predictSVC = classifier.fit(X_train, y_train).predict(X_test)
y_values = [0,1,2,3,4,5]
activity = labels_activity
# Compute confusion matrix
conf_arr = confusion_matrix(y_test, y_predictSVC)
norm_conf = []
for i in conf_arr:
    a = 0
    tmp_arr = []
    a = sum(i, 0)
    for j in i:
        tmp_arr.append(float(j)/float(a))
    norm_conf.append(tmp_arr)
width = len(conf_arr)
height = len(conf_arr[0])
for x in range(width):
    for y in range(height):
        ax.annotate(str(conf_arr[x][y]), xy=(y, x), 
                    horizontalalignment='center',
                    verticalalignment='center')
# Show confusion matrix in a separate window
cb = fig.colorbar(res)
plt.matshow(conf_arr,cmap=plt.cm.winter)
plt.title('Confusion matrix for SVM',y=1.5)
plt.colorbar(cmap=plt.cm.YlGn)
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.yticks(y_values, activity)
plt.xticks(y_values, activity,rotation = 90,)
plt.grid(b = False, which='major', axis='both')
width = len(conf_arr)
height = len(conf_arr[0])
for x in range(width):
    for y in range(height):
        plt.annotate(str(conf_arr[x][y]), xy=(y, x), 
                    horizontalalignment='center',
                    verticalalignment='center')
#plt.show()
Explanation: The original paper used a SVC from R to classify the data. Here is a comparison in Python using the same type of classifier.
End of explanation
accuracy = accuracy_score(y_test, y_predictSVC)
print('---'*20)
print('\n')
print("A SVC was {}% accurate on the test data.".format(round(accuracy*100,2)))
print('\n')
print('---'*20)
Explanation: The confusion matrix from the SVC in sklearn (Python) was very close to the result obtained by the original authors using R. (see paper https://www.elen.ucl.ac.be/Proceedings/esann/esannpdf/es2013-84.pdf)
End of explanation
from sklearn.ensemble import RandomForestClassifier
#from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (14.0, 10.0)
# Run classifier
#classifier = svm.SVC(kernel='linear')
#y_predictSVC = classifier.fit(X_train, y_train).predict(X_test)
y_values = [0,1,2,3,4,5]
activity = labels_activity
# Compute confusion matrix
conf_arr = confusion_matrix(y_test, y_predict_test)
norm_conf = []
for i in conf_arr:
    a = 0
    tmp_arr = []
    a = sum(i, 0)
    for j in i:
        tmp_arr.append(float(j)/float(a))
    norm_conf.append(tmp_arr)
width = len(conf_arr)
height = len(conf_arr[0])
for x in range(width):
    for y in range(height):
        ax.annotate(str(conf_arr[x][y]), xy=(y, x), 
                    horizontalalignment='center',
                    verticalalignment='center')
# Show confusion matrix in a separate window
cb = fig.colorbar(res)
plt.matshow(conf_arr,cmap=plt.cm.winter)
plt.title('Confusion matrix for Random Forest Classifier',y=1.5)
plt.colorbar(cmap=plt.cm.YlGn)
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.yticks(y_values, activity)
plt.xticks(y_values, activity,rotation = 90,)
plt.grid(b = False, which='major', axis='both')
width = len(conf_arr)
height = len(conf_arr[0])
for x in range(width):
    for y in range(height):
        plt.annotate(str(conf_arr[x][y]), xy=(y, x), 
                    horizontalalignment='center',
                    verticalalignment='center')
#plt.show()
accuracy = accuracy_score(y_test, y_predict_test)
print('---'*20)
print('\n')
print("A Random Forest Classifier with 150 estimators was {}% accurate on the test data.".format(round(accuracy*100,2)))
print('\n')
print('---'*20)
Explanation: An alternative model using a Random Forest Classifier
End of explanation
<END_TASK>