Numpy resize or Numpy reshape

感情迁移 提交于 2020-03-27 11:43:34

问题


I've been scouring the stackexchange archives and can not seem to come across the right answer... should reshape be used, should resize be used, but both fail...

setup: 3 netCDF files of two resolutions... 1 500 meter, 2 1000 meter

need to resize or decrease resolution or reshape or whatever the right word is the higher resolution file :)

using either gdalinfo or "print (np.shape(array))" we know that the higher resolution file has a shape or size of (2907, 2331) and the lower resolution array has the size of (1453, 1166)

So i have tried both np.resize (array, (1453,1166)) and np.reshape (array, (1453,1166)) and receive errors such as:

ValueError: cannot reshape array of size 6776217 into shape (1453,1166)

Surely I'm using the wrong terms / lingo and I apologize for that... on the command line to do what I would need done it would be as simple as gdal_translate -outsize x y -of GTiff infile outfile

Please help!


回答1:


Neither.

Reshape only changes the shape of the data, but not the total size, so you can for example reshape an array of shape 1x9 into one which is 3x3, but not into 2x4.

Resize does similar thing, but lets you increase the size, in which case it will fill new space with elements of array which is being resized.

You have two choices: write your function which does resizing in the manner you want it to do so, or use one of Python image libraries (PIL, Pillow...) to apply common image resizing functions.




回答2:


Did had the same problem:

  File "primes_test2audio.py", line 117, in <module>
  librosa.display.specshow(features.reshape(n_feat, n_frames) ,
  ValueError: cannot reshape array of size 9620 into shape (20,313)

Solution: you divide by 9620/20=481 and you get a compatible shape:

  shape of one sample in 2D:  (20, 481)
  shape of one sample flat: (9620,)

n_frames = 481 # number of frames for each sample

n_features = 20 # number of coefficients for mfcc analysis



来源:https://stackoverflow.com/questions/45985692/numpy-resize-or-numpy-reshape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!