代码拉取完成,页面将自动刷新
# 如果运行报错:ValueError: The number of classes has to be greater than one; got 1 class
# 直接多运行几次或者多等待一会
# 数据读取
import pandas as pd
import numpy as np # 注意表头的位置
import json
import os # 清除残余文件
import pickle
# 清楚文件
if os.path.exists('model/ecg_svm.pkl'):
os.remove('model/ecg_svm.pkl')
if os.path.exists('model/ecg_knn.pkl'):
os.remove('model/ecg_knn.pkl')
if os.path.exists('model/ecg_rand.pkl'):
os.remove('model/ecg_rand.pkl')
if os.path.exists('model/ecg_adbost.pkl'):
os.remove('model/ecg_adbost.pkl')
logname = 'train_report/log_ecg.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[["bpm",
"ibi",
"sdnn",
"sdsd",
"rmssd",
"pnn20",
"pnn50",
"hr_mad",
"sd1",
"sd2",
"s",
"sd1/sd2",
"breathingrate"
]]
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/ecg_svm.pkl', 'wb') as fw:
pickle.dump(clf, fw)
print('ecg_svm.pkl 模型保存成功! \n', file=f)
# 读取测试
# with open('model/ecg_svm.pkl', 'rb') as fr:
# new_svm = pickle.load(fr)
# print (new_svm.predict(x_test))
print('----------------对比预测结果-----------------', file=f)
print('----------------对比预测结果-----------------', file=f)
# 写入字典
print(y_test == clf.predict(x_test), file=f) # 直接对比预测结果
print('-------------------------------------------', file=f)
accuracy = clf.score(x_test, y_test) * 100 # 准确率计算
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/ecg_knn.pkl', 'wb') as fw:
pickle.dump(classifier, fw)
print('ecg_knn.pkl 模型保存成功! \n', file=f)
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/ecg_rand.pkl', 'wb') as fw:
pickle.dump(classifier, fw)
print('ecg_rand.pkl 模型保存成功! \n', file=f)
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)
print('\n', file=f)
print('\n', file=f)
print('***********************开始AdaBoost分类**************************\n', file=f)
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/ecg_adbost.pkl', 'wb') as fw:
pickle.dump(model, fw)
print('ecg_adbost.pkl 模型保存成功! \n', file=f)
# 预测
y_pred = model.predict(x_test)
print('预测验证数据集:', file=f)
print(y_pred, file=f)
print('\n', file=f)
acc = metrics.accuracy_score(y_test, y_pred) # 准确率
print(f"准确率:{acc:.2}\n", file=f)
print('***********************AdaBoost分类结束**************************\n', file=f)
print('心电数据训练结束\n')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。