0

When I print the array it looks like this

print(my_array)
[ 0 0 0 1 0 1...  
1 0 2 0 1 ]  
[ 0 0 0 1 0 1...  
1 0 2 0 1 ]  
none

When I index the array with a single index like

print(my_array[0])

it returns

0  
0  
none

but if I try, for example

print(my_array[0,0])

I get

"IndexError: too many indices for array"

Finally, the shape returns

(750,)  
(750,)  
None  

and the type returns

class 'numpy.ndarray'   
class 'numpy.ndarray'  
None  

This array was given to me as is for a homework assignment, so I don't know how it was made (Coursera Course). How can I split the three arrays? I can see that each 750 data points are repeats, and I just need one set of the 750 to use for the assignment.

1
  • The array is 1d with object dtype. Like a list is contains pointers to other arrays. It's not a 2d array (matrix) but rather more like a list of lists. Commented Oct 1, 2018 at 23:24

1 Answer 1

1

Your array has dtype object because it's not a numeric array with the same number of columns in each row. It looks something like this:

L = [np.array([0, 0, 1]), np.array([0, 0, 0]), None]

A = np.array(L)

array([array([0, 0, 1]),
       array([0, 0, 0]),
       None], dtype=object)

for i in A:
    print(type(i))    

<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'NoneType'>

That's not how you should use NumPy. You can try removing the None values to construct a regular NumPy numeric array:

B = np.array([i for i in L if i is not None])

array([[0, 0, 1],
       [0, 0, 0]])

If in doubt, check the dtype of an array to check it has the correct type:

A.dtype  # dtype('O')
B.dtype  # dtype('int32')
Sign up to request clarification or add additional context in comments.

Comments

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.