【问题标题】:Machine Learning using Python: how do I use matplotlib to plot the SVM?使用 Python 进行机器学习:如何使用 matplotlib 绘制 SVM?
【发布时间】:2017-08-15 04:06:38
【问题描述】:

我是机器学习的新手(使用 python)。我参考了几本书,但我无法弄清楚如何使用 matplotlib 绘制 SVM。

请帮助我绘制以下内容:

from sklearn.svm import SVC
features_train=[[0,0],[1,1],[2,2]]
label_train=[1,2,3]
features_test=[[1,2],[2,1]]
clf=SVC()
clf.fit(x,y)
pred=clf.predict(features_test)
print(pred)

【问题讨论】:

  • 你尝试了什么,你面临的问题是什么?
  • 我试过这个.... plt.scatter(fearures_train,label_train)。当两个列表都是一维时,这工作正常。但是当我将 features_train 保持为 2D 时,它的抛出错误。基本上我只是想知道有没有办法只使用matplotlib来绘制它?

标签: python matplotlib machine-learning svm


【解决方案1】:

尝试下面的代码并阅读 cmets。它会帮助你作为一个tyro。有关更多详细信息,您可以查看此链接 mlExtend,这有助于更好地了解 SVM 拥有 matplotlib

import numpy as np
import pandas as pd # For more detail catch up the link below
from sklearn import svm
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt


# Create arbitrary dataset for example
df = pd.DataFrame({'Planned_End': np.random.uniform(low=-5, high=5, size=50),
               'Actual_End':  np.random.uniform(low=-1, high=1, size=50),
               'Late':        np.random.random_integers(low=0,  high=2, size=50)}
)

# Fit Support Vector Machine Classifier
X = df[['Planned_End', 'Actual_End']]
y = df['Late']

clf = svm.SVC(decision_function_shape='ovo')
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)

但是,如果您不知道 pandas,您可以关注此链接: http://pandas.pydata.org/ 用于文档。希望有效

【讨论】:

  • 谢谢。我不知道这个“mlxtend”模块。它很有趣。但是我们可以在不使用 mlxtend 并且只使用 matplotlib 的情况下进行绘图吗?
  • 查看这个链接:gist.github.com/glamp/4365631 虽然它不会像上面的答案那么灵活,但试试看
  • @RishiAnand 这个答案有帮助吗?
  • 您能在答案中添加示例图片吗?
  • Sir(@Martin Thoma) 我嵌入了一个链接,可以通过许多示例图像更好地了解情况(感谢 Raschka、Sebastian)。
猜你喜欢
  • 2020-12-10
  • 2018-01-27
  • 2014-04-29
  • 2020-04-24
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
相关资源
最近更新 更多