Skip to content

Numpy

Install packages

1
2
3
4
!uv pip install -q \
    numpy==2.3.0 \
    sympy==1.14.0 \
    latexify-py==0.4.4

Import packages

1
2
3
4
import latexify
import numpy as np
import sympy
from IPython.display import Math

Creating arrays

creating an array of zeros

np.zeros(5)
array([0., 0., 0., 0., 0.])

Creating an array of ones

np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

Creating an array with specific numbers

np.full(10, 2.5)
array([2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5])

Convert a Python list to a Numpy array

1
2
3
4
5
6
7
array_from_list = np.array([1, 2, 3, 4, 5])

print(
    f"Array: {array1}\n"
    f" Shape: {array1.shape}\n"
    f" Dtype: {array1.dtype}\n"
)
Array: [1 2 3 4 5]

 Shape: (5,)

 Dtype: int64

Array representation

Math(sympy.latex(sympy.Matrix(array_from_list)))

\(\displaystyle \left[\begin{matrix}1\\2\\3\\4\\5\end{matrix}\right]\)

Accessing ana array element

1
2
3
4
array_to_access_element = np.array([10, 20, 30, 40, 50])
array_element = array_to_access_element[2]

print(f"Element at index 2: {array_element}")
Element at index 2: 30

Generating arrays with a range of values

1
2
3
4
range_start = 0
range_end = 10
range_step = 2
np.arange(range_start, range_end, range_step)
array([0, 2, 4, 6, 8])

Generating arrays with a range of values

1
2
3
4
linear_space_start = 0
linear_space_end = 10
num_points = 5
np.linspace(linear_space_start, linear_space_end, num_points)
array([ 0. ,  2.5,  5. ,  7.5, 10. ])

Multi-dimensional arrays

Multi dimensional array with zeros

multi_dimensional_zeros_array = np.zeros((2, 3))
multi_dimensional_zeros_array
array([[0., 0., 0.],
       [0., 0., 0.]])

Mathematical representation of multi-dimensional array

Math(sympy.latex(sympy.Matrix(multi_dimensional_zeros_array)))

\(\displaystyle \left[\begin{matrix}0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0\end{matrix}\right]\)

Creating a multi-dimensional array from a Python list

multi_dimensional_array_from_python_list = np.array([[1, 2, 3], [4, 5, 6]])
multi_dimensional_array_from_python_list
array([[1, 2, 3],
       [4, 5, 6]])

Mathematical representation of multi-dimensional array from a Python list

Math(sympy.latex(sympy.Matrix(multi_dimensional_array_from_python_list)))

\(\displaystyle \left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\end{matrix}\right]\)

Accessing multi-dimensional array elements

1
2
3
4
multi_dimensional_array_to_be_accessed = np.array([[10, 20, 30], [40, 50, 60]])
element_at_row_1_col_2 = multi_dimensional_array_to_be_accessed[1, 2]

print(f"Element at row 1, column 2: {element_at_row_1_col_2}")
Element at row 1, column 2: 60

Replacing elements in a multi-dimensional array

1
2
3
4
multi_dimensional_array_to_be_replaced = np.array([[1, 2, 3], [4, 5, 6]])
multi_dimensional_array_to_be_replaced[0, 1] = 99

multi_dimensional_array_to_be_replaced
array([[ 1, 99,  3],
       [ 4,  5,  6]])

Accessing multi-dimensional array rows

1
2
3
4
5
6
multi_dimensional_array_to_retrieve_rows = np.array(
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
)
first_row = multi_dimensional_array_to_retrieve_rows[0, :]

first_row
array([1, 2, 3])

Replacing multi-dimensional array rows

1
2
3
4
5
6
multi_dimensional_array_to_replace_rows = np.array(
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
)
multi_dimensional_array_to_replace_rows[1, :] = [99, 99, 99]

multi_dimensional_array_to_replace_rows
array([[ 1,  2,  3],
       [99, 99, 99],
       [ 7,  8,  9]])

Accessing multi-dimensional array columns

1
2
3
4
5
6
multi_dimensional_array_to_access_columns = np.array(
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
)
first_column = multi_dimensional_array_to_access_columns[:, 0]

first_column
array([1, 4, 7])

Randomly generated arrays

Generating a random multi-dimensional array with 3 rows and 4 columns

np.random.seed(2)  # Will allow reproducibility of same random numbers
np.random.rand(3, 4)
array([[0.4359949 , 0.02592623, 0.54966248, 0.43532239],
       [0.4203678 , 0.33033482, 0.20464863, 0.61927097],
       [0.29965467, 0.26682728, 0.62113383, 0.52914209]])

Generating a random multi-dimensional array with normal distribution

np.random.seed(2)
np.random.randn(5, 2)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [-1.79343559, -0.84174737],
       [ 0.50288142, -1.24528809],
       [-1.05795222, -0.90900761]])

Generating a random multi-dimensional array of integers

np.random.seed(2)
np.random.randint(low=0, high=100, size=(4, 3))
array([[40, 15, 72],
       [22, 43, 82],
       [75,  7, 34],
       [49, 95, 75]])

Element-wise operations on arrays

Summing a value to each element in an array

1
2
3
4
5
6
7
8
9
array_to_add_on_each_element = np.arange(5)
array_to_add_on_each_element
array_after_adding_on_each_element = array_to_add_on_each_element + 10


print(
    f"Original array: {array_to_add_on_each_element}"
    f"\nAfter adding 10 to each element: {array_after_adding_on_each_element}"
)
Original array: [0 1 2 3 4]

After adding 10 to each element: [10 11 12 13 14]

Applying multiple operations to an array

array_to_apply_multiple_operations = np.array([1, 2, 3, 4, 5])

array_after_multiple_operations_applied = (
    10 + (array_to_apply_multiple_operations * 2)
) ** 2

print(
    f"Original array: {array_to_apply_multiple_operations}"
    f"\nAfter applying multiple operations: {array_after_multiple_operations_applied}"
)
Original array: [1 2 3 4 5]

After applying multiple operations: [144 196 256 324 400]

Summing two arrays

1
2
3
4
5
first_array_to_sum = np.array([1, 2, 3])
second_array_to_sum = np.array([4, 5, 6])
sum_of_arrays = first_array_to_sum + second_array_to_sum

sum_of_arrays
array([5, 7, 9])

Comparison operations on arrays

Comparing if elements in one array are greater than the corresponding elements in another array

1
2
3
4
first_array_to_compare = np.array([10, 20, 30, 40, 50])
second_array_to_compare = np.array([15, 25, 20, 40, 60])

first_array_to_compare > second_array_to_compare
array([False, False,  True, False, False])

Which elements are actually greater?

first_array_to_compare[first_array_to_compare > second_array_to_compare]
array([30])

Summarizing operations on arrays

Return the minimum value in an array

array_to_apply_summarizations = np.array([42, 17, 23, 56, 8, 99])
np.min(array_to_apply_summarizations)
np.int64(8)

Return the maximum value in an array

np.max(array_to_apply_summarizations)
np.int64(99)

Sum all the values in an array

np.sum(array_to_apply_summarizations)
np.int64(245)

Get the mean value of an array

array_to_apply_summarizations.mean()
np.float64(40.833333333333336)