基于SVM的齿轮箱轴承故障诊断python代码
时间: 2023-12-29 11:02:47 浏览: 128
基于SVM的齿轮箱轴承故障诊断(含matlab程序).docx
5星 · 资源好评率100%
以下是基于SVM的齿轮箱轴承故障诊断的Python代码:
```python
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
# 读取数据
data = pd.read_csv('gearbox.csv')
# 将数据分为特征和标签
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# 将标签中的字符串转换为数字
y = y.map({'normal': 0, 'fault': 1})
# 将数据分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建SVM模型
model = SVC(kernel='sigmoid')
# 训练模型
model.fit(X_train, y_train)
# 预测测试集
y_pred = model.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
# 输出准确率
print('Accuracy:', accuracy)
```
阅读全文