NumPy: n-dimensional arrays#

  • Sum of elements and store in sum_of_matrix variable

  • Find max/min element in matrix and store in max_value/min_value variable

import numpy as np
matrix = np.array([[1, 5, 3], [9, 2, 6], [4, 8, 7], [0, 3, 2]])
sum_of_matrix = np.sum(matrix)
max_value, min_value = np.max(matrix), np.min(matrix)
print(sum_of_matrix, max_value, min_value)

Hide code cell output

50 9 0
matrix = np.array([[1, 5, 3], [9, 2, 6], [4, 8, 7], [0, 3, 2]])
max_idx = np.unravel_index(np.argmax(matrix), matrix.shape)
min_idx = np.unravel_index(np.argmin(matrix), matrix.shape)
max_value = matrix[max_idx]
min_value = matrix[min_idx]
print(max_value, min_value)

Hide code cell output

9 0

Create Numpy array#

From List#

  • Create 1D array from list [1, 2, 3, 4, 5]

  • Create 2D array of shape 2x3 from list [[1, 2, 3], [4, 5, 6]]

  • Create 3D array of shape 2x2x3 from list [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]

import numpy as np

list_1d = np.array([1, 2, 3, 4, 5])
list_2d = np.array([[1, 2, 3], [4, 5, 6]])
list_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print('list_1d: ', list_1d)
print('list_2d: ', list_2d)
print('list_3d: ', list_3d)

Hide code cell output

list_1d:  [1 2 3 4 5]
list_2d:  [[1 2 3]
 [4 5 6]]
list_3d:  [[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]
list_3d.dtype, list_3d.shape, list_3d.ndim

Hide code cell output

(dtype('int64'), (2, 2, 3), 3)

Array Creation Functions#

np.zeros#

np.zeros(shape, dtype=float, order='C', *, like=None)

import numpy as np

zeros_matrix_2x3 = np.zeros((2,3))
print(zeros_matrix_2x3)

Hide code cell output

[[0. 0. 0.]
 [0. 0. 0.]]

np.ones#

np.ones(shape, dtype=None, order='C', *, like=None)

import numpy as np

ones_matrix_2x3 = np.ones((2,3))
print(ones_matrix_2x3)

Hide code cell output

[[1. 1. 1.]
 [1. 1. 1.]]

np.full#

np.full(shape, fill_value, dtype=None, order='C', *, like=None)

import numpy as np

full_of_9_matrix = np.full((2,3), 9)
print(full_of_9_matrix)

Hide code cell output

[[9 9 9]
 [9 9 9]]

np.arange#

np.arange([start, ]stop, [step, ]dtype=None, *, like=None)

import numpy as np

arange_numpy = np.arange(start=0, stop=5, step=1)
print(arange_numpy)

Hide code cell output

[0 1 2 3 4]

np.eye#

np.eye(N, M=None, k=0, dtype=<class 'float'>, order='C', *, like=None)

import numpy as np

ones_in_diagonal_zeros_elsewhere = np.eye(3)
print(ones_in_diagonal_zeros_elsewhere)

Hide code cell output

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

np.random.random#

np.random.random(size=None)

Return random floats in the half-open interval [0.0, 1.0).

import numpy as np

my_arr = np.random.random((2,3))
print(my_arr)

Hide code cell output

[[0.3511155  0.15105555 0.04231165]
 [0.38675743 0.85587757 0.31778496]]

Important NumPy functions#

np.clip#

np.clip(a, a_min, a_max, out=None, **kwargs)

Any values less than a_min are set to a_min, and any values greater than a_max are set to a_max.

import numpy as np

arr = np.array([1,5,8,10,3,6])
clipped_arr = np.clip(arr, a_min=3, a_max=8)
print(clipped_arr)

Hide code cell output

[3 5 8 8 3 6]

np.concatenate#

np.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")

import numpy as np

arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
concatenated = np.concatenate((arr1, arr2))
print(concatenated)

Hide code cell output

[1 2 3 4 5 6]

Add more axes to the array#

import numpy as np

arr = np.array([1,2,3,4,5])
new_arr1 = arr[:, np.newaxis]
new_arr2 = arr[np.newaxis, :]
print(new_arr1.shape, new_arr1)
print(new_arr2.shape, new_arr2)

Hide code cell output

(5, 1) [[1]
 [2]
 [3]
 [4]
 [5]]
(1, 5) [[1 2 3 4 5]]

np.vectorize#

np.vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None)

This function converts a Python function that operates on scalars into a vectorized function that can operate on NumPy arrays.

import numpy as np

def calculate_circle_area(radius):
    return np.pi * (radius**2)

radius_values = np.array([1,2,3,4])
vectorized_func = np.vectorize(calculate_circle_area)
area_values = vectorized_func(radius_values)
print(area_values)

Hide code cell output

[ 3.14159265 12.56637061 28.27433388 50.26548246]

np.where#

np.where(condition, x=None, y=None, *, out=None)

import numpy as np

arr = np.array([1,2,3,4,5])
greater_than_3 = np.where(arr > 3, 0, arr)
print(greater_than_3)

Hide code cell output

[1 2 3 0 0]

flatten#

<array>.flatten(order='C')

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
flattened_arr = arr.flatten()