mitbih数据集心电信号SVM分类代码
时间: 2023-07-05 10:07:12 浏览: 150
以下是使用SVM对MIT-BIH心电信号数据集进行分类的示例代码:
```python
import wfdb
import numpy as np
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# 加载数据集
signals, fields = wfdb.rdsamp('mitdb/100', channels=[0])
annotations = wfdb.rdann('mitdb/100', 'atr')
# 数据预处理
signals = signals.flatten()
labels = np.zeros(len(signals))
for idx, label in enumerate(annotations.symbol):
if label == 'N':
labels[annotations.sample[idx]:annotations.sample[idx+1]] = 0
elif label == 'V':
labels[annotations.sample[idx]:annotations.sample[idx+1]] = 1
# 将数据分成训练集和测试集
train_data = signals[:int(0.8*len(signals))]
train_labels = labels[:int(0.8*len(signals))]
test_data = signals[int(0.8*len(signals)):]
test_labels = labels[int(0.8*len(signals)):]
# SVM分类器
svm = SVC(kernel='linear')
# 训练模型
svm.fit(train_data.reshape(-1, 1), train_labels)
# 测试模型
predictions = svm.predict(test_data.reshape(-1, 1))
# 计算准确率
accuracy = accuracy_score(test_labels, predictions)
print("Accuracy:", accuracy)
```
该代码将MIT-BIH数据集中的ECG信号(通道0)和标签加载到numpy数组中。然后,它将标签从字符串转换为二进制格式(0表示正常心律,1表示室性心律)。接下来,数据被分成训练和测试集。SVM分类器被创建,训练和测试数据被传递给它。最后,准确率被计算并打印出来。
阅读全文