加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
train_skin.py 8.60 KB
一键复制 编辑 原始数据 按行查看 历史
uyk 提交于 2022-04-23 20:59 . 1
# 如果运行报错:ValueError: The number of classes has to be greater than one; got 1 class
# 直接多运行几次或者多等待一会
# 数据读取
import pandas as pd
import numpy as np # 注意表头的位置
import json
<<<<<<< HEAD
import os # 清除残余文件
if os.path.exists('model_skin_skra.pkl'):
os.remove('model_skin_skra.pkl')
=======
import os #清除残余文件
import pickle
#清除文件
if os.path.exists('model/skin_svm.pkl'):
os.remove('model/skin_svm.pkl')
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
if os.path.exists('model/skin_knn.pkl'):
os.remove('model/skin_knn.pkl')
<<<<<<< HEAD
# 初始化全局变量
svm_a = 0
svm_p = 0
knn_a = 0
knn_p = 0
rand_a = 0
rand_p = 0
adbost_a = 0
adbost_p = 0
res_dict = {"svm_a": svm_a,
"svm_p": svm_p,
"knn_a": knn_a,
"knn_p": knn_p,
"rand_a": rand_a,
"rand_p": rand_p,
"adbost_a": adbost_a,
"adbost_p": adbost_p}
=======
if os.path.exists('model/skin_rand.pkl'):
os.remove('model/skin_rand.pkl')
if os.path.exists('model/skin_adbost.pkl'):
os.remove('model/skin_adbost.pkl')
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
logname = 'train_report/log_skin.txt'
with open(logname, 'w', encoding='utf-8', newline='') as f:
print('********************运行日志**************************', file=f)
csv = pd.read_csv('feature.csv', header=0) # 筛选特征
data_x = csv[[
"sc_mean",
"sc_median",
"sc_std",
"sc_min",
"sc_max",
"sc_range",
"sc_min_ratio",
"sc_max_ratio",
"sc1diff_mean",
"sc1diff_median",
"sc1diff_std",
"sc1diff_min",
"sc1diff_max",
"sc1diff_range",
"sc1diff_min_ratio",
"sc1diff_max_ratio",
"sc1adiff_mean",
"sc1gdiff_mean",
"sc2diff_mean",
"sc2diff_median",
"sc2diff_std",
"sc2diff_min",
"sc2diff_max",
"sc2diff_range",
"sc2diff_min_ratio",
"sc2diff_max_ratio",
"sc2adiff_mean",
"sc2gdiff_mean"]]
data_y = np.array(csv[['label']])[:, 0] # 降维
# 注: label 里的值至少有两个不同的, 否则向量机报错
print('***********************读取文件成功,分类开始*************************\n', file=f)
print('数据集划分:\n', file=f)
# 数据集划分
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size=0.1) # 验证样本比例
print('训练数据条目: %d 训练标签条目:%d 验证数据条目:%d 验证标签条目:%d\n' % (len(x_train), len(y_train), len(x_test), len(y_test)), file=f)
from sklearn.preprocessing import StandardScaler # 数据标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
print('数据标准化完成!\n', file=f)
from sklearn import svm # 支持向量机
clf = svm.SVC()
clf.fit(x_train, y_train)
#保存网络模型
with open('model/skin_svm.pkl', 'wb') as fw:
pickle.dump(clf, fw)
print('skin_svm.pkl 模型保存成功! \n',file=f)
# #读取测试
# with open('model/skin_svm.pkl', 'rb') as fr:
# new_svm = pickle.load(fr)
# print (new_svm.predict(x_test))
print('----------------对比预测结果-----------------', file=f)
<<<<<<< HEAD
# 写入字典
res_dict["svm_p"] = list(clf.predict(x_test))
print(y_test == clf.predict(x_test), file=f) # 直接对比预测结果
print('-------------------------------------------', file=f)
accuracy = clf.score(x_test, y_test) * 100 # 准确率计算
res_dict["svm_a"] = accuracy
=======
print('----------------对比预测结果-----------------',file=f)
#写入字典
print(y_test == clf.predict(x_test),file=f) # 直接对比预测结果
print('-------------------------------------------',file=f)
accuracy = clf.score(x_test, y_test) * 100 # 准确率计算
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
print('准确率为:%f\n' % (accuracy), file=f)
print('*********************支持向量机分类结束***********************\n', file=f)
print('\n', file=f)
print('***********************开始KNN分类**************************\n', file=f)
from sklearn.neighbors import KNeighborsClassifier # KNN算法 #分类蔟
classifier = KNeighborsClassifier(n_neighbors=2, metric='minkowski', p=2)
classifier.fit(x_train, y_train)
#保存网络模型
with open('model/skin_knn.pkl', 'wb') as fw:
pickle.dump(classifier, fw)
<<<<<<< HEAD
=======
print('skin_knn.pkl 模型保存成功! \n',file=f)
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
y_pred = classifier.predict(x_test) # Predicting the Test set results
accuracy = classifier.score(x_test, y_test)
print('---------------------生成KNN混肴矩阵-------------------\n', file=f)
from sklearn.metrics import confusion_matrix # Making the Confusion Matrix
from sklearn.metrics import classification_report
cm = confusion_matrix(y_test, y_pred)
print(cm, file=f)
print(classification_report(y_test, y_pred), file=f)
print('---------------------生成完毕-----------------------\n', file=f)
print('*********************KNN分类结束**********************************', file=f)
print('\n', file=f)
print('\n', file=f)
print('***********************开始随机森林分类**************************\n', file=f)
from sklearn.ensemble import RandomForestClassifier # Fitting Random Forest to the Training set
classifier = RandomForestClassifier(n_estimators=5, criterion='entropy', random_state=0) # 随机选取点
classifier.fit(x_train, y_train)
#保存网络模型
with open('model/skin_rand.pkl', 'wb') as fw:
pickle.dump(classifier, fw)
<<<<<<< HEAD
=======
print('skin_rand.pkl 模型保存成功! \n',file=f)
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
y_pred = classifier.predict(x_test) # Predicting the Test set results
accuracy = classifier.score(x_test, y_test)
print('预测验证数据集:', file=f)
print(y_pred, file=f)
print('\n', file=f)
print('---------------------生成随机森林混肴矩阵-------------------\n', file=f)
from sklearn.metrics import confusion_matrix # Making the Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
print(cm, file=f)
print(classification_report(y_test, y_pred), file=f)
print('---------------------生成完毕-----------------------\n', file=f)
print('*********************随机森林分类结束**********************************', file=f)
print('\n', file=f)
print('\n', file=f)
<<<<<<< HEAD
print('***********************开始AdaBoost分类**************************\n', file=f)
=======
print('\n',file=f)
print('\n',file=f)
print('***********************开始AdaBoost分类**************************\n',file=f)
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
from sklearn.ensemble import AdaBoostClassifier
from sklearn import metrics
# 定义模型 #迭代次数 学习率
model = AdaBoostClassifier(n_estimators=50, learning_rate=1.5)
# 训练
model.fit(x_train, y_train)
#保存网络模型
with open('model/skin_adbost.pkl', 'wb') as fw:
pickle.dump(model, fw)
print('skin_adbost.pkl 模型保存成功! \n',file=f)
# 预测
y_pred = model.predict(x_test)
<<<<<<< HEAD
print('预测验证数据集:', file=f)
print(y_pred, file=f)
print('\n', file=f)
acc = metrics.accuracy_score(y_test, y_pred) # 准确率
res_dict["adbost_a"] = acc
=======
print('预测验证数据集:',file=f)
print(y_pred,file=f)
print('\n',file=f)
acc = metrics.accuracy_score(y_test, y_pred) # 准确率
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
print(f"准确率:{acc:.2}\n", file=f)
print('***********************AdaBoost分类结束**************************\n', file=f)
<<<<<<< HEAD
# 将结果写入文件
with open('model_skin_skra.pkl', 'a', encoding='utf-8') as f:
f.write(json.dumps(res_dict))
#从文件读取结果 (测试)
# print(type(get_dict()))
=======
print('皮电数据训练结束\n')
>>>>>>> 8a5a0887874285680f0779573ce0c9bb0aa10a0c
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化