NumPy Special Arrays Explained
This guide focuses on how and why we use NumPy array creation functions — not just syntax.
🔹 Step 1: Import NumPy
import numpy as np
Important:
We use np as a shortcut because NumPy functions are used frequently.
🔹 1. Zero Array → np.zeros()
ar_zero = np.zeros((3)) print(ar_zero) print(ar_zero.ndim)
Output:
[0. 0. 0.] 1
✅ Explanation:
(3)means 3 elements → 1D array- All values are automatically set to 0
.ndimtells dimension (1 here)
💡 Real Scenario:
Suppose you are building a system to track daily sales for 3 days. Initially, no sales → start with 0 values.
⚠️ Important Points:
- Always pass shape inside tuple
(3)→ 1D,(3,4)→ 2D
🔸 Multi-Dimensional Example
ar_zero = np.zeros((3,4)) print(ar_zero)
Explanation:
- 3 rows and 4 columns
- Used when working with tables or grids
💡 Real Scenario:
Think of a classroom attendance sheet → 3 students, 4 subjects → all values start at 0.
🔹 2. Ones Array → np.ones()
ar_ones = np.ones((3)) print(ar_ones)
Output:
[1. 1. 1.]
✅ Explanation:
- Creates array filled with 1
- Structure same as zeros
💡 Real Scenario:
Used when all items have default value = 1 Example: Assign equal weight to 3 features in ML
⚠️ Important:
- Data type is float by default →
1.not1
🔹 3. Empty Array → np.empty()
ar_empt = np.empty((3)) print(ar_empt)
✅ Explanation:
- Does NOT initialize values
- Returns whatever is in memory
💡 Real Scenario:
When you need speed and will overwrite values immediately.
⚠️ Important Concept:
If you previously created a ones array, memory may still contain [1. 1. 1.].
So empty array may show same values → but it is NOT actual data.
🔥 Hack:
np.empty()is faster thannp.zeros()- Use only when you're sure you'll assign values
🔹 4. Range Array → np.arange()
ar_range = np.arange(3,7) print(ar_range)
Output:
[3 4 5 6]
✅ Explanation:
- Starts from 3 and ends at 6 (7 excluded)
- Works like Python range but returns array
💡 Real Scenario:
Generating IDs → Customer IDs from 1000 to 1010
⚠️ Important:
- End value is always excluded
🔹 5. Diagonal Matrix → np.eye()
ar_diag = np.eye(4,4) print(ar_diag)
✅ Explanation:
- Diagonal elements = 1
- Rest = 0
💡 Real Scenario:
Used in Machine Learning & Linear Algebra (Identity Matrix)
⚠️ Important:
- Can create non-square matrices too
np.eye(3,4)
🔹 6. Linspace → np.linspace()
ar_linespace = np.linspace(1,10,num=5) print(ar_linespace)
Output:
[ 1. 3.25 5.5 7.75 10. ]
✅ Explanation:
- Divides range into equal parts
- Includes both start and end
💡 Real Scenario:
Used in plotting graphs → evenly spaced X-axis values
⚠️ Important Difference:
- arange() → step-based
- linspace() → count-based
🔥 Final Key Takeaways
- zeros → initialize values
- ones → default weight/value
- empty → fast but risky
- arange → sequences
- eye → identity matrix
- linspace → equal spacing
🚀 Pro Tips
- Always check dimension using
.ndim - Use tuple correctly → common beginner mistake
- Prefer linspace for graphs
- Use empty only for performance optimization
❌ Common Mistakes
- Thinking empty = zeros
- Forgetting end is excluded in arange
- Confusing shape format
🔚 Conclusion
These functions are essential tools for working with NumPy efficiently.
Once you master them, handling data becomes much easier ⚡