Numpy#

NumPy is a powerful Python library for numerical computation. It provides efficient multi-dimensional array operations, along with a wide range of mathematical functions. NumPy’s compact syntax and high-performance capabilities make it essential for scientific and data-related tasks.
import numpy as np
Data Types
NumPy introduces specialized data types that are more memory-efficient and offer faster computations compared to the built-in Python data types. These NumPy data types are designed to handle large arrays efficiently, enabling vectorized operations and better numerical precision. In contrast, the standard Python data types are more general-purpose but may not perform as efficiently when dealing with large-scale numerical computations.
Category |
Type |
|---|---|
Boolean |
|
Complex |
|
Float |
|
Integer |
|
Object |
|
String |
|
Creation#
my_array = np.array([1, 2, 3, 4, 5]) # simple array creation
my_array
array([1, 2, 3, 4, 5])
print(type(my_array))
<class 'numpy.ndarray'>
ndim: returns the number of dimensions of an array
shape: returns the number of elements in each dimension (like calling len on each dimension)
size: returns the total number of elements in an array (i.e., the product of shape)
array_0d = np.array(1)
array_1d = np.array([1, 2, 3, 4, 5, 6])
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
array_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[3, 2, 1], [6, 5, 4]]])
list_2d = [[1, 2], [3, 4], [5, 6]] # creation from list
array_2d = np.array(list_2d)
array_2d
array([[1, 2],
[3, 4],
[5, 6]])
print(np.ndim(array_2d))
print(np.shape(array_2d))
print(np.size(array_2d))
2
(3, 2)
6
Functions
Function |
Paramethers |
Description |
|---|---|---|
|
start, stop, step*, dtype*=None … |
Returns evenly spaced values within a given interval |
|
v, k*=0 |
Returns a diagonal 2d-array extracting the values in the array |
|
shape, dtype*=float … |
Returns a new empty array of given shape and type |
|
N, M*=None, k*=0, dtype*=float … |
Returns a new identity 2d-array of given number of row (and columns) and type |
|
shape, fill_value, dtype*=None … |
Returns a new array of given shape and type, filled with a specific value |
|
n, dtype*=None … |
Returns a new identity quadratic 2d-array of given number of row and columns |
|
start, stop, num*=50, dtype*=None … |
Returns evenly spaced numbers over a specified interval |
|
shape, dtype*=None … |
Returns a new array of given shape and type, filled with ones |
|
a, size*=None, replace*=True, p*=None |
Generates random values from a given 1d-array in a given shape with specific probabilities |
|
d0, d1, …, dN |
Generates random values between 0 and 1 in a given shape |
|
low, high*=None, size*=None, dtype*=None |
Generates random integer values between start and end in a given shape |
|
shape, dtype*=float … |
Returns a new array of given shape and type, filled with zeros |
Examples
np.arange(1, 5)
array([1, 2, 3, 4])
np.arange(0, 11, 2)
array([ 0, 2, 4, 6, 8, 10])
np.diag([1, 2, 3])
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
np.empty((3, 4))
array([[1.17233938e-311, 2.47032823e-322, 0.00000000e+000,
0.00000000e+000],
[0.00000000e+000, 2.46567317e+179, 6.19915850e-091,
3.12074122e-033],
[1.00552133e-047, 4.99511561e+174, 3.99910963e+252,
4.95516258e-062]])
np.eye(3, 4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
np.full((3, 4), 3.14)
array([[3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14]])
np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
np.linspace(0, 10, 5)
array([ 0. , 2.5, 5. , 7.5, 10. ])
np.ones((3, 4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np.random.choice([1, 2, 3, 4, 5], (3, 4))
array([[4, 3, 4, 4],
[5, 2, 5, 1],
[4, 1, 2, 1]])
np.random.rand(3, 4)
array([[0.61002814, 0.850292 , 0.05808765, 0.57604482],
[0.61193594, 0.11855404, 0.73917881, 0.58380001],
[0.92385904, 0.29403394, 0.21585735, 0.52637638]])
np.random.randint(-10, 50, (3, 4))
array([[29, 2, 6, 29],
[41, 24, 3, -6],
[10, -6, 23, 47]])
np.zeros((3, 4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
Access#
Array indexing is the same as accessing an array element.
You can access an array element by referring to its index number.
As usual, the indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
arr = np.array([[1,2,3,4], [5,6,7,8]])
print('1st row and 2nd column: ', arr[0, 1])
1st row and 2nd column: 2
Array slicing in python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start:end].
We can also define a step, like this: [start:end:step].
If we don’t pass start its considered 0.
If we don’t pass end its considered length of array in that dimension.
If we don’t pass step its considered 1.
print('From the 2nd row, slice elements from column 1 to 4 (not included): ', arr[1, 1:4])
From the 2nd row, slice elements from column 1 to 4 (not included): [6 7 8]
Array iterating means going through elements one by one. You can iterate through the elements of an array using the for loop.
for x in arr: # iterate through all the rows
print(x)
[1 2 3 4]
[5 6 7 8]
for x in arr: # 1) iterate through all the elements
for y in x:
print(y)
1
2
3
4
5
6
7
8
To iterate through the elements of an array, the nditer(op, flags*=None, op_dtypes*=None …) function or the ndenumerate(arr) function can be used:
for x in np.nditer(arr): # 2) iterate through all the elements
print(x)
1
2
3
4
5
6
7
8
# 3) iterate through all the elements skipping 1 element at a time
for x in np.nditer(arr[:, ::2]):
print(x)
1
3
5
7
# 4) iterate through all the elements changing the data type in-place
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
print(x)
b'1'
b'2'
b'3'
b'4'
b'5'
b'6'
b'7'
b'8'
# 5) iterate through all the elements printing the indexes
for idx, x in np.ndenumerate(arr):
print(idx, x)
(0, 0) 1
(0, 1) 2
(0, 2) 3
(0, 3) 4
(1, 0) 5
(1, 1) 6
(1, 2) 7
(1, 3) 8
Array filtering refers to the operation of extracting elements from an array that meet a particular condition, creating a new array that contains only those selected elements.
print(arr[arr % 2 == 0])
[2 4 6 8]
Array searching refers to the process of finding specific elements or their indices within an array based on a given condition or value. To search for element indexes, the where(condition) function can be used:
print(arr % 2 == 0)
print(np.where(arr % 2 == 0))
[[False True False True]
[False True False True]]
(array([0, 0, 1, 1], dtype=int64), array([1, 3, 1, 3], dtype=int64))
Operations#
Elementwise
Elementwise operations involve performing mathematical operations independently on each element of a set. They enable efficient processing of large datasets and are commonly used in programming and mathematical computations.
array_x = np.array([[1,2,3], [4,5,6]]) # 2x3
array_y = np.array([[12,11,10], [9,8,7]]) # 2x3
array_z = np.array([[1], [2], [3]]) # 3x1
array_w = np.array([[1, 2, 3]]) # 1x3
array_x + 1 # operation between array and scalar
array([[2, 3, 4],
[5, 6, 7]])
array_x + array_y # operation between two arrays
array([[13, 13, 13],
[13, 13, 13]])
array_x == array_y # compares the elements of two arrays by returning an array # of Boolean values indicating which elements are equal
array([[False, False, False],
[False, False, False]])
np.array_equal(x, y) # checks whether two arrays are identical
True
np.dot(array_x, array_z) # scalar product between arrays
# array_x @ array_z # equivalent
array([[14],
[32]])
array_z + array_w # broadcasting
array([[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
Reshape
The reshape function in NumPy allows for changing the shape or dimensions of an array without altering its data. It enables reorganizing the elements of an array into a different shape while ensuring that the total number of elements remains the same. The reshape function does not change the array in place.
arr.reshape(4, 2) # changes the shape
arr.reshape(2, -1) # changes the shape by recognizing missing dimensions
arr.reshape(2, 2, 2) # changes shape and dimensions
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
The resize function in NumPy allows modifying the shape of an array, either by adding or removing elements, to fit the specified dimensions. It can resize the array inplace, altering its data, and can also introduce or discard elements based on the new shape.
arr.resize(4, 2) # changes the shape in place
The flatten function in NumPy converts a multi-dimensional array into a 1D array by concatenating all the elements in a row-major order. It creates a flat array, which is useful for simplifying the array structure and iterating over the elements sequentially. The flatten function does not change the array in place.
arr.flatten()
array([1, 2, 3, 4, 5, 6, 7, 8])
Sort
The sort function in NumPy arranges the elements of an array in ascending order by default, or in descending order if specified. This enables efficient data analysis, searching, and retrieval of specific values within the array.
np.sort(arr)
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
Join and Split
Joining in NumPy usually refers to concatenating or combining arrays along a specified axis. Splitting, on the other hand, involves dividing an array into multiple smaller arrays along a specified axis.
To do the join between two arrays there are basically two ways: chaining (concatenate function) joins two arrays without adding additional dimensions, while stacking (stack function) adds another dimension.
print(np.shape(array_x), np.shape(array_y))
array_c1 = np.concatenate((array_x, array_y)) # concatenates along columns
print(array_c1)
print(np.shape(array_c1))
array_c2 = np.concatenate((array_x, array_y), axis=1) # concatenates along rows
print(array_c2)
print(np.shape(array_c2))
(2, 3) (2, 3)
[[ 1 2 3]
[ 4 5 6]
[12 11 10]
[ 9 8 7]]
(4, 3)
[[ 1 2 3 12 11 10]
[ 4 5 6 9 8 7]]
(2, 6)
array_s1 = np.stack((array_x, array_y)) # stacks two arrays along columns
print(array_s1)
print(np.shape(array_s1))
array_s2 = np.stack((array_x, array_y), axis=1) # stacks two arrays along rows
print(array_s2)
print(np.shape(array_s2))
[[[ 1 2 3]
[ 4 5 6]]
[[12 11 10]
[ 9 8 7]]]
(2, 2, 3)
[[[ 1 2 3]
[12 11 10]]
[[ 4 5 6]
[ 9 8 7]]]
(2, 2, 3)
array_s3 = np.hstack((array_x, array_y)) # stacks horizontally (along columns)
print(array_s3)
print(np.shape(array_s3))
array_s4 = np.vstack((array_x, array_y)) # stacks vertically (along rows)
print(array_s4)
print(np.shape(array_s4))
array_s5 = np.dstack((array_x, array_y)) # stacks adding a depth dimension
print(array_s5)
print(np.shape(array_s5))
[[ 1 2 3 12 11 10]
[ 4 5 6 9 8 7]]
(2, 6)
[[ 1 2 3]
[ 4 5 6]
[12 11 10]
[ 9 8 7]]
(4, 3)
[[[ 1 12]
[ 2 11]
[ 3 10]]
[[ 4 9]
[ 5 8]
[ 6 7]]]
(2, 3, 2)
To split an array into many subarrays, the array_split functions or variants of the split function are used in a similar and opposite manner to the join functions.
array_split1 = np.array_split(array_x, 2) # splits an array into n arrays by columns
print(array_split1)
array_split2 = np.array_split(array_x, 2, axis=1) # splits an array into n arrays by rows
print(array_split2)
# np.hsplit(array_x, 2)
# np.vsplit(array_x, 2)
# np.dsplit(array_x, 2)
[array([[1, 2, 3]]), array([[4, 5, 6]])]
[array([[1, 2],
[4, 5]]), array([[3],
[6]])]
Functions#
Category |
Transformation |
Function |
Description |
|---|---|---|---|
Arithmetic |
1 array -> 1 array |
|
Computes the absolute values of the elements in an array |
|
Returns the cumulative sum of elements in an array along a specified axis |
||
|
Returns the cumulative product of elements in an array along a specified axis |
||
|
Computes the exponential values of the elements in an array |
||
|
Compute the logarithms of the elements in an array |
||
2 array -> 1 array |
|
Performs element-wise addition between two arrays |
|
|
Performs element-wise subtraction between two arrays |
||
|
Performs element-wise multiplication between two arrays |
||
|
Performs element-wise division between two arrays |
||
|
Returns the quotient and the remainder of element-wise division between two arrays |
||
|
Raises the elements of an array to the power of corresponding elements in another array |
||
|
Returns the remainder of element-wise division between two arrays |
||
1+ array -> 1 number |
|
Returns the sum of elements in an array |
|
|
Returns the product of elements in an array |
||
Linear Algebra |
1 array -> 1 array |
|
Returns the determinant of a square matrix |
|
Returns the eigenvalues and eigenvectors of a square matrix |
||
|
Returns the inverse of a square matrix |
||
|
Returns the rank of a matrix |
||
|
Returns the Euclidean norm of a vector or the matrix norm of an array |
||
|
Returns the trace of a matrix |
||
|
Returns a specific decomposition of a matrix |
||
|
Returns the transposed matrix |
||
2 array -> 1 array |
|
Performs matrix multiplication between two arrays |
|
Rounding |
1 array -> 1 array |
|
Rounds the elements of an array towards zero to the nearest integer |
|
Rounds the elements of an array to the specified number of decimals or to the nearest integer if no decimals are provided |
||
|
Rounds the elements of an array up to the nearest integer |
||
|
Rounds the elements of an array down to the nearest integer |
||
Sets |
2 array -> 1 array |
|
Returns the sorted unique values in an array, removing any duplicates |
|
Returns the sorted, unique values that are common to two input arrays |
||
|
Returns the sorted, unique values that are in one input array but not in the other |
||
|
Returns the sorted, unique values from combining two input arrays, removing any duplicates |
||
Statistics |
1+ array -> 1 number |
|
Returns the minimum / maximum value along a specified axis or the minimum / maximum element in an array |
|
Returns the arithmetic mean of elements in an array along a specified axis |
||
|
Returns the median value of elements in an array along a specified axis |
||
|
Returns the specified quantile(s) of elements in an array along a specified axis |
||
Trigonometric |
1 array -> 1 array |
|
Returns the specific trigonometric function of elements in an array |