Skip to content

Commit 6e675ab

Browse files
authored
Merge pull request #23 from Golixco/feat/numpy
feat (advanced) : add NumPy array operations and math functions tutorial
2 parents 5b6b558 + 37b0aa1 commit 6e675ab

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

advanced/numpy_tutorial.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import numpy as np
2+
3+
# create one-dimensional array with five elements
4+
a = np.array([1, 2, 3, 4, 5])
5+
print("Array a:", a)
6+
7+
# create 2D array (matrix) of shape 3x3
8+
b = np.array([[1, 2, 3],
9+
[4, 5, 6],
10+
[7, 8, 9]])
11+
print("Matrix b:\n", b)
12+
13+
# array attributes
14+
print("Shape of b:", b.shape)
15+
print("Number of dimensions of b:", b.ndim)
16+
print("Data type of b elements:", b.dtype)
17+
# convert to float type
18+
b = b.astype(float)
19+
print("Converted matrix b to float, new dtype:", b.dtype)
20+
21+
# indexing and slicing
22+
print("b[0, 0]:", b[0, 0])
23+
print("First row of b:", b[0, :])
24+
print("Second column of b:", b[:, 1])
25+
# slicing with step
26+
print("Elements of a with step 2:", a[::2])
27+
28+
# basic array operations
29+
sum_array = a + np.array([5, 5, 5, 5, 5]) # adding two arrays
30+
print("a + [5,5,5,5,5]:", sum_array)
31+
32+
scaled = a * 2 # element-wise multiplication by scalar
33+
print("a * 2:", scaled)
34+
35+
# matrix multiplication (dot product)
36+
c = np.dot(b, b)
37+
print("Dot product b * b:\n", c)
38+
39+
# sum of elements
40+
print("Sum of elements in a:", a.sum())
41+
print("Sum of each column in b:", b.sum(axis=0))
42+
# example of an invalid axis in sum (to show an error)
43+
try:
44+
print("Sum with invalid axis 2:", b.sum(axis=2))
45+
except Exception as e:
46+
print("Error summing along axis 2 (invalid):", e)
47+
48+
# use of universal functions (ufuncs)
49+
d = np.array([0, np.pi/2, np.pi])
50+
print("Angles d:", d)
51+
print("sin(d):", np.sin(d))
52+
print("log of d (with -inf for zero):", np.log(d))
53+
54+
# broadcasting example
55+
e = np.arange(1, 4)
56+
f = np.array([[1], [2], [3]])
57+
print("e:", e)
58+
print("f:", f)
59+
print("Broadcasted sum e+f:\n", e + f)
60+
61+
# reshape example
62+
g = np.arange(8)
63+
g = g.reshape((2,4))
64+
print("Reshaped g to 2x4:\n", g)
65+
66+
# simple statistics
67+
print("Mean of a:", a.mean())
68+
print("Standard deviation of g:", g.std())
69+
70+
# find unique elements
71+
h = np.array([1, 2, 2, 3, 3, 3])
72+
print("Unique elements in h:", np.unique(h))
73+
74+
# sort elements of an array in descending order
75+
print("Sorted a in descending:", np.sort(a)[::-1])
76+
77+
# random array example
78+
np.random.seed(0)
79+
rand_arr = np.random.rand(3, 3)
80+
print("Random array:", rand_arr)
81+
82+
# element-wise comparison
83+
print("Elements of rand_arr > 0.5:\n", rand_arr > 0.5)
84+
85+
# creating arrays using linspace
86+
lin = np.linspace(0, 1, 5)
87+
print("Linearly spaced array:", lin)

0 commit comments

Comments
 (0)