Python Numpy 从 np 数组中删除一个维度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37152031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:57:34  来源:igfitidea点击:

Numpy remove a dimension from np array

pythonarraysnumpy

提问by Kevin

I have some images I want to work with, the problem is that there are two kinds of images both are 106 x 106 pixels, some are in color and some are black and white.

我有一些我想处理的图像,问题是有两种图像都是 106 x 106 像素,一些是彩色的,一些是黑白的。

one with only two (2) dimensions:

一个只有两 (2) 个维度:

(106,106)

(106,106)

and one with three (3)

和一个三(3)

(106,106,3)

(106,106,3)

Is there a way I can strip this last dimension?

有没有办法我可以剥离这最后一个维度?

I tried np.delete, but it did not seem to work.

我试过 np.delete,但似乎没有用。

np.shape(np.delete(Xtrain[0], [2] , 2))
Out[67]: (106, 106, 2)

回答by Matt Messersmith

You could use numpy's fancy indexing (an extension to Python's built-in slice notation):

您可以使用 numpy 的奇特索引(Python 内置切片符号的扩展):

x = np.zeros( (106, 106, 3) )
result = x[:, :, 0]
print(result.shape)

prints

印刷

(106, 106)

A shape of (106, 106, 3)means you have 3 sets of things that have shape (106, 106). So in order to "strip" the last dimension, you just have to pick one of these (that's what the fancy indexing does).

A shape of(106, 106, 3)意味着您有 3 组具有 shape 的事物(106, 106)。因此,为了“剥离”最后一个维度,您只需选择其中一个(这就是花哨的索引所做的)。

You can keep any slice you want. I arbitrarily choose to keep the 0th, since you didn't specify what you wanted. So, result = x[:, :, 1]and result = x[:, :, 2]would give the desired shape as well: it all just depends on which slice you need to keep.

你可以保留任何你想要的切片。我随意选择保留第 0 个,因为您没有指定您想要什么。因此,result = x[:, :, 1]并且result = x[:, :, 2]也会给出所需的形状:这一切都取决于您需要保留哪个切片。

回答by Dmitry Mottl

Just take the mean value over the colors dimension (axis=2):

只需取颜色维度 ( axis=2)的平均值:

Xtrain_monochrome = Xtrain.mean(axis=2)

回答by Samuel Nde

When the shape of your array is (106, 106, 3), you can visualize it as a table with 106 rowsand 106 columnsfilled with data points where each point is array of 3 numberswhich we can represent as [x, y ,z]. Therefore, if you want to get the dimensions (106, 106), you must make the data points in your table of to not be arrays but single numbers. You can achieve this by extracting either the x-component, y-component or z-componentof each data point or by applying a function that somehow aggregates the three component like the mean, sum, max etc.You can extract any component just like @matt Messersmith suggested above.

当数组的形状为 时(106, 106, 3),您可以将其可视化为一个包含106 行106 列的表格,其中填充了数据点,其中每个点都array of 3 numbers可以表示为[x, y ,z]。因此,如果要获取维度(106, 106),则必须使表中的数据点不是数组而是单个数字。您可以通过提取每个数据点的x 分量、y 分量或 z 分量或通过应用以某种方式聚合三个分量(如均值、总和、最大值等)的函数来实现此目的您可以像这样提取任何分量@matt Messersmith 上面建议。

回答by Mayank Dhiman

well, you should be careful when you are trying to reduce the dimensions of an image. An Image is normally a 3-D matrix that contains data of the RGB values of each pixel. If you want to reduce it to 2-D, what you really are doing is converting a colored RGB image into a grayscale image.

好吧,当您尝试减小图像的尺寸时应该小心。图像通常是一个 3-D 矩阵,其中包含每个像素的 RGB 值的数据。如果您想将其缩小为 2-D,您真正要做的是将彩色 RGB 图像转换为灰度图像。

And there are several ways to do this like you can take the maximum of three, min, average, sum, etc, depending on the accuracy you want in your image. The best you can do is, take a weighted average of the RGB values using the formula

并且有多种方法可以做到这一点,例如您可以取三个的最大值,最小值,平均值,总和等,具体取决于您想要的图像精度。您能做的最好的事情是,使用公式对 RGB 值进行加权平均

Y = 0.299R + 0.587G + 0.114B

Y = 0.299R + 0.587G + 0.114B

where R stands for RED, G is GREEN and B is BLUE. In numpy, this can be written as

其中 R 代表红色,G 代表绿色,B 代表蓝色。在 numpy 中,这可以写成

new_image = img[:, :, 0]*0.299 + img[:, :, 1]*0.587 + img[:, :, 2]*0.114