Skip to content

Commit bdc27ef

Browse files
Updated Creation folder with more examples and a markdown fike
1 parent 97cb6e0 commit bdc27ef

File tree

2 files changed

+166
-22
lines changed

2 files changed

+166
-22
lines changed

Creation/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# NumPy Array Creation Guide
2+
3+
This guide provides examples for creating NumPy arrays of various dimensions, initializing arrays with specific values, generating sequences, identity matrices, and using random number generation.
4+
5+
---
6+
7+
## 1. Creating Arrays
8+
9+
### 1.1 One-Dimensional Array
10+
```python
11+
one_d_array = np.array([1, 2, 3, 4])
12+
```
13+
14+
### 1.2 Two-Dimensional Array (Matrix)
15+
```python
16+
two_d_array = np.array([[1, 2, 3, 4],
17+
[5, 6, 7, 8]])
18+
```
19+
20+
---
21+
22+
## 2. Arrays with Constant Values
23+
24+
### 2.1 Zeros
25+
```python
26+
zero_array_1d = np.zeros(3) # [0. 0. 0.]
27+
zero_array_2d = np.zeros((3, 4)) # 3x4 matrix of zeros
28+
```
29+
30+
### 2.2 Ones
31+
```python
32+
ones_array = np.ones((2, 3)) # 2x3 matrix of ones
33+
```
34+
35+
### 2.3 Filled with a Specific Value
36+
```python
37+
filled_array = np.full((2, 3), 5) # 2x3 matrix filled with 5
38+
```
39+
40+
---
41+
42+
## 3. Sequence Arrays
43+
44+
### 3.1 Using `np.arange()`
45+
```python
46+
sequence_array = np.arange(1, 11, 2) # [1 3 5 7 9]
47+
```
48+
49+
### 3.2 Identity Matrix
50+
```python
51+
identity_matrix = np.eye(5) # 5x5 Identity matrix
52+
```
53+
54+
---
55+
56+
## 4. Multidimensional Arrays
57+
58+
### 4.1 Three-Dimensional Array
59+
```python
60+
# Shape: 4 blocks, each with 3 rows and 4 columns
61+
arr_3d = np.ones((4, 3, 4))
62+
```
63+
64+
### 4.2 Four-Dimensional Array
65+
```python
66+
# Shape: 2 blocks × 3 sub-blocks × 4 rows × 5 elements
67+
arr_4d = np.ones((2, 3, 4, 5))
68+
```
69+
70+
You can continue this pattern to create arrays with even higher dimensions (5D, 6D, etc.).
71+
72+
---
73+
74+
## 5. Arrays with Random Values
75+
76+
### 5.1 Random Float Arrays
77+
```python
78+
np.random.rand(2, 3) # Random floats in [0, 1)
79+
np.random.random((3, 2)) # Same as rand, alternative syntax
80+
```
81+
82+
### 5.2 Random Integers
83+
```python
84+
np.random.randint(1, 10, size=7) # 1D array of 7 random ints [1, 10)
85+
np.random.randint(1, 10, (3, 4)) # 2D array of random ints
86+
```
87+
88+
### 5.3 Random Choice
89+
```python
90+
np.random.choice([10, 20, 30, 40], size=5) # Picks 5 values randomly from list
91+
```
92+
93+
### 5.4 Reproducibility with Seed
94+
```python
95+
np.random.seed(37)
96+
np.random.randint(1, 100, 5) # Same output every time when seed is set
97+
```
98+
99+
Setting a seed ensures your results are reproducible for testing or demonstration purposes.
100+
101+
---
102+
103+
✅ With these methods, you can efficiently create and initialize arrays in NumPy across different shapes, dimensions, and value types!

Creation/creation.py

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,85 @@
11
import numpy as np
22

3-
# ------------------------------------------
3+
# -----------------------------------------------------
44
# 1. Creating a One-Dimensional Array
55
one_d_array = np.array([1, 2, 3, 4])
66
# print(one_d_array)
77

8-
# ------------------------------------------
8+
# -----------------------------------------------------
99
# 2. Creating a Two-Dimensional Array (Matrix)
1010
two_d_array = np.array([[1, 2, 3, 4],
1111
[5, 6, 7, 8]])
1212
# print(two_d_array)
1313

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
1717
# print(zero_array_1d)
1818

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
2222
# print(zero_array_2d)
2323

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
2727
# print(ones_array)
2828

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
3232
# print(filled_array)
3333

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
3837
# print(sequence_array)
3938

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
4442
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

Comments
 (0)