簡體   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