首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调试NumPy TypeError?[从以前的ValueError编辑]

调试NumPy TypeError?[从以前的ValueError编辑]
EN

Stack Overflow用户
提问于 2019-07-02 02:41:57
回答 1查看 85关注 0票数 0

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

TypeError:只有size-1数组可以转换为

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

代码语言:javascript
复制
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)

有人能检查一下原因并指出正确的方向吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-02 19:58:10

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

代码语言:javascript
复制
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行中。

将代码更改为:

代码语言:javascript
复制
    temp = labels[i]*(int(np.dot(feature_matrix[i][np.newaxis], tta)) + tta_0)
    print(temp)
    agrmnt = np.asscalar(temp)

我得到了

代码语言:javascript
复制
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()。也会产生同样的错误。

把它拿出来:

代码语言:javascript
复制
    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)数组,但在第1循环之后是(10,10)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56844635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档