提问人:Derrick Walker 提问时间:11/9/2023 最后编辑:Nick ODellDerrick Walker 更新时间:11/9/2023 访问量:52
AttributeError:“list”对象没有属性“ravel”
AttributeError: 'list' object has no attribute 'ravel'
问:
以下块继续返回相同的错误,我不明白为什么......有什么帮助吗?我已将 numpy 导入为 np 并定义了 xx1 和 xx2,但代码仍然返回错误。是什么原因导致了这个问题?
def plot_decision_regions(X,y,classifier,resolution=0.02):
markers = ('s','x','o','^','v')
colors = ('red','blue','lightgreen','gray','cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])
x1_min = X[:,0].min() -1
x1_max = X[:,0].max() +1
x2_min = X[:,1].min() -1
x2_max = X[:,1].max() +1
xx1 = np.meshgrid(np.arange(x1_min,x1_max,resolution))
xx2 = np.meshgrid(np.arange(x2_min,x2_max,resolution))
Z = classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1,xx2,Z,alpha=0.3,cmap=cmap)
plt.xlim(xx1.min(),xx1.max())
plt.ylim(xx2.min(),xx2.max())
for idx, cl in enumerate(np.unique(y)):
plt.scatter(x=X[y==cl,0],y=X[y==cl,1],alpha=0.8,c=colors[idx],marker=markers[idx],label=cl,edgecolor='black')
>>> plot_decision_regions(X,y,classifier=ppn)
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
plot_decision_regions(X,y,classifier=ppn)
File "<pyshell#67>", line 11, in plot_decision_regions
Z = classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T)
AttributeError: 'list' object has no attribute 'ravel'
答:
0赞
Nick ODell
11/9/2023
#1
的返回值是一个列表。np.meshgrid()
从坐标向量返回坐标矩阵列表。
(来源。
然后 xx1 和 xx2 是列表,您会收到此错误。
可用于将列表转换为数组。np.array()
评论
0赞
Karl Knechtel
11/9/2023
目前尚不清楚简单地将生成的列表强制回数组会导致应用有意义的东西,更不用说再次堆叠和转置这两个结果了。请尽量不要暗示类型是任意的障碍,或者知道如何转换类型是解决由错误类型引起的问题所需要的一切。.ravel
0赞
Nick ODell
11/9/2023
@KarlKnechtel 问题在于为什么会发生错误。答案是关于为什么会发生错误。如果你觉得它不合格,我鼓励你写一个更好的。
0赞
hpaulj
11/9/2023
#2
调试时最好的两个朋友是 numpy 文档和交互式测试环境
有问题的行是:
xx1 = np.meshgrid(np.arange(x1_min,x1_max,resolution))
xx2 = np.meshgrid(np.arange(x2_min,x2_max,resolution))
np.array([xx1.ravel(),xx2.ravel()]).T
让我们用一个简单的范围来做范围。我不会讨论你如何选择最小值和最大值。但要小心使用像 这样的分数步骤。端点处理可能是不可预测的,因此 . 警告这一点。0.02
arange
arange
In [205]: xx1 = np.meshgrid(np.arange(0,10))
In [206]: xx1
Out[206]: [array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]
In [207]: xx1.ravel()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[207], line 1
----> 1 xx1.ravel()
AttributeError: 'list' object has no attribute 'ravel'
正如我们告诉我们的,结果是一个 ,而一个列表没有 .你清楚你为什么使用和吗?一个简单的就足够了。meshgrid
list
ravel
meshgrid
ravel
arange
通常用于 2 个或更多范围。只用一个就用它没什么好处。meshgrid
In [208]: xx1 = np.arange(0,10)
现在让我们尝试在最后一行中制作数组(使用另一个代替):xx1
xx2
In [209]: np.array([xx1,xx1]).T
Out[209]:
array([[0, 0],
[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5],
[6, 6],
[7, 7],
[8, 8],
[9, 9]])
请注意,如果有不同的长度 - 我还没有看过最小值/最大值是否进行 anthing 来控制该长度。另外还有我之前提到的分数问题。xx2
step
In [210]: xx2 = np.arange(0,8)
In [211]: np.array([xx1,xx2]).T
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[211], line 1
----> 1 np.array([xx1,xx2]).T
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.
这个错误消息有点晦涩难懂,但它是处理“参差不齐”数组的长序列中的最新消息。 无法从长度不同的数组创建数值 dtype 数组。numpy
同样,我不知道你的 ovrall 目标是什么,但这应该有助于隔离构建数组以馈送到分类器的潜在问题。
评论
xx1 = np.meshgrid(np.arange(x1_min,x1_max,resolution))
xx1
.ravel
xx1
xx1
list
list
list
Array
xx1
ravel
xx1
xx1
np.meshgrid