numpy中的3维数组

编程入门 行业动态 更新时间:2024-10-27 05:30:16
本文介绍了numpy中的3维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Python和Numpy的新功能,试图创建3维数组.我的问题是,与Matlab相比,尺寸的顺序不正确.实际上,顺序根本没有道理.

New at Python and Numpy, trying to create 3-dimensional arrays. My problem is that the order of the dimensions are off compared to Matlab. In fact the order doesn't make sense at all.

创建矩阵:

x = np.zeros((2,3,4))

在我的世界中,这应该导致2行,3列和4个深度尺寸,并且应表示为:

In my world this should result in 2 rows, 3 columns and 4 depth dimensions and it should be presented as:

[0 0 0 [0 0 0 [0 0 0 [0 0 0 0 0 0] 0 0 0] 0 0 0] 0 0 0]

在每个深度尺寸上分开. 相反,它显示为

Seperating on each depth dimensions. Instead it is presented as

[0 0 0 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 0 0 0 0]

即3行,4列和2个深度尺寸.即,第一维是深度".为了进一步解决此问题,使用OpenCV导入图像时,颜色尺寸是最后一个尺寸,也就是说,我将颜色信息视为深度尺寸.如果我只想在已知的较小3维数组上尝试操作,这会使事情变得非常复杂.

That is, 3 rows, 4 column and 2 depth dimensions. That is, the first dimension is the "depth". To further add to this problem, importing an image with OpenCV the color dimension is the last dimension, that is, I see the color information as the depth dimension. This complicates things greatly if all I want to do is try something on a known smaller 3-dimensional array.

我误解了吗?如果不是,为什么使用这种不直观的3D三维阵列方法会变得麻木?

Have I misunderstood something? If not, why the heck is numpy using such a unintuitive way of working with 3D-dimensional arrays?

推荐答案

您有一个截断的数组表示形式.让我们看一个完整的示例:

You have a truncated array representation. Let's look at a full example:

>>> a = np.zeros((2, 3, 4)) >>> a array([[[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]], [[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]])

NumPy中的数组打印为单词array,后跟结构,类似于嵌入式Python列表.让我们创建一个类似的列表:

Arrays in NumPy are printed as the word array followed by structure, similar to embedded Python lists. Let's create a similar list:

>>> l = [[[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]], [[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]] >>> l [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]]

此复合列表l的第一级正好具有2个元素,就像数组a的第一维(行数)一样.这些元素中的每个元素本身就是一个包含3个元素的列表,该列表等于a的第二维(列数).最后,嵌套最多的列表每个都有4个元素,与a的第三维(深度/颜色数)相同.

The first level of this compound list l has exactly 2 elements, just as the first dimension of the array a (# of rows). Each of these elements is itself a list with 3 elements, which is equal to the second dimension of a (# of columns). Finally, the most nested lists have 4 elements each, same as the third dimension of a (depth/# of colors).

因此,您具有与Matlab完全相同的结构(就尺寸而言),只是以另一种方式打印.

So you've got exactly the same structure (in terms of dimensions) as in Matlab, just printed in another way.

一些警告:

  • Matlab逐列存储数据("Fortran顺序"),而NumPy默认情况下逐行存储数据("C顺序").这不会影响索引编制,但可能会影响性能.例如,在Matlab中,有效循环将遍历列(例如for n = 1:10 a(:, n) end),而在NumPy中最好遍历行(例如for n in range(10): a[n, :]-注意n在第一个位置,而不是最后一个).

  • Matlab stores data column by column ("Fortran order"), while NumPy by default stores them row by row ("C order"). This doesn't affect indexing, but may affect performance. For example, in Matlab efficient loop will be over columns (e.g. for n = 1:10 a(:, n) end), while in NumPy it's preferable to iterate over rows (e.g. for n in range(10): a[n, :] -- note n in the first position, not the last).

    如果您在OpenCV中使用彩色图像,请记住:

    If you work with colored images in OpenCV, remember that:

    2.1.像大多数Python库一样,它以BGR格式而不是RGB存储图像.

    2.1. It stores images in BGR format and not RGB, like most Python libraries do.

    2.2.大多数功能在与矩阵坐标(i, j)相反的图像坐标(x, y)上起作用.

    2.2. Most functions work on image coordinates (x, y), which are opposite to matrix coordinates (i, j).

  • 更多推荐

    numpy中的3维数组

    本文发布于:2023-07-29 19:33:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1243448.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数组   numpy

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!