problem that the shape changes after numpy.insert

Viewed 105

enter image description here

I want to maintain shape (20, 1) but if I use np.insert then shape is change

how can I solve this problem?

2 Answers

reshape is not an inplace function. So use plist = plist.reshape(20,1)

Use optional parameter axis=0 of np.insert without axis it will flattened array first.

x_test = np.random.rand(2,20,1)
plist = x_test[-1]
plist = plist[1:]

plist = np.insert(plist, 19, 0.1, axis=0)
plist
array([[0.98886077],
       [0.03109283],
       [0.77616058],
       [0.16404329],
       [0.93935774],
       [0.26792546],
       [0.58095962],
       [0.62661957],
       [0.77044689],
       [0.047671  ],
       [0.37621377],
       [0.86003833],
       [0.23979666],
       [0.42546281],
       [0.67893301],
       [0.37828647],
       [0.46051384],
       [0.4041629 ],
       [0.09148197],
       [0.1       ]])
Related