Numpy
Install packages
You can use both pip magic as astral/uv
| !uv pip install -q numpy==2.3.0 sympy==1.14.0
# %pip install numpy==2.3.0 sympy==1.14.0
|
Import packages
| import numpy as np
import sympy
from IPython.display import Math
|
Usage
Basic
| array1 = np.array([1, 2, 3, 4, 5])
Math(sympy.latex(sympy.Matrix(array1)))
|
\(\displaystyle \left[\begin{matrix}1\\2\\3\\4\\5\end{matrix}\right]\)
| print(array1)
print(type(array1))
print(array1.shape)
|
[1 2 3 4 5]
(5,)
| array2 = np.array([1, 2, 3, 4, 5])
array2.reshape(1, 5) # 1 row, 5 columns
|
array([[1, 2, 3, 4, 5]])
| array3 = np.array([[1, 2, 3, 4, 5]])
Math(sympy.latex(sympy.Matrix(array3)))
|
\(\displaystyle \left[\begin{matrix}1 & 2 & 3 & 4 & 5\end{matrix}\right]\)
(1, 5)
| array4 = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]])
Math(sympy.latex(sympy.Matrix(array4)))
|
\(\displaystyle \left[\begin{matrix}1 & 2 & 3 & 4 & 5\\2 & 3 & 4 & 5 & 6\end{matrix}\right]\)
| print(array4)
print(array4.shape)
|
[[1 2 3 4 5]
[2 3 4 5 6]]
(2, 5)
| # 0 - 10, from 2 to 2
np.arange(0, 10, 2).reshape(5, 1)
|
array([[0],
[2],
[4],
[6],
[8]])
| np.ones((3, 4)) # 3 rows, 4 columns of number one
|
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
| # Entity Matrix
np.eye(3)
|
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Array attributes
| array5 = np.array([[1, 2, 3], [4, 5, 6]])
Math(sympy.latex(sympy.Matrix(array5)))
|
\(\displaystyle \left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\end{matrix}\right]\)
| print(f"Array: {array5}\n")
print(f"(Rows, Columns): {array5.shape}")
print(f"Array number of dimensions: {array5.ndim}")
print(f"Array size: {array5.size}")
print(f"Data type: {array5.dtype}")
print(f"Item size (in bytes): {array5.itemsize}")
|
Array: [[1 2 3]
[4 5 6]]
(Rows, Columns): (2, 3)
Array number of dimensions: 2
Array size: 6
Data type: int64
Item size (in bytes): 8
Vectorized operations
| array6 = np.array([1, 2, 3, 4, 5])
array7 = np.array([10, 20, 30, 40, 50])
print(f"Addition: {array6 + array7}")
print(f"Subtraction: {array6 - array7}")
print(f"Multiplication: {array6 * array7}")
print(f"Division: {array6 / array7}")
|
Addition: [11 22 33 44 55]
Subtraction: [ -9 -18 -27 -36 -45]
Multiplication: [ 10 40 90 160 250]
Division: [0.1 0.1 0.1 0.1 0.1]
Mathematical expressions
| array8 = np.array([2, 3, 4, 5, 6])
print(f"Square root: {np.sqrt(array8)}")
print(f"Exponential: {np.exp(array8)}")
print(f"Sine: {np.sin(array8)}")
print(f"Natural log: {np.log(array8)}")
|
Square root: [1.41421356 1.73205081 2. 2.23606798 2.44948974]
Exponential: [ 7.3890561 20.08553692 54.59815003 148.4131591 403.42879349]
Sine: [ 0.90929743 0.14112001 -0.7568025 -0.95892427 -0.2794155 ]
Natural log: [0.69314718 1.09861229 1.38629436 1.60943791 1.79175947]
Array slicing and indexing
| array9 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
Math(sympy.latex(sympy.Matrix(array9)))
|
\(\displaystyle \left[\begin{matrix}1 & 2 & 3 & 4\\5 & 6 & 7 & 8\\9 & 10 & 11 & 12\end{matrix}\right]\)
| print(f"Array:\n {array9}")
print(f"Element in first row and first column: {array9[0][0]}") # array9[0, 0]
print(
"From rows from second onwards, columns from third onwards:\n "
f" {array9[1:, 2:]}"
)
|
Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Element in first row and first column: 1
From rows from second onwards, columns from third onwards:
[[ 7 8]
[11 12]]
Modify Arrays
| array10 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
Math(sympy.latex(sympy.Matrix(array10)))
|
\(\displaystyle \left[\begin{matrix}1 & 2 & 3 & 4\\5 & 6 & 7 & 8\\9 & 10 & 11 & 12\end{matrix}\right]\)
| array10[0, 0] = 15
Math(sympy.latex(sympy.Matrix(array10)))
|
\(\displaystyle \left[\begin{matrix}15 & 2 & 3 & 4\\5 & 6 & 7 & 8\\9 & 10 & 11 & 12\end{matrix}\right]\)
| array10[2:] = 20
Math(sympy.latex(sympy.Matrix(array10)))
|
\(\displaystyle \left[\begin{matrix}15 & 2 & 3 & 4\\5 & 6 & 7 & 8\\20 & 20 & 20 & 20\end{matrix}\right]\)
Statistical applications
Normalization
Mean of zero and standard deviation of 1
| array11 = np.array([1, 2, 3, 4, 5])
Math(sympy.latex(sympy.Matrix(array11)))
|
\(\displaystyle \left[\begin{matrix}1\\2\\3\\4\\5\end{matrix}\right]\)
| mean = np.mean(array11)
std_dev = np.std(array11)
normalized_array11 = (array11 - mean) / std_dev
Math(sympy.latex(sympy.Matrix(normalized_array11)))
|
\(\displaystyle \left[\begin{matrix}-1.41421356237309\\-0.707106781186547\\0.0\\0.707106781186547\\1.41421356237309\end{matrix}\right]\)
Statistical methods
| array12 = np.array([1, 2, 3, 4, 5])
print(f"Mean: {np.mean(array12)}")
print(f"Median: {np.median(array12)}")
print(f"Standard Deviation: {np.std(array12)}")
print(f"Variance: {np.var(array12)}")
|
Mean: 3.0
Median: 3.0
Standard Deviation: 1.4142135623730951
Variance: 2.0
Logical Operations
| array13 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
print(f"Data greater than 5:\n{array13 > 5}")
print(
f"Data greater than 5 and less than 8:\n"
f"{array13[(array13 > 5) & (array13 < 8)]}"
)
|
Data greater than 5:
[False False False False False True True True True True True]
Data greater than 5 and less than 8:
[6 7]