AttributeError:“list”对象没有属性“ravel”

AttributeError: 'list' object has no attribute 'ravel'

提问人:Derrick Walker 提问时间:11/9/2023 最后编辑:Nick ODellDerrick Walker 更新时间:11/9/2023 访问量:52

问:

以下块继续返回相同的错误,我不明白为什么......有什么帮助吗?我已将 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'
python numpy 属性错误

评论

1赞 Karl Knechtel 11/9/2023
用你自己的话来说,代码说,你到底希望它做什么?你认为之后的价值是什么,你认为它的类型是什么?为什么?您是否尝试通过阅读文档或检查值来检查这一点?你明白为什么不应该在这个结果上使用吗?xx1 = np.meshgrid(np.arange(x1_min,x1_max,resolution))xx1.ravel
0赞 Derrick Walker 11/9/2023
错误消息表明第 11 行导致了问题。你们有没有怀疑它可能是第 9 行?我同意我希望 np.meshgrid() 返回一个坐标列表,包括 x1_max 和 x1_min,它们将用作决策区域的组成部分。同意?
1赞 SmellyCat 11/9/2023
第 9 行是分配的位置。 是一个 .第 11 行报告一个错误,因为 ravel 不是 Python 内置的方法。也许,您的意思是在分配(在第 9 行)之前将 转换为 Numpy,就像 Nick 建议的那样。也许您打算调用 的各个元素,并且您需要遍历 (在第 11 行)的元素。xx1xx1listlistlistArrayxx1ravelxx1xx1
0赞 Karl Knechtel 11/9/2023
“ 同意我希望 np.meshgrid() 返回一个坐标列表,包括 x1_max 和 x1_min,它们将用作决策区域的组成部分。同意吗?是的。在第 9 行,您调用 ,它为您提供一个列表。列表不是 Numpy 数组。在第 11 行,您尝试将列表用作 Numpy 数组,这会导致错误消息。错误消息无法告诉您问题的最终原因,只能告诉您近因。这是关于错误消息如何工作的基本内容,在尝试学习 Numpy 之前,您应该了解这些内容。np.meshgrid

答:

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.02arangearange

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'

正如我们告诉我们的,结果是一个 ,而一个列表没有 .你清楚你为什么使用和吗?一个简单的就足够了。meshgridlistravelmeshgridravelarange

通常用于 2 个或更多范围。只用一个就用它没什么好处。meshgrid

In [208]: xx1 = np.arange(0,10)

现在让我们尝试在最后一行中制作数组(使用另一个代替):xx1xx2

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 来控制该长度。另外还有我之前提到的分数问题。xx2step

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 目标是什么,但这应该有助于隔离构建数组以馈送到分类器的潜在问题。