9. 监督学习实例——运动状态预测
来源:互联网 发布:营销视频制作软件 编辑:程序博客网 时间:2024/06/17 16:24
- 代码
代码
"""可穿戴式设备的流行,让我们可以更便利地使用传感器获取人体的各项数据,甚至是生理数据。当传感器采集到大量数据后,我们就可以通过对数据进行分析和建模,通过各项特征的数值进行用户状态的判断,根据用户所处的状态提供给用户更加精准、便利的服务。我们现在收集了来自A,B,C,D,E 5位用户的可穿戴设备上的传感器数据,每位用户的数据集包含一个特征文件(a.label)和一个标签文件(a.feature)。特征文件中每一行对应一个时刻的所有传感器数值,标签文件中每行记录了和特征文件中对应时刻的标记过的用户姿态,两个文件的行数相同,相同行之间互相对应。"""import pandas as pdimport numpy as np# 导入预处理模块 Imputerfrom sklearn.preprocessing import Imputer# 导入自动生成训练集和测试集的模块 train_test_splitfrom sklearn.cross_validation import train_test_split# 导入预测结果评估模块 classification_reportfrom sklearn.metrics import classification_report# K近邻分类器from sklearn.neighbors import KNeighborsClassifier# 决策树分类器from sklearn.tree import DecisionTreeClassifier# 高斯朴素贝叶斯函数from sklearn.naive_bayes import GaussianNBdef load_dataset(feature_paths, label_paths): """ :param feature_paths: 特征文件的列表 :param label_paths: 标签文件的列表 :return: 特征文件列表和标签文件列表中的内容 """ feature = np.ndarray(shape=(0, 41)) label = np.ndarray(shape=(0, 1)) for file in feature_paths: # 使用逗号分隔符读取特征数据,将问号替换标记为缺省值,文件中不含表头 df = pd.read_table(file, delimiter=',', na_values='?', header=None) # 使用平均值补全缺失值,然后将数据进行补全 imp = Imputer(missing_values='NaN', strategy='mean', axis=0) imp.fit(df) df = imp.transform(df) # 将新读入的数据合并到特征集合中 feature = np.concatenate((feature, df)) for file in label_paths: # 读取标签数据,文件不包含表头 df = pd.read_table(file, header=None) # 将新读入的数据合并到标签集合中 label = np.concatenate((label, df)) # 将标签规整为一维向量 label = np.ravel(label) return feature, labelif __name__ == '__main__': ''' 数据路径 ''' featurePaths = ['Data/A/A.feature', 'Data/B/B.feature', 'Data/C/C.feature', 'Data/D/D.feature', 'Data/E/E.feature'] labelPaths = ['Data/A/A.label', 'Data/B/B.label', 'Data/C/C.label', 'Data/D/D.label', 'Data/E/E.label'] ''' 读入数据 ''' # 将前四个数据作为训练集读入 x_train, y_train = load_dataset(featurePaths[:4], labelPaths[:4]) # 将最后一个数据作为测试集读入 x_test, y_test = load_dataset(featurePaths[4:], labelPaths[4:]) # 使用全量数据作为训练集,借助train_test_split函数将训练数据打乱 # 通过设置测试比例test_size为0,将数据随机打乱,便于后续分类器的初始化和训练 x_train, x_, y_train, y_ = train_test_split(x_train, y_train, test_size=0.0) # knn print('Start training knn') knn = KNeighborsClassifier().fit(x_train, y_train) print('Training done') answer_knn = knn.predict(x_test) print('Prediction done') # DT print('Start training DT') dt = DecisionTreeClassifier().fit(x_train, y_train) print('Training done') answer_dt = dt.predict(x_test) print('Prediction done') # Bayes print('Start training Bayes') gnb = GaussianNB().fit(x_train, y_train) print('Training done') answer_gnb = gnb.predict(x_test) print('Prediction done') print('\n\nThe classification report for knn:') print(classification_report(y_test, answer_knn)) print('\n\nThe classification report for DT:') print(classification_report(y_test, answer_dt)) print('\n\nThe classification report for Bayes:') print(classification_report(y_test, answer_gnb))
阅读全文
0 0
- 9. 监督学习实例——运动状态预测
- 人体运动状态预测-实例分析
- Python机器学习应用 | 人体运动状态预测
- OpenCV学习——运动检测实例
- 机器学习——监督学习,半监督学习,无监督学习,主动学习
- 聚类(序)——监督学习与无监督学习
- 聚类——监督学习与无监督学习
- 聚类——监督学习与无监督学习
- 聚类(序)——监督学习与无监督学习
- 聚类(序)——监督学习与无监督学习
- 聚类(序)——监督学习与无监督学习
- 聚类(序)——监督学习与无监督学习
- 监督学习——决策树
- 数据挖掘——学习笔记(机器学习--监督,非监督,半监督学习
- 数据挖掘——学习笔记(机器学习--监督,非监督,半监督学习)
- 数据挖掘——学习笔记(机器学习--监督,非监督,半监督学习)
- 神经网络学习——有监督vs无监督
- Torch学习——有监督学习
- MySql的心得
- HDU-4109 Instrction Arrangement(差分约束系统+增加源点技巧)
- JS学习的几点思考(一)
- Linux驱动编写经验
- Linux之网络基础
- 9. 监督学习实例——运动状态预测
- 指针与数组
- servlet
- 某中学培训机构的宣传稿
- [JS] JS与Cookie
- 处理App状态改变的策略
- tomcat压缩包启动
- 共用一个数据库实现pc网站和手机网站的制作
- SQL COUNT 函数