Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3-dimensional array in numpy

Tags:

python

numpy

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.

Creating a matrix:

x = np.zeros((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] 

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.

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

like image 548
Vejto Avatar asked Apr 10 '14 07:04

Vejto


People also ask

How do you create a 3-dimensional array using numpy in Python?

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.]]])

What is a 3-dimensional array?

A 3D array is a multi-dimensional array(array of arrays). A 3D array is a collection of 2D arrays . It is specified by using three subscripts:Block size, row size and column size. More dimensions in an array means more data can be stored in that array.


1 Answers

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.]]]) 

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]]] 

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).

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

Some caveats:

  1. 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).

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

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

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

like image 193
ffriend Avatar answered Sep 28 '22 07:09

ffriend