svc看特征重要性代码
时间: 2023-11-12 17:04:55 浏览: 81
下面是使用SVC模型查看特征重要性的代码:
```python
from sklearn.datasets import load_iris
from sklearn.svm import SVC
import matplotlib.pyplot as plt
# 加载数据
iris = load_iris()
X = iris.data[:, :2] # 只取前两个特征
y = iris.target
# 训练SVC模型
model = SVC(kernel='linear', C=10)
model.fit(X, y)
# 查看特征重要性
coef = model.coef_[0]
print('Feature importance:', coef)
# 可视化特征重要性
plt.bar(range(len(coef)), coef)
plt.xticks(range(len(coef)), ['sepal length', 'sepal width'])
plt.xlabel('Feature')
plt.ylabel('Importance')
plt.show()
```
这里使用了鸢尾花数据集,只取前两个特征进行训练。然后调用`model.coef_`查看特征重要性,使用Matplotlib进行可视化展示。