决策树预测活动类型
import os import csv import random import numpy as np import pandas as pd from sklearn.cross_validation import cross_val_score os.chdir('E:\\HumanActivity') #加载数据 data=pd.read_csv('test.csv',sep=',') print(data.ix[:5]) #决策树预测活动类型 from sklearn.tree import DecisionTreeClassifier clf = DecisionTreeClassifier(random_state=14) x_previous = data[['timestamp','x','y','z']].values y_true = data['act_num'].values scores = cross_val_score(clf,x_previous,y_true,scoring='accuracy') print("决策树预测准确率: {0:1f}%".format(np.mean(scores) * 100))
预测结果:
决策树预测准确率: 40.238367%
随机森林算法:
from sklearn.ensemble import RandomForestClassifier clf = RandomForestClassifier(random_state=14) x_test = data[['timestamp','x','y','z']].values scores = cross_val_score(clf,x_test,y_true,scoring='accuracy') print("随机森林预测准确率: {0:1f}%".format(np.mean(scores) * 100))
预测结果:
随机森林预测准确率: 46.861539%