At the core of the NumPy package is the ndarray object – a core data structure used in the NumPy library. The ndarray is an n-dimensional array of homogeneous data types. The array is a table of elements (normally numbers) arranged in rows and columns.
NumPy’s array class is called ndarray. It is also known by the alias array.
Check the link below for information on ndarrays and arrays.
https://numpy.org/doc/stable/reference/arrays.ndarray.html#arrays-ndarray
What is an array
An array is a structure for storing and retrieving data. NumPy arrays are classified by what are called dimensions, as described below. The dimensions are also called axes in NumPy.
The array dimensions can be 0-dimension, 1-dimension, 2-dimension or more than two dimensions (n-dimensional array).
0-dimension Array (Scalar)
The 0-dimensional arrays are also called Scalars. A scalar is a single numerical value that is not part of an array. It represents a magnitude and no direction. Scalars do not have any axes.
Scalars can be of various data types, such as integers, floats, or complex numbers. They are the simplest form of data in NumPy and can be created using the np.array() function.
The program below creates a Scalar data type.
PROGRAM EXAMPLE: 0-dimension (Scalar) NumPy Arrays
# Scalar (0-dimension array) Example
import numpy as np
array0d = np.array(21) #array0d is a 0-dimension array (scalar)
print("0-dimension array created by the above statement is : \n ", array0d)
The following is the program’s output:
0-dimension array created by the above statement is :
21
1-dimension Array
A one-dimensional (1-D) NumPy array is a linear array that contains elements in a single dimension. It is similar to lists in Python. The 1-dimensional array is sometimes referred to as a vector.
Differences between Python lists and NumPy 1-dimension arrays
- The elements in a 1-D array must be of the same type (homogeneous); whereas the lists elements may be of mixed data.
- The performance of computation of 1-D array is much faster as compared to lists for large datasets.
- The memory usage of a 1-D array is generally more efficient than that of lists.
The 1-D array can be created using the example below:
PROGRAM EXAMPLE: 1-dimensional NumPy Array and List
# 1-D Array and List
print("PROGRAM NAME: array1-D and list")
import numpy as np
list1 = [1, 2, 3, 4]
print("list1 is printed below : \n", list1)
print("----------------- End of List printing --------------------- \n")
# Create a 1-D array
array1d = np.array([1, 2, 3, 4])
print("1-D array is printed below : \n ", array1d)
print()
#Convert list to a 1-D array
arraynew = np.array(list1)
print("arraynew is the array converted from list1 : \n", arraynew)
The following is the program’s output:
PROGRAM NAME: array1-D and list
list1 is printed below :
[1, 2, 3, 4]
—————– End of List printing ———————
1-D array is printed below :
[1 2 3 4]
arraynew is the array converted from list1 :
[1 2 3 4]
2-dimensional Array
A 2-dimensional array (sometimes referred to as matrix) comprises of homogeneous data values arranged in a rectangular (or square) array in rows and columns. A 2-D format allow for efficient representation and manipulation of large, structured data.
The program below shows how to create a 2-D array.
PROGRAM EXAMPLE: 2-dimensional NumPy Array
# 2-D Array Example
print("PROGRAM NAME : array2d.ph")
import numpy as np
array2d = np.array([[1, 3, 4, 5], [6, 7, 8,9]])
print("2-D array is printed below : \n ", array2d)
print()
# Another Example
array2d1= np.array([(1, 2, 3, 4), (5, 6, 7, 8)])
print("Another 2-D array is printed below : \n ", array2d1)
print()
The following is the program’s output:
PROGRAM NAME : array2d.py
2-D array is printed below :
[[1 3 4 5]
[6 7 8 9]]
Another 2-D array is printed below :
[[1 2 3 4]
[5 6 7 8]]
3-dimensional Array
A 3-dimensional array has the elements arranged in three axes. You can imagine a 3-dimensional array as two or more 2D arrays printed on separate pages and overlaid together. For physical visualization of 3-D arrays, think about the 3-D array as a cube with the elements along the three axes. You can create a 3D array using the np.array() function, as in the program example below.
PROGRAM EXAMPLE: 3-dimensional NumPy Array
print(" Program name: array3D.py \n")
import numpy as np
# Create a (2x2x2) 3D array
array3d = np.array([[[1, 2], [3, 4]], [[5, 6], [8, 9]]])
print("The (2x2x2) 3D array is printed below: \n", array3d)
print("---- End of (2x2x2) 3D array ---- \n")
# Another Example: (2x3x2) array
# Create a (2x3x2) 3D array
array3d1 = np.array([[[1, 2], [3, 4], [10, 11]], [[5, 6], [8, 9], [12, 13]]])
print("The 2x3x2 array is printed below: \n", array3d1)
print(" ---- End of (2x3x2) 3D array ---- \n")
print("Example of 4x3x2 3D array \n")
# Create a (4x3x2) 3D array
array3d2 = np.array([[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]], [[13, 14], [15, 16], [17, 18]], [[19, 20], [21, 22], [23, 24]]])
print(" 4x3x2 3-D array is : \n", array3d2)
The following is the program’s output:
Program name: array3D.py
The (2x2x2) 3D array is printed below:
[[[1 2]
[3 4]]
[[5 6]
[8 9]]]
—- End of (2x2x2) 3D array —-
The 2x3x2 array is printed below:
[[[ 1 2]
[ 3 4]
[10 11]]
[[ 5 6]
[ 8 9]
[12 13]]]
—- End of (2x3x2) 3D array —-
Example of 4x3x2 3D array
4x3x2 3-D array is :
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]
[[13 14]
[15 16]
[17 18]]
[[19 20]
[21 22]
[23 24]]]
n-dimensional Array
An array with more than 3 dimensions is difficult to visualize physically since our physical world is limited to three dimensions.
Printing Arrays
Note that when you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:
- The last axis is printed from left to right,
- The second-to-last axis is printed from top to bottom,
- The rest are also printed from top to bottom, with each slice separated from the next by an empty line.
One-dimensional arrays are printed as rows, 2-dimensionals as matrices, and 3-dimensional as lists of matrices.
The program example shows how to print an array in reverse.
Program Example: How to Print in Reverse
# print in reverse
import numpy as np
print("Create an array 'a' \n")
a = np.array([1, 3, 5, 27, 9, 10, 16, 17, 18, 29])
#
print("array 'a' is: \n", a)
print()
print("Now reverse the array 'a' and print it.")
print("Array 'b' is the revesed array. \n")
b = a[::-1] # reversed a
print("Reversed array 'b' is: \n", b)
The following is the program’s output showing how to print an array in reverse.
Create an array ‘a’
array ‘a’ is:
[ 1 3 5 27 9 10 16 17 18 29]
Now reverse the array ‘a’ and print it.
Array ‘b’ is the reversed array.
Reversed array ‘b’ is:
[29 18 17 16 10 9 27 5 3 1]