gpt4 book ai didi

python - 支持向量机: Python Error Message

转载 作者:行者123 更新时间:2023-11-30 08:58:01 25 4
gpt4 key购买 nike

我对机器学习比较陌生,我决定深入研究一些理论,然后用一些代码进行练习。在此过程中,我收到了许多错误消息,我设法修复了这些错误消息,但我对这一消息感到困惑。我对 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)

但我不断收到以下错误:

Traceback (most recent call last): File "C:\Users...\Support Vector Machine (from scratch).py", line 134, in x2=[ w[0],w[1],-w[1],w[0] ] NameError: name 'w' is not defined

希望有更懂行的人帮忙。谢谢。

最佳答案

首先,您在方法 svm_sgd_plot 中定义了 w,但该方法在您显式调用它执行某些操作之前不会执行任何操作。

您可以通过添加行 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()

enter image description here

关于python - 支持向量机: Python Error Message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51921193/

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