"builtin_function_or_method' object has no attribute 'reshape'" what does this mean?

Viewed 2704

I'm a novice, so this question may be somewhat obvious for someone.

import numpy as np
print("array")
array = np.arange(8)
matrix = np.array.reshape(2,4)
print(matrix)

The result is this.

array
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-88e5e9409344> in <module>
      2 print("array")
      3 array = np.arange(8)
----> 4 matrix = np.array.reshape(2,4)
      5 print(matrix)

AttributeError: 'builtin_function_or_method' object has no attribute 'reshape'

I don't know why it does not work.

1 Answers

It looks like you're calling reshape on np.array, which is a function that is used to create a new array.

You already created your variable array. Try to use this variable instead of np.array:

import numpy as np
print("array")
array = np.arange(8)
matrix = array.reshape(2,4) # <-- remove the "np." to access a function on your array
print(matrix)

Why is this?

myArray = np.array(k) is a function that creates a new NumPy array using the input k. The result of this function is returned and saved to a variable (in my case myArray).

On this array, you can call functions to manipulate it (like reshape).

What you tried to do: You used np.array (remember, the function that creates an array). You did not use your array, but used a function pointer instead.

Related