【问题标题】:how do I fix TypeError: only integer scalar arrays can be converted to a scalar index? [duplicate]如何修复 TypeError:只有整数标量数组可以转换为标量索引? [复制]
【发布时间】:2019-10-28 13:03:37
【问题描述】:

当我创建代码以使其输入“Pin”并且代码将尝试对其进行解码时,我编写了一个代码来解决此问题,但它循环了太多次,所以我尝试用它来缩短它。

import numpy as np

print('enter your pin')
p = [
[int(input())],
[int(input())],
[int(input())],
[int(input())]
]
x = [
[0],
[0],
[0],
[0]
]
np_X = np.array(x)
y = [
[1],
[1],
[1],
[1]
]
np_Y = np.array(y)

while np_X.all(np_X) != p:
        np_X = np_X + np_Y
        print(np_X)

但它给了我一个error,我尝试自己解决它,但我得到的只是

TypeError:只有整数标量数组可以转换为标量索引。

我想知道我做错了什么。

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    np_X 是一个 (4,1) 数组:

    In [114]: np_X                                                                  
    Out[114]: 
    array([[0],
           [0],
           [0],
           [0]])
    

    这是产生错误的代码。您应该已经显示了整个回溯。它可以帮助我们以及确定问题所在。

    In [115]: np_X.all(np_X)                                                        
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-115-a3b876cb154f> in <module>
    ----> 1 np_X.all(np_X)
    
    /usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py in _all(a, axis, dtype, out, keepdims)
         46 
         47 def _all(a, axis=None, dtype=None, out=None, keepdims=False):
    ---> 48     return umr_all(a, axis, dtype, out, keepdims)
         49 
         50 def _count_reduce_items(arr, axis):
    
    TypeError: only integer scalar arrays can be converted to a scalar index
    

    查看numpyall 的文档。它需要一个axis 参数,而不是一个数组!

    有效的用途是:

    In [116]: np_X.all(0)                                                           
    Out[116]: array([False])
    In [117]: np_X.all(1)                                                           
    Out[117]: array([False, False, False, False])
    

    你想做什么?比较pnp_X

    In [119]: np_X != p                                                             
    Out[119]: 
    array([[ True],
           [False],
           [ True],
           [ True]])
    

    all 方法应用于该布尔数组:

    In [120]: (np_X != p).all()                                                     
    Out[120]: False
    

    使您的数组 (4,1) 形状、列向量是不必要的复杂化。一个简单的 4 元素数组就足够了:

    In [121]: np_X = np.zeros(4, int)                                               
    In [122]: np_X                                                                  
    Out[122]: array([0, 0, 0, 0])
    p = [
    int(input()),
    int(input()),
    int(input()),
    int(input())
    ]
    

    或者简单地说:

    p = [int(input()) for _ in range(4)] 
    

    【讨论】:

    • 应该被接受的答案。
    【解决方案2】:

    把你的while循环改成这个。

    • np_X == p 将返回一个布尔值数组。
    • 每个布尔值将对应两个数组中的数字是否匹配。
    • np.all 仅当数组中的每个元素都为 true 时才会返回 true
    • 因此,如果 n_X 正好是 p,则 np.all(np_X == p) 返回 true
    while not np.all(np_X == p):
        np_X = np_X + np_Y
        print(np_X)
    

    但是,您的整个代码仍然无法正常工作。您将 1 添加到 x 的每个元素。相反,您似乎想遍历 4 个整数 0 - 9 的所有可能组合。

    即。

    from itertools import combinations
    for pin_1, pin_2, pin_3, pin_4 in combinations(range(9), 4):
        np_X = np.array([[pin_1], [pin_2], [pin_3], [pin_4]])
        if np.all(np_X == p):
            break
    print(np_X)
    

    【讨论】:

      【解决方案3】:

      在这一行:

      while np.all(np_X != p)

      你正在打印

      while np_X.all(np_X) != p:
      

      【讨论】:

        猜你喜欢
        • 2021-02-09
        • 2021-01-27
        • 2019-04-26
        • 2017-12-15
        • 2021-10-31
        • 1970-01-01
        • 2018-04-04
        • 2022-01-03
        • 2018-09-21
        相关资源
        最近更新 更多