非线性决策边界的支持向量机图

2024-09-28 23:05:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制支持向量机的决策边界,将癌症和非癌症区分开来。然而,它展示的情节与我想要的相去甚远。我希望它看起来像这样:

enter image description here 或者任何显示点分散的东西。我的代码是:

import numpy as np
import pandas as pd
from sklearn import svm
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt

autism = pd.read_csv('predictions.csv')


# Fit Support Vector Machine Classifier
X = autism[['TARGET','Predictions']]
y = autism['Predictions']

clf = svm.SVC(C=1.0, kernel='rbf', gamma=0.8)
clf.fit(X.values, y.values) 

# Plot Decision Region using mlxtend's awesome plotting function
plot_decision_regions(X=X.values, 
                      y=y.values,
                      clf=clf, 
                      legend=2)

# Update plot object with X/Y axis labels and Figure Title
plt.xlabel(X.columns[0], size=14)
plt.ylabel(X.columns[1], size=14)
plt.title('SVM Decision Region Boundary', size=16)
plt.show()

但我有一个奇怪的情节:

enter image description here

您可以在这里找到csv文件predictions.csv


Tags: csvfromimportsizeplotaspltpd
1条回答
网友
1楼 · 发布于 2024-09-28 23:05:35

你听起来有点困惑。。。在

您的predictions.csv看起来像:

TARGET  Predictions
     1  0
     0  0
     0  0
     0  0

而且,正如我猜列名所暗示的那样,它包含了基本真理(TARGET)和一些(?)的Predictions模型已经运行。在

鉴于此,您在发布的代码中所做的工作完全没有意义:您将这两个列作为您的X中的功能来预测您的y,这是。。。正好是这些列中的一个(Predictions),已经包含在您的X中。。。在

你的图看起来很“奇怪”,因为你绘制的是不是你的数据点,你在这里显示的X和{}数据是适合你的分类器的数据。在

我更困惑的是,在你的链接回购中,你的脚本中确实有正确的程序:

^{pr2}$

也就是说,从10-features-uns.csv中读取你的特性和标签,当然,不是来自predictions.csv,就像你在这里莫名其妙地试图做的那样。。。在

相关问题 更多 >