0

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.

3
  • sampling3.shape # Nothing comes out, something may be wrong. - what's wrong on that particular line is that you forgot to print. Commented Apr 4, 2020 at 22:45
  • 1
    Also you forgot to return anything, and you're throwing away the results of the previous iteration on each new iteration. Commented Apr 4, 2020 at 22:47
  • Thank you user2357112 supports Monica. You made me realize that I had a misconception of return versus print. I thought that by assigning the out of print to a variable I was doing more than a return, seeing and saving it. I appreciate you pointed out the problem. Commented Apr 5, 2020 at 16:29

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @hpaulj for a very clear explanation. I have understood the problems in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.