简体   繁体   中英

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

I have checked the questions/answers given in the following links and found nothing that can help me: 1) how to read array of numbers from text file in python 2) TypeError: iteration over a 0-d array Python 3) How to index 0-d array in Python?

This post is the closest: Why does python think my array is 0-d? (TypeError: iteration over a 0-d array)

So, I am going to write my question here, rather than opening up a new tag. I hope this is fine, I am new here, so pardon me if this is not the way.

My case:

I made a randomSampling function (for a class exercise), like this:

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))

I then run the function like this:

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.

The output is:

[ 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'>

When I run:

SEM(sampling3)

I get:

<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

I don't understand why the outcome of the function although it is 'numpy.ndarray' class, and I even created another variable (sampling3) with np.asarray to make sure it is a np.array.

I notice that the shape attribute comes out empty. Ideally, the array would be: name = [[6 9 7 9 9],[12 1 1 13 12],...,[ 7 1 6 0 12]], with shape (13,5).

Any help would be appreciated. Thanks in advance.

Let's step through your function's action:

Make the initial sample:

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])

Inside the loop take a sampling from that:

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

The concatenate does nothing. What was it supposed to do?

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

take another sample (the () arr do nothing):

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])

The samp from Out[28] has been lost. If you want save values in a loop you need to collect them in a structure, such as a list.

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

produces a list of 3 arrays.

Your function does not have a return statement, so it returns None :

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

making an array out of None produces a 0d array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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