N-dimensional arrays in NumPy
Creating N-dimensional arrays
- The core of the NumPy package is the ndarray object.
- Homogeneous data types.
- By array performs reading/writing array data to disk and working with memory-mapped files.
- Provides Linear algebra, random number generation, and Fourier transform capabilities.
- This feature is wrapping inheritance C/C++/Fortran codebases and giving them a dynamic and easy-to-use interface.
- Stores data in a contiguous block of memory, independent of other built-in Python objects.
- Use much less memory than built-in Python sequences.
- Various operations perform complex computations on entire arrays without the need for Python for loops.
- It is a table of elements (usually numbers), same data type, indexed by a tuple of non-negative integers.
- Dimensions are called axes.
- Array class is called ndarray was known by the alias array.
- The array object in NumPy is called ndarray.
- Create a NumPy ndarray object by using the array() function.
ndarray.ndim : -
- The number of axes (dimensions) of the array.
ndarray.shape:-
- This is a tuple of integers indicating the size of the array in each dimension.
- For a matrix with n rows and m columns, the shape will be (n,m).
- The length of the shape tuple is therefore the number of axes, ndim.
ndarray.size
Python program to demonstrate
# basic array characteristics
import numpy as np
# Creating array object
arr = np.array( [[ 1, 2, 3],[ 4, 2, 5]] )
# Printing type of arr object
print("Array is of type: ", type(arr))
# Printing array dimensions (axes)
print("No. of dimensions: ", arr.ndim)
# Printing shape of array
print("Shape of array: ", arr.shape)
# Printing size (total number of elements) of array
print("Size of array: ", arr.size)
# Printing type of elements in array
print("Array stores elements of type: ", arr.dtype)
- The total number of elements of the array. equal to the product of the elements of shape.
- The type of the elements in the array. specify dtype’s using standard Python types. examples numpy.int32, numpy.int16, and numpy.float64.
- The size in bytes of each element of the array.
- example, an array of elements of type float64 has item size 8 (=64/8),
- in type complex32 has item size 4 (=32/8).
- It is equivalent to ndarray.dtype.itemsize.
- The buffer contains the actual elements of the array.
- Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.
Python program to demonstrate
# basic array characteristics
import numpy as np
# Creating array object
arr = np.array( [[ 1, 2, 3],[ 4, 2, 5]] )
# Printing type of arr object
print("Array is of type: ", type(arr))
# Printing array dimensions (axes)
print("No. of dimensions: ", arr.ndim)
# Printing shape of array
print("Shape of array: ", arr.shape)
# Printing size (total number of elements) of array
print("Size of array: ", arr.size)
# Printing type of elements in array
print("Array stores elements of type: ", arr.dtype)
Output :
Array is of type:
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64
>>> c
array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])
There are 6 general mechanisms for creating arrays:
Array is of type:
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64
- The type of the array can also be explicitly specified at creation time:
>>> c
array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])
There are 6 general mechanisms for creating arrays:
- Conversion from other Python structures (i.e. lists and tuples)
- Basic NumPy array creation functions (e.g. arrange, ones, zeros, etc.)
- Replicating, joining, or mutating existing arrays
- Reading arrays from disk, either from standard or custom formats
- Creating arrays from raw bytes through the use of strings or buffers
- Use of special library functions (e.g., random)
- Using Python sequences such as lists and tuples.
- Lists and tuples can define ndarray creation as 1D array, 2D array and 3D array.
- Use numpy.array to define a new array, dtype define the data type of the elements in the array explicitly
>>> a2D = np.array([[1, 2], [3, 4]])
>>> a3D = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
>>> a3D = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
2) Intrinsic NumPy array creation functions
- NumPy uses built-in functions for creating arrays.
- These functions can be split into roughly three categories, based on the dimension of the array:
1D arrays
2D arrays
ndarrays
2D arrays
ndarrays
1 - 1D array creation functions
The 1D array creation functions e.g. numpy.linspace and numpy.arange.
It takes at least two inputs, starts, and stops.
numpy.arange:- Creates arrays with regularly incrementing values. use integer start, end, and step values
example:-
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(2, 10, dtype=float)
array([2., 3., 4., 5., 6., 7., 8., 9.])
>>> np.arange(2, 3, 0.1)
array([2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])
numpy.linspace:- Create arrays with a specified number of elements, and spaced equally between the specified beginning and end values.
For example:
>>> np.linspace(1., 4., 6)
array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])
2 - 2D array creation functions
The 2D array creation functions e.g. numpy.eye, numpy.diag, and numpy.vander .
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(2, 10, dtype=float)
array([2., 3., 4., 5., 6., 7., 8., 9.])
>>> np.arange(2, 3, 0.1)
array([2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])
numpy.linspace:- Create arrays with a specified number of elements, and spaced equally between the specified beginning and end values.
For example:
>>> np.linspace(1., 4., 6)
array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])
2 - 2D array creation functions
The 2D array creation functions e.g. numpy.eye, numpy.diag, and numpy.vander .
np.eye(n, m) :- defines a 2D identity matrix. The elements where i=j (row index and column index are equal) are 1 and the rest are 0, as such:
>>> np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> np.eye(3, 5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.]])
numpy.diag:- define either a square 2D array with given values along the diagonal or if given a 2D array returns a 1D array that is only the diagonal elements. helpful in linear algebra.
>>> np.diag([1, 2, 3])
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
>>> np.diag([1, 2, 3], 1)
array([[0, 1, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]])
>>> a = np.array([[1, 2], [3, 4]])
>>> np.diag(a)
array([1, 4])
>>> np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> np.eye(3, 5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.]])
numpy.diag:- define either a square 2D array with given values along the diagonal or if given a 2D array returns a 1D array that is only the diagonal elements. helpful in linear algebra.
>>> np.diag([1, 2, 3])
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
>>> np.diag([1, 2, 3], 1)
array([[0, 1, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]])
>>> a = np.array([[1, 2], [3, 4]])
>>> np.diag(a)
array([1, 4])
vander(x, n):- defines a Vandermonde matrix as a 2D NumPy array. Each column of the Vandermonde matrix is a decreasing power of the input 1D array or list or tuple, x where the highest polynomial order is n-1. Use in generating linear least squares models.
>>> np.vander(np.linspace(0, 2, 5), 2)
array([[0. , 1. ],
[0.5, 1. ],
[1. , 1. ],
[1.5, 1. ],
[2. , 1. ]])
>>> np.vander([1, 2, 3, 4], 2)
array([[1, 1],
[2, 1],
[3, 1],
[4, 1]])
>>> np.vander((1, 2, 3, 4), 4)
array([[ 1, 1, 1, 1],
[ 8, 4, 2, 1],
[27, 9, 3, 1],
[64, 16, 4, 1]])
3 - General ndarray creation functions
The ndarray creation functions e.g. numpy.ones, numpy.zeros, and random define arrays based upon the desired shape. The ndarray creation functions can create arrays with any dimension by specifying how many dimensions and lengths are along that dimension are in a tuple or list.
numpy.zeros:- create an array filled with 0 values with the specified shape. The default dtype is float64:
>>> np.zeros((2, 3))
array([[0., 0., 0.],
[0., 0., 0.]])
>>> np.zeros((2, 3, 2))
array([[[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.]]])
numpy.ones:- create an array filled with 1 values. It is identical to zeros in all other respects as such:
>>> np.ones((2, 3))
array([[1., 1., 1.],
[1., 1., 1.]])
>>> np.ones((2, 3, 2))
array([[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]]])
numpy.random : create an array filled with random values between 0 and 1.
>>> from numpy.random import default_rng
>>> default_rng(42).random((2,3))
array([[0.77395605, 0.43887844, 0.85859792],
[0.69736803, 0.09417735, 0.97562235]])
>>> default_rng(42).random((2,3,2))
array([[[0.77395605, 0.43887844],
[0.85859792, 0.69736803],
[0.09417735, 0.97562235]],
[[0.7611397 , 0.78606431],
[0.12811363, 0.45038594],
[0.37079802, 0.92676499]]])
numpy.indices: create a set of arrays (stacked as a one-higher dimensioned array), one per dimension with each representing variation in that dimension:
>>> np.indices((3,3))
array([[[0, 0, 0],
[1, 1, 1],
[2, 2, 2]],
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]])
3) Replicating, joining, or mutating existing arrays
Once create arrays, can replicate, join, or mutate. It create new arrays.
numpy.copy: assign an array or its elements to a new variable.
example:
>>> a = np.array([1, 2, 3, 4, 5, 6])
>>> b = a[:2]
>>> b += 1
>>> print('a =', a, '; b =', b)
a = [2 3 3 4 5 6] ; b = [2 3]
>>> np.vander(np.linspace(0, 2, 5), 2)
array([[0. , 1. ],
[0.5, 1. ],
[1. , 1. ],
[1.5, 1. ],
[2. , 1. ]])
>>> np.vander([1, 2, 3, 4], 2)
array([[1, 1],
[2, 1],
[3, 1],
[4, 1]])
>>> np.vander((1, 2, 3, 4), 4)
array([[ 1, 1, 1, 1],
[ 8, 4, 2, 1],
[27, 9, 3, 1],
[64, 16, 4, 1]])
3 - General ndarray creation functions
The ndarray creation functions e.g. numpy.ones, numpy.zeros, and random define arrays based upon the desired shape. The ndarray creation functions can create arrays with any dimension by specifying how many dimensions and lengths are along that dimension are in a tuple or list.
numpy.zeros:- create an array filled with 0 values with the specified shape. The default dtype is float64:
>>> np.zeros((2, 3))
array([[0., 0., 0.],
[0., 0., 0.]])
>>> np.zeros((2, 3, 2))
array([[[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.]]])
numpy.ones:- create an array filled with 1 values. It is identical to zeros in all other respects as such:
>>> np.ones((2, 3))
array([[1., 1., 1.],
[1., 1., 1.]])
>>> np.ones((2, 3, 2))
array([[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]]])
numpy.random : create an array filled with random values between 0 and 1.
>>> from numpy.random import default_rng
>>> default_rng(42).random((2,3))
array([[0.77395605, 0.43887844, 0.85859792],
[0.69736803, 0.09417735, 0.97562235]])
>>> default_rng(42).random((2,3,2))
array([[[0.77395605, 0.43887844],
[0.85859792, 0.69736803],
[0.09417735, 0.97562235]],
[[0.7611397 , 0.78606431],
[0.12811363, 0.45038594],
[0.37079802, 0.92676499]]])
numpy.indices: create a set of arrays (stacked as a one-higher dimensioned array), one per dimension with each representing variation in that dimension:
>>> np.indices((3,3))
array([[[0, 0, 0],
[1, 1, 1],
[2, 2, 2]],
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]])
3) Replicating, joining, or mutating existing arrays
Once create arrays, can replicate, join, or mutate. It create new arrays.
numpy.copy: assign an array or its elements to a new variable.
example:
>>> a = np.array([1, 2, 3, 4, 5, 6])
>>> b = a[:2]
>>> b += 1
>>> print('a =', a, '; b =', b)
a = [2 3 3 4 5 6] ; b = [2 3]
get the same result by adding 1 to a[:2]. use the numpy.copy array creation routine as such:
>>> a = np.array([1, 2, 3, 4])
>>> b = a[:2].copy()
>>> b += 1
>>> print('a = ', a, 'b = ', b)
a = [1 2 3 4] b = [2 3]
>>> a = np.array([1, 2, 3, 4])
>>> b = a[:2].copy()
>>> b += 1
>>> print('a = ', a, 'b = ', b)
a = [1 2 3 4] b = [2 3]
There are a number of procedures to join existing arrays e.g. numpy.vstack, numpy.hstack, and numpy.block.
example :- joining four 2-by-2 arrays into a 4-by-4 array using block:
>>> A = np.ones((2, 2))
>>> B = np.eye(2, 2)
>>> C = np.zeros((2, 2))
>>> D = np.diag((-3, -4))
>>> np.block([[A, B], [C, D]])
array([[ 1., 1., 1., 0.],
[ 1., 1., 0., 1.],
[ 0., 0., -3., 0.],
[ 0., 0., 0., -4.]])
4) Reading arrays from disk, either from standard or custom formats
$ cat simple.csv
x, y
0, 0
1, 1
2, 4
3, 9
example :- joining four 2-by-2 arrays into a 4-by-4 array using block:
>>> A = np.ones((2, 2))
>>> B = np.eye(2, 2)
>>> C = np.zeros((2, 2))
>>> D = np.diag((-3, -4))
>>> np.block([[A, B], [C, D]])
array([[ 1., 1., 1., 0.],
[ 1., 1., 0., 1.],
[ 0., 0., -3., 0.],
[ 0., 0., 0., -4.]])
4) Reading arrays from disk, either from standard or custom formats
- Use for large array creation. The details depend on the format of data on the disk.
- This gives general pointers for handling various formats.
- able to read and write many image formats such as jpg, png, etc.
- Delimited files such as comma-separated value (CSV) and tab-separated value (tsv) files are used for programs like Excel and LabView.
- Python functions can read and describe these files line-by-line.
- NumPy has two standard routines (procedure) for importing a file with delimited data numpy.loadtxt and numpy.genfromtxt.
$ cat simple.csv
x, y
0, 0
1, 1
2, 4
3, 9
Importing simple.csv is accomplished using loadtxt:
>>> np.loadtxt('simple.csv', delimiter = ',', skiprows = 1)
array([[0., 0.],
[1., 1.],
[2., 4.],
[3., 9.]])
5) Creating arrays from raw bytes through the use of strings or buffers
6) Use of special library functions (e.g., SciPy, Pandas, and OpenCV)
==============================================
>>> np.loadtxt('simple.csv', delimiter = ',', skiprows = 1)
array([[0., 0.],
[1., 1.],
[2., 4.],
[3., 9.]])
5) Creating arrays from raw bytes through the use of strings or buffers
- There are a variety of approaches.
- For the simple format then one can write a simple I/O library and use the NumPy fromfile() function and .tofile() method to read and write NumPy arrays directly (byteorder).
- Requires more advanced knowledge to interface with C or C++.
6) Use of special library functions (e.g., SciPy, Pandas, and OpenCV)
- NumPy is the fundamental library for array containers in the Python Scientific Computing stack. Many Python libraries, including SciPy, Pandas, and OpenCV, use NumPy ndarrays as the common format for data exchange.
- These libraries can create, operate on, and work with NumPy arrays.
==============================================
0 Comments