采用 scikit-learn 中的线性 SVM 对 iris 数据集进行二分类,通过散点图可视化数据样本(之前选择的两个特征),并画出决策边界和 2 个最大间隔边界,标出支持向量
时间: 2024-02-11 20:03:43 浏览: 30
好的,我可以为您提供一个 Python 代码示例,使用 scikit-learn 中的线性 SVM 对 iris 数据集进行二分类,并通过散点图可视化数据样本,并画出决策边界和 2 个最大间隔边界,标出支持向量。
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
# 加载 iris 数据集
iris = load_iris()
X = iris.data[:, :2]
y = iris.target
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练 LinearSVC 模型
clf = LinearSVC(random_state=42)
clf.fit(X_train, y_train)
# 可视化数据样本
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
# 画出决策边界和最大间隔边界,并标出支持向量
w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(X[:, 0].min(), X[:, 0].max())
yy = a * xx - (clf.intercept_[0]) / w[1]
margin = 1 / np.sqrt(np.sum(clf.coef_ ** 2))
yy_down = yy - np.sqrt(1 + a ** 2) * margin
yy_up = yy + np.sqrt(1 + a ** 2) * margin
plt.plot(xx, yy, 'k-', label='decision boundary')
plt.plot(xx, yy_down, 'k--', label='margin')
plt.plot(xx, yy_up, 'k--', label='margin')
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=200, facecolors='none', edgecolors='k', label='support vector')
plt.legend()
plt.show()
```
运行代码后,您将得到一个包含决策边界和最大间隔边界的散点图,并标出支持向量。希望这个示例能够帮助您理解如何使用线性 SVM 对 iris 数据集进行二分类,并可视化数据样本。