Sklearn 根据现有身高和体重训练模型,再对测试集做出判断

根据现有身高和体重训练模型,再对测试集做出判断

一.根据现有数据创建标签
[python] view plain copy
  1. #对数据集进行预处理  
  2. import random  
  3.   
  4. def calc_bmi(h, w):  
  5.     bmi = w / (h/100) ** 2  
  6.     if bmi < 18.5return "thin"  
  7.     if bmi < 25.0return "normal"  
  8.     return "fat"  
  9. #bim值小于18.5是瘦,小于25.0是普通,否侧是胖  
  10.   
  11. fp = open("./data/bmi.csv","w",encoding="utf-8")  
  12. fp.write("height,weight,label\r\n")  
  13.   
  14. cnt = {"thin":0"normal":0"fat":0}  
  15. for i in range(20000):  
  16.     h = random.randint(120,200)  
  17.     w = random.randint(3580)  
  18.     label = calc_bmi(h, w)  
  19.     cnt[label] += 1  
  20.     fp.write("{0},{1},{2}\r\n".format(h, w, label))  
  21. fp.close()  
  22. print("ok,", cnt)  

二.数据信息可视化绘图
[python] view plain copy
  1. #绘制三种不同类型的数据分布  
  2. import matplotlib.pyplot as plt  
  3. import pandas as pd  
  4.   
  5. tbl = pd.read_csv("./data/bmi.csv", index_col=2)  
  6. #读取数据  
  7.   
  8. fig = plt.figure()  
  9. ax = fig.add_subplot(111)  
  10. #三种子图重叠  
  11.   
  12. def scatter(lbl, color):  
  13.     b = tbl.loc[lbl]  
  14.     ax.scatter(b["weight"],b["height"], c=color, label=lbl)  
  15.   
  16. scatter("fat",    "red")  
  17. scatter("normal""yellow")  
  18. scatter("thin",   "purple")  
  19. #设置不同属性颜色  
  20.   
  21. ax.legend()   
  22. plt.show()  
  23. #显示图像  

运行结果


三.运用sklearn中的SVM的SVC训练数据并预测结果
[python] view plain copy
  1. #用sklearn的SVC方法来训练数据集,并交叉验证预测精度  
  2. from sklearn import cross_validation, svm, metrics  
  3. import matplotlib.pyplot as plt  
  4. import pandas as pd  
  5.   
  6. tbl = pd.read_csv("./data/bmi.csv")  
  7. #读取数据  
  8.   
  9. label = tbl["label"]  
  10. #读取数据中的标签列  
  11. w = tbl["weight"] / 100   
  12. h = tbl["height"] / 200   
  13. wh = pd.concat([w, h], axis=1)  
  14.   
  15. data_train, data_test, label_train, label_test = cross_validation.train_test_split(wh, label)  
  16. #将数据分成两组数据集和测试集  
  17.   
  18. clf = svm.SVC()  
  19. clf.fit(data_train, label_train)  
  20. #训练数据  
  21.   
  22. predict = clf.predict(data_test)  
  23. #预测数据  
  24.   
  25. ac_score = metrics.accuracy_score(label_test, predict)  
  26. #生成测试精度  
  27. cl_report = metrics.classification_report(label_test, predict)  
  28. #生成交叉验证的报告  
  29. print(ac_score)  
  30. #显示数据精度  
  31. print(cl_report)  
  32. #显示交叉验证数据集报告  

运行结果

交叉验证三组测试集平均预测精度为0.99

四.运用sklearn中的SVM的linearSVC训练数据并预测结果
[python] view plain copy
  1. #用sklearn的LinearSVC方法来训练数据集,并交叉验证预测精度  
  2. from sklearn import cross_validation, svm, metrics  
  3. import matplotlib.pyplot as plt  
  4. import pandas as pd  
  5.   
  6. tbl = pd.read_csv("./data/bmi.csv")  
  7. #读取数据  
  8.   
  9. label = tbl["label"]  
  10. #读取数据中的标签列  
  11. w = tbl["weight"] / 100   
  12. h = tbl["height"] / 200   
  13. wh = pd.concat([w, h], axis=1)  
  14.   
  15. data_train, data_test, label_train, label_test = cross_validation.train_test_split(wh,label)  
  16. #将数据分成两组数据集和测试集  
  17.   
  18. clf = svm.LinearSVC()  
  19. clf.fit(data_train, label_train)  
  20. #训练数据  
  21.   
  22. predict = clf.predict(data_test)  
  23. #预测数据  
  24.   
  25. ac_score = metrics.accuracy_score(label_test, predict)  
  26. #生成测试精度  
  27. cl_report = metrics.classification_report(label_test, predict)  
  28. #生成交叉验证的报告  
  29. print(ac_score)  
  30. #显示数据精度  
  31. print(cl_report)  
  32. #显示交叉验证数据集报告  

运行结果

交叉验证三组测试集平均预测精度为0.9182

猜你喜欢

转载自blog.csdn.net/sinat_23338865/article/details/80291326