import numpy as npimport pandas as pdfrom sklearn.model_selection import train_test_splitfrom sklearn.svm import SVCfrom sklearn.metrics import accuracy_score, confusion_matriximport matplotlib.pyplot as pltimport xlrd# 加载数据集并进行预处理def load_data(filename): data = pd.read_excel(filename) data.dropna(inplace=True) X = data.drop('label', axis=1) X = (X - X.mean()) / X.std() y = data['label'] return X, y# 训练SVM分类器def train_svm(X_train, y_train, kernel='rbf', C=1, gamma=0.1): clf = SVC(kernel=kernel, C=C, gamma=gamma) clf.fit(X_train, y_train) return clf# 预测新的excel文件并输出预测结果excel、精度和混淆矩阵图def predict_svm(clf, X_test, y_test, filename): y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) cm = confusion_matrix(y_test, y_pred) # 输出预测结果excel data = pd.read_excel(filename) data['predicted_label'] = pd.Series(y_pred, index=data.index) data.to_excel('predicted_result.xlsx', index=False) # 绘制混淆矩阵图 plt.imshow(cm, cmap=plt.cm.Blues) plt.title('Confusion matrix') plt.colorbar() tick_marks = np.arange(len(set(y_test))) plt.xticks(tick_marks, sorted(set(y_test)), rotation=45) plt.yticks(tick_marks, sorted(set(y_test))) plt.xlabel('Predicted Label') plt.ylabel('True Label') plt.show() return accuracy# 加载数据集并划分训练集和验证集data = pd.read_excel('data.xlsx')data.dropna(inplace=True)X = data.drop('label', axis=1)X = (X - X.mean()) / X.std()y = data['label']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# 训练SVM分类器clf = train_svm(X_train, y_train)# 预测新的excel文件accuracy = predict_svm(clf, X_test, y_test, 'test_data.xlsx')# 输出精度print('Accuracy:', accuracy)改进,预测新的结果输出在新表中
时间: 2024-02-18 18:05:18 浏览: 205
numpy-1.22.4+vanilla-cp311-cp311-win-amd64-whl
您好,根据您的要求,我稍作修改,将输出预测结果的excel文件名改为新的文件名,并在新的文件中保存预测结果。修改后的代码如下:
```
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import xlrd
# 加载数据集并进行预处理
def load_data(filename):
data = pd.read_excel(filename)
data.dropna(inplace=True)
X = data.drop('label', axis=1)
X = (X - X.mean()) / X.std()
y = data['label']
return X, y
# 训练SVM分类器
def train_svm(X_train, y_train, kernel='rbf', C=1, gamma=0.1):
clf = SVC(kernel=kernel, C=C, gamma=gamma)
clf.fit(X_train, y_train)
return clf
# 预测新的excel文件并输出预测结果excel、精度和混淆矩阵图
def predict_svm(clf, X_test, y_test, filename, result_file):
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
# 输出预测结果excel
data = pd.read_excel(filename)
data['predicted_label'] = pd.Series(y_pred, index=data.index)
data.to_excel(result_file, index=False)
# 绘制混淆矩阵图
plt.imshow(cm, cmap=plt.cm.Blues)
plt.title('Confusion matrix')
plt.colorbar()
tick_marks = np.arange(len(set(y_test)))
plt.xticks(tick_marks, sorted(set(y_test)), rotation=45)
plt.yticks(tick_marks, sorted(set(y_test)))
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()
return accuracy
# 加载数据集并划分训练集和验证集
data = pd.read_excel('data.xlsx')
data.dropna(inplace=True)
X = data.drop('label', axis=1)
X = (X - X.mean()) / X.std()
y = data['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练SVM分类器
clf = train_svm(X_train, y_train)
# 预测新的excel文件
accuracy = predict_svm(clf, X_test, y_test, 'test_data.xlsx', 'predicted_result.xlsx')
# 输出精度
print('Accuracy:', accuracy)
```
在这个修改后的代码中,`predict_svm` 函数中新增了一个参数 `result_file`,用于指定预测结果输出的文件名。在预测结果输出时,将预测结果保存为新的excel文件,文件名即为 `result_file` 参数所指定的文件名。
阅读全文