Hey everyone,
Currently I am learning arrays in python for learning machine learning, and I learned 1D array and 2D array now I want to learn 3D array, but I don't get any resource which explaining 3D arrays in python, I searched on google, Gemini, ChatGPT, Bing ai, YouTube. But anyone is not explaining 3D array properly,
Can anyone please Explain me 3D arrays and How 3D arrays look like?
Videos
A one dimensional array is an array for which you have to give a single argument (called index) to access a specific value.
E.G. with the following one dimensional array
array = [0,1,2,9,6,5,8]
The array at index 1 has the value 1. The array at index 3 has value 9. If you want to update the 3rd value to 8 in the array, you should do
array[2] = 8
A two-dimensional array is simply an array of arrays. So, you have to give two arguments to access a single value.
two_dim_array = [[1,2,3],[4,5,6],[7,8,9]]
If you want to update the 'second' value, you have to do
two_dim_array[0][1] = 'something'
That is because two_dim_array[0] is a one-dimensional array, and you still have to specify an index to access a value.
From now on, you can keep going deeper with the same reasoning. As any further dimension is another level in the list. So a three dimensional array would be :
3d_array =
[
[
[1,2,3,4],
[5,6,7,8]
],
[
[9,10,11,12],
[13,14,15,16]
]
]
Now to access a value you have to give .. 3 parameters. Because
3d_array[0] // is a two-dim array
3d_array[0][1] // is a one-dim array
3d_array[0][1][0] // is a value
I suggest you start doing simple exercices to get you familiar with this concept, as it is really 101 programming stuff. W3resource has great exercices to get you started.
To declare a two-dimensional array, you simply list two sets of empty brackets, like this:
int numbers[][];
Here, numbers is a two-dimensional array of type int. To put it another way, numbers is an array of int arrays.
Often, nested for loops are used to process the elements of a two-dimensional array, as in this example:
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
numbers[x][y] = (int)(Math.random() * 100) + 1
}
}
To declare an array with more than two dimensions, you just specify as many sets of empty brackets as you need. For example:
int[][][] threeD = new int[3][3][3];
Here, a three-dimensional array is created, with each dimension having three elements. You can think of this array as a cube. Each element requires three indexes to Access.
You can nest initializers as deep as necessary, too. For example:
int[][][] threeD =
{ { {1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} },
{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
{ {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } };
You need to use
data.reshape((data.shape[0], data.shape[1], 1))
Example
from numpy import array
data = [[11, 22],
[33, 44],
[55, 66]]
data = array(data)
print(data.shape)
data = data.reshape((data.shape[0], data.shape[1], 1))
print(data.shape)
Running the example first prints the size of each dimension in the 2D array, reshapes the array, then summarizes the shape of the new 3D array.
Result
(3,2)
(3,2,1)
Source :https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/
If you want to create a 3D Matrix where every subarray is in the new 3D dimension, wouldn't the final shape be (350,9,5)? In that case, you can simply use:
new_array = np.asarray(data).reshape(350,9,5)
I currently have a loop where I have a 3D array A[2x2x1000] and a 2D array B[2x2]. I want to multiply A by B for every value in the 3rd dimension without using a loop.
My current loop does the job but it runs slowly.
for i in range(1000):
C[:,:,i] = A[:,:,i]*BI've come across np.einsum, np.matmul and np.tensordot but I'm not completely sure how these work and how to use them.
Any help would be much appreciated.
You need to use np.transpose to rearrange dimensions. Now, n x m x 3 is to be converted to 3 x (n*m), so send the last axis to the front and shift right the order of the remaining axes (0,1). Finally , reshape to have 3 rows. Thus, the implementation would be -
img.transpose(2,0,1).reshape(3,-1)
Sample run -
In [16]: img
Out[16]:
array([[[155, 33, 129],
[161, 218, 6]],
[[215, 142, 235],
[143, 249, 164]],
[[221, 71, 229],
[ 56, 91, 120]],
[[236, 4, 177],
[171, 105, 40]]])
In [17]: img.transpose(2,0,1).reshape(3,-1)
Out[17]:
array([[155, 161, 215, 143, 221, 56, 236, 171],
[ 33, 218, 142, 249, 71, 91, 4, 105],
[129, 6, 235, 164, 229, 120, 177, 40]])
[ORIGINAL ANSWER]
Let's say we have an array img of size m x n x 3 to transform into an array new_img of size 3 x (m*n)
Initial Solution:
new_img = img.reshape((img.shape[0]*img.shape[1]), img.shape[2])
new_img = new_img.transpose()
[EDITED ANSWER]
Flaw: The reshape starts from the first dimension and reshapes the remainder, this solution has the potential to mix the values from the third dimension. Which in the case of images could be semantically incorrect.
Adapted Solution:
# Dimensions: [m, n, 3]
new_img = new_img.transpose()
# Dimensions: [3, n, m]
new_img = img.reshape(img.shape[0], (img.shape[1]*img.shape[2]))
Strict Solution:
# Dimensions: [m, n, 3]
new_img = new_img.transpose((2, 0, 1))
# Dimensions: [3, m, n]
new_img = img.reshape(img.shape[0], (img.shape[1]*img.shape[2]))
The strict is a better way forward to account for the order of dimensions, while the results from the Adapted and Strict will be identical in terms of the values (set(new_img[0,...])), however with the order shuffled.