svm支持向量机代码
时间: 2023-10-31 20:07:32 浏览: 100
svm支持向量机
3星 · 编辑精心推荐
以下是一个简单的 SVM 支持向量机的 Python 代码示例:
```python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# 加载数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 训练 SVM 模型
svm = SVC(kernel='linear', C=1.0, random_state=42)
svm.fit(X_train, y_train)
# 预测测试集结果
y_pred = svm.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
阅读全文