写一个用SVM算法实现鸢尾花识别的代码
时间: 2023-06-13 15:08:07 浏览: 93
SVM鸢尾花分类Python实现
好的,以下是用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, y = iris.data, iris.target
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建模型并训练
model = SVC(kernel='linear')
model.fit(X_train, y_train)
# 预测并评估模型
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
运行后,会输出模型在测试集上的准确率。该代码实现了一个线性核SVM分类器,可以通过修改`kernel`参数来使用其他核函数。
阅读全文