python - SVC 分类器支持 python Sklearn 中的向量类

标签 python machine-learning scikit-learn svm

我如何找出哪些支持向量属于 sklearn SVC 中的哪个类? ?

model = clf.fit(X,y) 

vectors = model.support_vectors_

哪个向量属于哪个决策边界?

最佳答案

您可以使用 SVC.support_属性。 support_ 属性为 SVC.support_vectors_ 中的每个支持向量提供训练数据索引。您可以按如下方式检索每个支持向量的类(给定您的示例):

X[model.support_]

一个更完整的例子:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_classification
from sklearn.svm import SVC

svc = SVC(kernel='linear', C=0.025)
X, y = make_classification(n_samples=500, n_features=2, n_redundant=0, n_informative=2, random_state=1, n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
X = StandardScaler().fit_transform(X)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=.4, random_state=42)
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
fig, ax = plt.subplots(figsize=(18,12))
ax.scatter(X_tr[:, 0], X_tr[:, 1], c=y_tr, cmap=cm_bright)
svc.fit(X_tr, y_tr)
y_tr[svc.support_]
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1])
fig2, ax2 = plt.subplots(figsize=(18,12))
ax2.scatter(X_tr[:, 0], X_tr[:, 1], c=y_tr, cmap=cm_bright)
ax2.scatter(svc.support_vectors_[:, 0], svc.support_vectors_[:, 1])    
fig3, ax3 = plt.subplots(figsize=(18,12))
ax3.scatter(X_tr[:, 0], X_tr[:, 1], c=y_tr, cmap=cm_bright)
ax3.scatter(svc.support_vectors_[:, 0], svc.support_vectors_[:, 1], c=y_tr[svc.support_], cmap=cm_bright)

enter image description here enter image description here enter image description here

关于python - SVC 分类器支持 python Sklearn 中的向量类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54790775/

相关文章:

python - Pandas df 将值从一列拆分为自己的列

machine-learning - Scikit Learn 从管道内的特征联合中提取特征名称

python-2.7 - 文本聚类和主题提取

python - 如何在 div 另一个标签中提取 span 标签

python - python 到 exe 转换后 botocore.exceptions.DataNotFoundError

python - sklearn 逻辑回归的特点

python - scipy.interpolate.lagrange 在某些数据上失败

python-3.x - XGBoost 训练 n_jobs = -1 不使用所有核心

scikit-learn - 当 p > n 时 sklearn 如何进行线性回归?

python - 如何在热图中将刻度和标签居中