使用Matplotlib绘制SVM? [英] Plot SVM with Matplotlib?
问题描述
我有一些有趣的用户数据.它提供了有关要求用户执行某些任务的及时性的信息.我正在尝试找出late
(这是告诉我用户是否按时(0
),晚点(1
)还是晚点(2
))是可预测/可解释的.我从提供交通信号灯信息的列中生成late
(绿色=不晚,红色=超晚).
I have some interesting user data. It gives some information on the timeliness of certain tasks the users were asked to perform. I am trying to find out, if late
- which tells me if users are on time (0
), a little late (1
), or quite late (2
) - is predictable/explainable. I generate late
from a column giving traffic light information (green = not late, red = super late).
这是我的工作:
#imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn import svm
import sklearn.metrics as sm
#load user data
df = pd.read_csv('April.csv', error_bad_lines=False, encoding='iso8859_15', delimiter=';')
#convert objects to datetime data types
cols = ['Planned Start', 'Actual Start', 'Planned End', 'Actual End']
df = df[cols].apply(
pd.to_datetime, dayfirst=True, errors='ignore'
).join(df.drop(cols, 1))
#convert datetime to numeric data types
cols = ['Planned Start', 'Actual Start', 'Planned End', 'Actual End']
df = df[cols].apply(
pd.to_numeric, errors='ignore'
).join(df.drop(cols, 1))
#add likert scale for green, yellow and red traffic lights
df['late'] = 0
df.ix[df['End Time Traffic Light'].isin(['Yellow']), 'late'] = 1
df.ix[df['End Time Traffic Light'].isin(['Red']), 'late'] = 2
#Supervised Learning
#X and y arrays
# X = np.array(df.drop(['late'], axis=1))
X = df[['Planned Start', 'Actual Start', 'Planned End', 'Actual End', 'Measure Package', 'Measure' , 'Responsible User']].as_matrix()
y = np.array(df['late'])
#preprocessing the data
X = preprocessing.scale(X)
#Supper Vector Machine
clf = svm.SVC(decision_function_shape='ovo')
clf.fit(X, y)
print(clf.score(X, y))
我现在正在尝试了解如何绘制决策边界,我的目标是使用Actual End
和Planned End
绘制2向散点图.自然,我检查了文档(请参见例如此处).但是我不能把头缠住它.如何运作?
I am now trying to understand how to plot the decision boundaries.My goal is to plot a 2-way scatter with Actual End
and Planned End
. Naturally, I checked the documentation (see e.g. here). But I can't wrap my head around it. How does this work?
推荐答案
作为对未来的展望,如果您为尝试绘制的绘图代码提供可公开获得的数据集,通常可以获得更快(更好)的响应,因为我们没有"April.csv".您也可以省略"April.csv"的数据整理代码.话虽如此...
As a heads up for the future, you'll generally get faster (and better) responses if you provide a publicly available dataset with your attempted plotting code, since we don't have 'April.csv'. You can also leave out your data-wrangling code for 'April.csv'. With that said...
Sebastian Raschka创建了 mlxtend 软件包,该软件包具有出色的绘图功能.它在后台使用了matplotlib.
Sebastian Raschka created the mlxtend package, which has has a pretty awesome plotting function for doing this. It uses matplotlib under the hood.
import numpy as np
import pandas as pd
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)
这篇关于使用Matplotlib绘制SVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!