使用Matplotlib绘制SVM?

Rac*_*hel 3 plot python-3.x scikit-learn supervised-learning

我有一些有趣的用户数据。它提供了有关要求用户执行某些任务的及时性的一些信息。我正在尝试确定-是否可以预测/解释late-告诉我用户是否准时(0),稍晚(1)或相当晚(2)。我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))
Run Code Online (Sandbox Code Playgroud)

我现在想了解如何画出决定boundaries.My目标是绘制了2路散射Actual EndPlanned End。自然,我检查了文档(请参见例如此处)。但是我不能把头缠住它。这是如何运作的?

Max*_*wer 9

展望未来,如果您为尝试绘制的代码提供公开可用的数据集,则通常会得到更快(更好)的响应,因为我们没有“ April.csv”。您也可以省略“ April.csv”的数据整理代码。照这样说...

Sebastian Raschka创建了mlxtend软件包,该软件包具有出色的绘图功能。它在后台使用matplotlib。

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明