繁体   English   中英

如果有内容,为什么 class 'numpy.ndarray' 为空(迭代 0 维数组)?

[英]Why is the class 'numpy.ndarray' empty (iteration over a 0-d array) if there is content?

我检查了以下链接中给出的问题/答案,没有发现任何可以帮助我的东西:1) 如何从 python 中的文本文件中读取数字数组2) TypeError:迭代 0 维数组 Python 3) 如何索引Python 中的 0 维数组?

这篇文章是最接近的: 为什么 python 认为我的数组是 0-d? (TypeError:迭代 0 维数组)

所以,我将在这里写我的问题,而不是打开一个新标签。 我希望这很好,我是新来的,如果不是这样,请原谅我。

我的情况:

我做了一个随机采样 function (用于 class 练习),如下所示:

def randomSamples(array):
    print(array)
    print(type(array))
    i = 0

    while i < len(array):
        sampling1 = np.random.choice((array), 5)
        i += 1
        sampling1 = np.concatenate([sampling1])
        print(sampling1)

print(type(sampling1))

然后我像这样运行 function:

test1 = np.random.choice(15, 13)
sampling2 = randomSamples(test1)
sampling3 = np.asarray(sampling2)
print(type(sampling3))
sampling3.shape  # Nothing comes out, something may be wrong.

output 是:

[ 7  9  6  3 13  7  1  1  9  9  0  6 12]
<class 'numpy.ndarray'>
[6 9 7 9 9]
[12  1  1 13 12]
[ 9  7 13  0  1]
[3 1 9 3 1]
[ 1  1  7  6 13]
[ 6  9  7 12  0]
[ 9 12  3  3  6]
[3 9 6 3 3]
[ 1  9  9  6 13]
[6 1 1 3 3]
[1 9 9 3 1]
[13  9 13  9  9]
[ 7  1  6  0 12]
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

当我运行时:

SEM(sampling3)

我得到:

<class 'numpy.ndarray'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-556-1456ec9b184d> in <module>
----> 1 SEM(sampling3)

<ipython-input-269-90a8bbeb1e1a> in SEM(array)
      4     array1 = []
      5 
----> 6     for i in array:
      7         counter += i
      8         a1 = float(counter/len(array))

TypeError: iteration over a 0-d array

我不明白为什么 function 的结果是 'numpy.ndarray' class,我什至用 np.asarray 创建了另一个变量(sampling3)以确保它是一个 np.array。

我注意到 shape 属性显示为空。 理想情况下,数组应为:name = [[6 9 7 9 9],[12 1 1 13 12],...,[7 1 6 0 12]],形状为 (13,5)。

任何帮助,将不胜感激。 提前致谢。

让我们逐步了解您的函数的操作:

制作初始样本:

In [22]: arr = np.random.choice(15,13)                                                         
In [23]: arr                                                                                   
Out[23]: array([ 1,  3,  0, 14, 13, 13, 10,  9,  5,  0, 12, 12,  2])

在循环内部从中取样:

In [25]: samp = np.random.choice((arr), 5)                                                     
In [26]: samp                                                                                  
Out[26]: array([14,  5,  5,  3,  3])

concatenate什么都不做。 它应该做什么?

In [27]: samp = np.concatenate([samp])                                                         
In [28]: samp                                                                                  
Out[28]: array([14,  5,  5,  3,  3])

取另一个样本( () arr什么都不做):

In [29]: samp = np.random.choice(arr, 5)                                                       
In [30]: samp                                                                                  
Out[30]: array([13,  3,  9,  9, 12])
In [31]: samp = np.concatenate([samp])                                                         
In [32]: samp                                                                                  
Out[32]: array([13,  3,  9,  9, 12])

samp Out[28]的采样已丢失。 如果要在循环中保存值,则需要将它们收集在结构中,例如列表。

alist = []
for i in range(3):
   alist.append(np.random.choice(arr, 5)

生成 3 个 arrays 的列表。

您的 function 没有 return 语句,因此它返回None

In [33]: np.asarray(None)                                                                      
Out[33]: array(None, dtype=object)
In [34]: _.shape                                                                               
Out[34]: ()

None制作一个数组会产生一个 0d 数组。

In [36]: for i in np.asarray(None): pass                                                       
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-8942a42b4c6b> in <module>
----> 1 for i in np.asarray(None): pass

TypeError: iteration over a 0-d array

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM