调试NumPy类型错误?[从上一个ValueError编辑]

2024-06-10 19:07:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写下面的代码,得到了错误:

TypeError:只有size-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)

有人能查一下原因并给我指出正确的方向吗?你知道吗


Tags: inforsizelabelsifnprange语句
1条回答
网友
1楼 · 发布于 2024-06-10 19:07:14

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

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)数组(对于np.dot并不奇怪)。下一次迭代是(1,10)数组。不能用intasscalar转换成标量的。你知道吗

我已经指出了错误的位置和问题。并提出了调试方法。但我不会试图为您解决这个问题-您需要仔细跟踪数组维度。不要试图用intasscalar命令包装东西。你知道吗

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

相关问题 更多 >