gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:08:10 24 4
gpt4 key购买 nike

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

错误:

Traceback (most recent call last):
File "classifier-plot.py", line 115, in <module>
Z = Z.reshape(xx.shape)
ValueError: cannot reshape array of size 260 into shape (150,1750)

代码:

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()

最佳答案

tutorial您正在关注 Z 是通过将分类器应用于一组生成的特征向量来计算的,这些特征向量生成为形成一个规则的 NxM 网格。这样剧情就流畅了。

当你更换

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

Z = predicted_labels

您将此常规网格替换为对您的数据集所做的预测。下一行因错误而失败,因为它无法将大小为 len(files) 的数组整形为 NxM 矩阵。没有理由 len(files) = NxM

您无法直接按照教程进行操作是有原因的。您的数据维度是 3000,因此您的决策边界将是 3000 维空间中的 2999 维超平面。这不容易形象化。

在教程中维度是 4,为了可视化它被减少到 2。减少数据维度的最佳方法取决于数据。在本教程中,我们只选择 4 维向量的前两个分量。

在许多情况下,另一种行之有效的方法是使用主成分分析来降低数据的维度。

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)

此类模型可用于二维可视化。你可以直接按照教程定义

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% 的方差,并且图片的红色部分包含的红色点比蓝色部分多得多。

关于python - 如何在python中的SVM sklearn数据中绘制决策边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43778380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com