我对机器学习还比较陌生,因此决定深入研究一些理论,然后使用一些代码进行练习。在此过程中,我收到了很多错误消息,这些错误消息都可以修复,但我对此一无所知。我也是Python的新手,所以我确定这是一些与语法相关的问题,但这次我无法确定(Python 2.7.15)。这是完整的代码:
import numpy as np
from matplotlib import pyplot as plt
# Next we input our data of the for [X, Y, Bias] in a matrix using the Numpy array method:
X = np.array([
[-2, 4,-1],
[2, -2, -1],
[2, 4, -1],
[8,-4, -1],
[9, 4, -1],
])
# Let's make another variable Y that contains the output labels for each element in the matrix:
Y = np.array([-1,-1,1,1,1])
#Now let's plot our data. We're going to use a For Loop for this:
for index,element in enumerate(X):
if index<2:
plt.scatter(element[0],element[1], marker="_", s=120, color="r")
else:
plt.scatter(element[0],element[1], marker="+", s=120, color="b")
plt.plot([-2,8], [8,0.5])
plt.show()
def svm_sgd_plot(X, Y):
#Initialize our SVMs weight vector with zeros (3 values)
w = np.zeros(len(X[0]))
#The learning rate
eta = 1
#how many iterations to train for
epochs = 100000
#store misclassifications so we can plot how they change over time
errors = []
#training part & gradient descent part
for epoch in range(1,epochs):
error = 0
for i, x in enumerate(X):
#misclassification
if (Y[i]*np.dot(X[i], w)) < 1:
#misclassified update for ours weights
w = w + eta * ( (X[i] * Y[i]) + (-2 * (1/epoch) * w) )
error = 1
else:
#correct classification, update our weights
w = w + eta * (-2 * (1/epoch) * w)
errors.append(error)
# lets plot the rate of classification errors during training for our SVM
plt.plot(errors, '|')
plt.ylim(0.5,1.5)
plt.axes().set_yticklabels([])
plt.xlabel('Epoch')
plt.ylabel('Misclassified')
plt.show()
return w
for d, sample in enumerate(X):
# Plot the negative samples
if d < 2:
plt.scatter(sample[0], sample[1], s=120, marker='_', linewidths=2)
# Plot the positive samples
else:
plt.scatter(sample[0], sample[1], s=120, marker='+', linewidths=2)
# Add our test samples
plt.scatter(2,2, s=120, marker='_', linewidths=2, color='yellow')
plt.scatter(4,3, s=120, marker='+', linewidths=2, color='blue')
plt.show()
# Print the hyperplane calculated by svm_sgd()
x2=[ w[0],w[1],-w[1],w[0] ]
x3=[ w[0],w[1],w[1],-w[0] ]
x2x3 = np.array([x2,x3])
X,Y,U,V = zip(*x2x3)
ax = plt.gca()
ax.quiver(X,Y,U,V,scale=1, color='blue')
w = svm_sgd_plot(X,Y)
但是我不断收到以下错误:
回溯(最近一次拨打电话):文件“ C:\ Users ... \ Support Vector 机器(从头开始).py”,第134行,在 x2 = [w [0],w [1],-w [1],w [0]] NameError:名称'w'未定义
我希望知识渊博的人会有所帮助。谢谢。
答案 0 :(得分:2)
首先,您在方法w
中定义了svm_sgd_plot
,但是在您显式调用该方法以执行某项操作之前,该方法没有执行任何操作。
例如,在绘制测试数据后,您可以通过添加行w = svm_sgd_plot(X,Y)
来调用它,从而使代码成为
#PLOT TRAINING DATA
for d, sample in enumerate(X):
# Plot the negative samples
if d < 2:
plt.scatter(sample[0], sample[1], s=120, marker='_', linewidths=2)
# Plot the positive samples
else:
plt.scatter(sample[0], sample[1], s=120, marker='+', linewidths=2)
#PLOT TESTING DATA
# Add our test samples
plt.scatter(2,2, s=120, marker='_', linewidths=2, color='yellow')
plt.scatter(4,3, s=120, marker='+', linewidths=2, color='blue')
plt.show()
#CALL YOUR METHOD
w = svm_sgd_plot(X,Y)
然后,您只需要可视化您的方法提供的分类即可。我添加了您的两个测试数据观察值,以便您可以看到SVM方法如何将它们正确分类。请注意,黄点和蓝点由SVM方法生成的线隔开。
# Print the hyperplane calculated by svm_sgd()
x2=[ w[0],w[1],-w[1],w[0] ]
x3=[ w[0],w[1],w[1],-w[0] ]
x2x3 = np.array([x2,x3])
X,Y,U,V = zip(*x2x3)
ax = plt.gca()
ax.quiver(X,Y,U,V,scale=1, color='blue')
#I ADDED THE FOLLOWING THREE LINES SO THAT YOU CAN SEE HOW YOU TESTING DATA IS BEING CLASSIFIED BY YOUR SVM METHOD
plt.scatter(2,2, s=120, marker='_', linewidths=2, color='yellow')
plt.scatter(4,3, s=120, marker='+', linewidths=2, color='blue')
plt.show()