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.
sampling3.shape # Nothing comes out, something may be wrong.- what's wrong on that particular line is that you forgot toprint.returnanything, and you're throwing away the results of the previous iteration on each new iteration.