简体   繁体   English

调整大小或调整大小

[英]Numpy resize or Numpy reshape

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... 我一直在寻找stackexchange档案,似乎无法找到正确的答案...应该使用重塑形状,应该使用重塑尺寸,但是都失败了...

setup: 3 netCDF files of two resolutions... 1 500 meter, 2 1000 meter 设置:3个具有两种分辨率的netCDF文件... 1500米,2 1000米

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) 使用gdalinfo或“ print(np.shape(array))”,我们知道分辨率较高的文件的形状或大小为(2907,2331),分辨率较低的数组的大小为(1453,1166)

So i have tried both np.resize (array, (1453,1166)) and np.reshape (array, (1453,1166)) and receive errors such as: 所以我尝试了np.resize(array,(1453,1166))和np.reshape(array,(1453,1166))并收到如下错误:

ValueError: cannot reshape array of size 6776217 into shape (1453,1166) ValueError:无法将大小为6776217的数组重塑为形状(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 xy -of GTiff infile outfile 当然,我使用了错误的术语/术语,为此我道歉...在命令行上做我需要做的事情,就像gdal_translate -outsize xy -of GTiff infile outfile一样简单

Please help! 请帮忙!

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 . 整形仅改变数据的形状,而不改变总大小,因此,例如,您可以将形状为1x9的数组整形为3x3 ,而不是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. 您有两种选择:编写可以按照您希望的方式进行大小调整的函数,或者使用Python图像库之一(PIL,Pillow ...)来应用常见的图像大小调整功能。

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: 解决方案:用9620/20 = 481除以得到兼容的形状:

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

n_frames = 481 # number of frames for each sample n_frames = 481#每个样本的帧数

n_features = 20 # number of coefficients for mfcc analysis n_features = 20#用于mfcc分析的系数数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM