|
1 | 1 | import numpy as np |
2 | 2 |
|
3 | | -# ------------------------------------------ |
| 3 | +# ----------------------------------------------------- |
4 | 4 | # 1. Creating a One-Dimensional Array |
5 | 5 | one_d_array = np.array([1, 2, 3, 4]) |
6 | 6 | # print(one_d_array) |
7 | 7 |
|
8 | | -# ------------------------------------------ |
| 8 | +# ----------------------------------------------------- |
9 | 9 | # 2. Creating a Two-Dimensional Array (Matrix) |
10 | 10 | two_d_array = np.array([[1, 2, 3, 4], |
11 | 11 | [5, 6, 7, 8]]) |
12 | 12 | # print(two_d_array) |
13 | 13 |
|
14 | | -# ------------------------------------------ |
15 | | -# 3. Creating a One-Dimensional Array Filled with Zeros |
16 | | -zero_array_1d = np.zeros(3) # 3 elements, all zeros |
| 14 | +# ----------------------------------------------------- |
| 15 | +# 3. One-Dimensional Array Filled with Zeros |
| 16 | +zero_array_1d = np.zeros(3) # Creates a 1D array of 3 elements, all set to 0 |
17 | 17 | # print(zero_array_1d) |
18 | 18 |
|
19 | | -# ------------------------------------------ |
20 | | -# 4. Creating a Two-Dimensional Array Filled with Zeros |
21 | | -zero_array_2d = np.zeros((3, 4)) # Shape = 3 rows × 4 columns |
| 19 | +# ----------------------------------------------------- |
| 20 | +# 4. Two-Dimensional Array Filled with Zeros |
| 21 | +zero_array_2d = np.zeros((3, 4)) # 3 rows × 4 columns of zeros |
22 | 22 | # print(zero_array_2d) |
23 | 23 |
|
24 | | -# ------------------------------------------ |
25 | | -# 5. Creating a Two-Dimensional Array Filled with Ones |
26 | | -ones_array = np.ones((2, 3)) # Shape = 2 rows × 3 columns |
| 24 | +# ----------------------------------------------------- |
| 25 | +# 5. Two-Dimensional Array Filled with Ones |
| 26 | +ones_array = np.ones((2, 3)) # 2 rows × 3 columns filled with 1s |
27 | 27 | # print(ones_array) |
28 | 28 |
|
29 | | -# ------------------------------------------ |
30 | | -# 6. Creating a Two-Dimensional Array Filled with a Specific Value |
31 | | -filled_array = np.full((2, 3), 5) # Shape = 2x3, filled with 5 |
| 29 | +# ----------------------------------------------------- |
| 30 | +# 6. Two-Dimensional Array Filled with a Specific Value |
| 31 | +filled_array = np.full((2, 3), 5) # 2×3 matrix filled with 5s |
32 | 32 | # print(filled_array) |
33 | 33 |
|
34 | | -# ------------------------------------------ |
35 | | -# 7. Creating a Sequence of Numbers |
36 | | -# Using arange(start, stop, step) |
37 | | -sequence_array = np.arange(1, 11, 2) # From 1 to 10 (exclusive 11), step by 2 |
| 34 | +# ----------------------------------------------------- |
| 35 | +# 7. Sequence of Numbers Using arange() |
| 36 | +sequence_array = np.arange(1, 11, 2) # From 1 to 10 (excluding 11), step size 2 |
38 | 37 | # print(sequence_array) |
39 | 38 |
|
40 | | -# ------------------------------------------ |
41 | | -# 8. Creating an Identity Matrix |
42 | | -# Identity matrix = square matrix with 1s on the diagonal and 0s elsewhere |
43 | | -identity_matrix = np.eye(5) # 5x5 Identity matrix |
| 39 | +# ----------------------------------------------------- |
| 40 | +# 8. Identity Matrix (square matrix with 1s on diagonal) |
| 41 | +identity_matrix = np.eye(5) # 5x5 identity matrix |
44 | 42 | print(identity_matrix) |
| 43 | + |
| 44 | +# ----------------------------------------------------- |
| 45 | +# 9. Creating a 3D Array with np.ones() |
| 46 | +# Shape (4, 3, 4) = 4 blocks, each with 3 rows and 4 columns |
| 47 | +arr_3d = np.ones((4, 3, 4)) |
| 48 | +# print(arr_3d) |
| 49 | + |
| 50 | +# ----------------------------------------------------- |
| 51 | +# 10. Creating a 4D Array with np.ones() |
| 52 | +# Shape (2, 3, 4, 5): |
| 53 | +# 2 blocks → each has 3 sub-blocks → each has 4 rows → each row has 5 elements |
| 54 | +arr_4d = np.ones((2, 3, 4, 5)) |
| 55 | +# print(arr_4d) |
| 56 | + |
| 57 | +# ----------------------------------------------------- |
| 58 | +# 11. Creating Arrays with Random Module |
| 59 | + |
| 60 | +''' |
| 61 | +np.random.rand(shape): Random floats in range [0.0, 1.0) |
| 62 | +np.random.random(shape): Similar to rand(), returns random floats in [0.0, 1.0) |
| 63 | +np.random.randint(low, high, size): Random integers from low (inclusive) to high (exclusive) |
| 64 | +np.random.choice(list, size): Random samples picked from the provided list |
| 65 | +np.random.seed(value): Sets the seed for reproducibility (same output each run) |
| 66 | +''' |
| 67 | + |
| 68 | +# Random floats between 0 and 1 with shape (2, 3) |
| 69 | +print(np.random.rand(2, 3)) |
| 70 | + |
| 71 | +# Random floats between 0 and 1 with shape (3, 2) |
| 72 | +print(np.random.random((3, 2))) |
| 73 | + |
| 74 | +# Random integers from 1 to 9 (7 values in 1D array) |
| 75 | +print(np.random.randint(1, 10, size=7)) |
| 76 | + |
| 77 | +# Random integers from 1 to 9 in a 3×4 array |
| 78 | +print(np.random.randint(1, 10, (3, 4))) |
| 79 | + |
| 80 | +# Picking 5 random values from the list [10, 20, 30, 40] |
| 81 | +print(np.random.choice([10, 20, 30, 40], size=5)) |
| 82 | + |
| 83 | +# Setting seed ensures consistent random values every run (important for reproducibility) |
| 84 | +np.random.seed(37) |
| 85 | +print(np.random.randint(1, 100, 5)) |
0 commit comments