HMReliable
HMReliable

Reputation: 885

Simple reshape of numpy array: error: 'total size of new array must be unchanged'

Here is a very straightforward version of the problem i am having reshaping a 40*1 array into a 20*2 array. What is going wrong here?

import numpy as np
x=np.linspace(1,20,40)
#confirm length is 40
print(np.shape(x))
#reshape to 2*20
print(np.reshape(x,2,20))
#returns error: 'total size of new array  must be unchanged'

Upvotes: 3

Views: 111

Answers (1)

seralouk
seralouk

Reputation: 33127

You do not use the function as you should use it.

Simply use this:

np.reshape(x,(2,20))

Documentation here

Full code:

import numpy as np
x=np.linspace(1,20,40)
#confirm length is 40
print(np.shape(x))
print(np.reshape(x,(2,20)))

Upvotes: 3

Related Questions