如何在python中绘制SVM sklearn数据中的决策边界?

时间:2017-05-04 09:02:31

标签: python matplotlib scikit-learn svm svc

我正在从训练集中读取电子邮件数据并创建train_matrix,train_labels和test_labels。现在如何在python中使用matplot显示决策边界。我正在使用sklearn的svm。通过虹膜提供给定数据集的在线示例。但是情节在自定义数据上失败。这是我的代码

错误:

import os
import numpy as np
from collections import Counter
from sklearn import svm
import matplotlib
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score


def make_Dictionary(root_dir):
    all_words = []
    emails = [os.path.join(root_dir,f) for f in os.listdir(root_dir)]
    for mail in emails:
        with open(mail) as m:
            for line in m:
                words = line.split()
                all_words += words
    dictionary = Counter(all_words)
    list_to_remove = dictionary.keys()

    for item in list_to_remove:
        if item.isalpha() == False:
            del dictionary[item]
        elif len(item) == 1:
            del dictionary[item]
    dictionary = dictionary.most_common(3000)

    return dictionary



def extract_features(mail_dir):
    files = [os.path.join(mail_dir,fi) for fi in os.listdir(mail_dir)]
    features_matrix = np.zeros((len(files),3000))
    train_labels = np.zeros(len(files))
    count = 0;
    docID = 0;
    for fil in files:
      with open(fil) as fi:
        for i,line in enumerate(fi):
          if i == 2:
            words = line.split()
            for word in words:
              wordID = 0
              for i,d in enumerate(dictionary):
                if d[0] == word:
                  wordID = i
                  features_matrix[docID,wordID] = words.count(word)
        train_labels[docID] = 0;
        filepathTokens = fil.split('/')
        lastToken = filepathTokens[len(filepathTokens) - 1]
        if lastToken.startswith("spmsg"):
            train_labels[docID] = 1;
            count = count + 1
        docID = docID + 1
    return features_matrix, train_labels



TRAIN_DIR = "../train-mails"
TEST_DIR = "../test-mails"

dictionary = make_Dictionary(TRAIN_DIR)

print "reading and processing emails from file."
features_matrix, labels = extract_features(TRAIN_DIR)
test_feature_matrix, test_labels = extract_features(TEST_DIR)


model = svm.SVC(kernel="rbf", C=10000)

print "Training model."
features_matrix = features_matrix[:len(features_matrix)/10]
labels = labels[:len(labels)/10]
#train model
model.fit(features_matrix, labels)

predicted_labels = model.predict(test_feature_matrix)

print "FINISHED classifying. accuracy score : "
print accuracy_score(test_labels, predicted_labels)







##----------------

h = .02  # step size in the mesh

# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0  # SVM regularization parameter
X = features_matrix
y = labels
svc = model.fit(X, y)
#svm.SVC(kernel='linear', C=C).fit(X, y)

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = y[:].min() - 1, y[:].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

# title for the plots
titles = ['SVC with linear kernel']



Z = predicted_labels#svc.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title(titles[0])

plt.show()

代码:

//make global
    Cursor c;  
    int id = 0;
    onCreate(){
       if(insertData("name")){
           if(id > 0){
              //do something
           }
       }
    }
    public boolean insertData(String name) {

        SQLiteDatabase db = this.getReadableDatabase();
        String[] cols = new String[]{COL_1, COL_2};

        c = db.query(TABLE_NAME, cols, COL_2 + "=?", new String[]{"" + name}, null, null, null);
        if (c.getCount() > 0) {
            return false;
        } else{
             if(c.moveToFirst()){
                id = Integer.parsInt(c.getString(c.getColumnIndex("id")))
             }
             return true;
        }
    }

1 个答案:

答案 0 :(得分:1)

通过将分类器应用于生成的一组特征向量以形成常规Z网格,计算您NxM跟随的tutorial。这使得情节顺利。

当您更换

Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])

Z = predicted_labels

您使用数据集上的预测替换了此常规网格。下一行失败并出现错误,因为它无法将大小为len(files)的数组重新整形为NxM矩阵。没有理由len(files) = NxM

您无法直接关注本教程。您的数据维度为3000,因此您的决策边界将是3000维空间中的2999维超平面。这不容易想象。

在教程中,维度为4,可视化时缩小为2。 减少数据维度的最佳方法取决于数据。在本教程中,我们只选择四维向量的前两个组件。

在许多情况下效果很好的另一个选项是使用主成分分析来减少数据的维度。

from sklearn.decomposition import PCA
pca = PCA(n_components = 2)
pca.fit(features_matrix, labels)
reduced_matrix = pca.fit_transform(features_matrix, labels)
model.fit(reduced_matrix, labels)

此类模型可用于2D可视化。您可以直接按照教程定义

Z = model.predict(np.c_[xx.ravel(), yy.ravel()])

一个完整但不是非常令人印象深刻的例子

我们无法访问您的电子邮件数据,因此,为了便于说明,我们可以使用随机数据。

from sklearn import svm
from sklearn.decomposition import PCA

# initialize algorithms and data with random
model = svm.SVC(gamma=0.001,C=100.0)
pca = PCA(n_components = 2)
rng = np.random.RandomState(0)
U = rng.rand(200, 2000)
v = (rng.rand(200)*2).astype('int')
pca.fit(U,v)
U2 = pca.fit_transform(U,v)
model.fit(U2,v)

# generate grid for plotting
h = 0.2
x_min, x_max = U2[:,0].min() - 1, U2[:, 0].max() + 1
y_min, y_max = U2[:,1].min() - 1, U2[:, 1].max() + 1
xx, yy = np.meshgrid(
    np.arange(x_min, x_max, h),
    np.arange(y_min, y_max, h))

# create decision boundary plot
Z = s.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
contourf(xx,yy,Z,cmap=plt.cm.coolwarm, alpha=0.8)
scatter(U2[:,0],U2[:,1],c=v)
show()

会产生一个看起来不太令人印象深刻的决定边界。

decision boundary obtain by reducing 2000 dimensions to 2

实际上,前两个主要组件只捕获数据中包含的信息的1%

>>> print(pca.explained_variance_ratio_) 
[ 0.00841935  0.00831764]

如果你现在只引入一些小心的伪装不对称,你就会看到效果。

修改数据以仅为每个要素随机选择的一个坐标引入移位

random_shifts = (rng.rand(2000)*200).astype('int')
for i in range(MM):
    if v[i] == 1:
        U[i,random_shifts[i]] += 5.0

使用PCA可以获得更多信息。

decision boundary obtain by reducing 2000 dimensions to 2 after positive instances were randomly shifted

请注意,这里前两个主要成分已经解释了约5%的方差,而图片的红色部分包含的红点比蓝色要多得多。