首页 > 解决方案 > 调试 NumPy 类型错误?[从以前的 ValueError 编辑]

问题描述

我正在编写下面的代码并收到错误:

TypeError:只有大小为 1 的数组可以转换为 Python 标量

问题出在“if语句和agrmnt变量”,但不知道为什么,因为 “agrmnt”变量是int类型。我不确定为什么我不能在这里使用条件语句。

import numpy as np

feature_matrix= np.array([[ 0.1837462,  0.29989789, -0.35889786, -0.30780561, -0.44230703, -0.03043835,
   0.21370063,  0.33344998, -0.40850817, -0.13105809],
 [ 0.08254096,  0.06012654,  0.19821234,  0.40958367,  0.07155838, -0.49830717,
   0.09098162,  0.19062183, -0.27312663,  0.39060785],
 [-0.20112519, -0.00593087,  0.05738862,  0.16811148, -0.10466314, -0.21348009,
   0.45806193, -0.27659307,  0.2901038,  -0.29736505],
 [-0.14703536, -0.45573697, -0.47563745, -0.08546162, -0.08562345,  0.07636098,
  -0.42087389, -0.16322197, -0.02759763,  0.0297091 ],
 [-0.18082261,  0.28644149, -0.47549449, -0.3049562,   0.13967768,  0.34904474,
   0.20627692,  0.28407868,  0.21849356, -0.01642202]])
labels = np.array([-1, -1, -1,  1, -1])
T= 10
L= 0.1456692551041303

    tta = np.zeros((feature_matrix[0].size)).reshape(-1,1)
    tta_0 = 0
    for t in range(T):
        for i in range(feature_matrix.shape[0]):
            agrmnt = np.asscalar(labels[i]*(int(np.dot(feature_matrix[i][np.newaxis], tta)) + tta_0))
            if agrmnt<=1:
                tta = (1-(L*1/((t+1)**0.5))*tta)+(1/((t+1)**0.5)* 
                         (labels[i]*feature_matrix[i][np.newaxis])))
                tta_0 = tta_0 + (labels[i]*1/((t+1)**0.5))
            else:
                tta = (1-(L*((t+1)**0.5))*tta[np.newaxis].T)
                tta_0 = tta_0

    print(tta,tta_0)

有人可以检查为什么并指出我正确的方向吗?

标签: pythonnumpy

解决方案


复制粘贴您的代码,并更正一个语法错误:

1256:~/mypy$ python3 stack56844635.py 
Traceback (most recent call last):
  File "stack56844635.py", line 21, in <module>
    agrmnt = np.asscalar(labels[i]*(int(np.dot(feature_matrix[i][np.newaxis], tta)) + tta_0))
TypeError: only size-1 arrays can be converted to Python scalars

很明显,错误就在这asscalar条线上。

将代码更改为:

    temp = labels[i]*(int(np.dot(feature_matrix[i][np.newaxis], tta)) + tta_0)
    print(temp)
    agrmnt = np.asscalar(temp)

我明白了

1259:~/mypy$ python3 stack56844635.py 
0
Traceback (most recent call last):
  File "stack56844635.py", line 21, in <module>
    temp = labels[i]*(int(np.dot(feature_matrix[i][np.newaxis], tta)) + tta_0)
TypeError: only size-1 arrays can be converted to Python scalars

所以它显然在迭代中执行得很好,产生了一个 0 值;但下一个循环会导致错误。但更仔细地看那条线,我看到了一个int(). 那会产生同样的错误。

把它拿出来:

    temp = labels[i]*(np.dot(feature_matrix[i][np.newaxis], tta) + tta_0)
    print(temp)
    agrmnt = np.asscalar(temp)

1300:~/mypy$ python3 stack56844635.py 
[[-0.]]
[[ 0.41001225  0.49396662  0.01778946  0.0547189  -0.04249864  0.25519979
   0.4316633   0.51821805 -0.01806885  0.1824719 ]]
Traceback (most recent call last):
  File "stack56844635.py", line 23, in <module>
    agrmnt = np.asscalar(temp)
  File "/usr/local/lib/python3.6/dist-packages/numpy/lib/type_check.py", line 547, in asscalar
    return a.item()
ValueError: can only convert an array of size 1 to a Python scalar

现在错误移动到ascalar(使用.item())。

在第一次迭代temp中是一个 (1,1) 数组(从 a 来看并不奇怪np.dot)。下一次迭代是一个 (1,10) 数组。这不能用int或转换成标量asscalar

我已经指出了错误位置和问题。并建议调试方法。但我不会尝试为您解决这个问题 - 您需要仔细跟踪数组维度。不要试图在 wrap things inintasscalarcommands 中作弊。

最初tta是 (10,1) 数组,但在第一个循环之后是 (10,10)。


推荐阅读