011. np.zeros and np.ones
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
These functions create arrays filled with 0 or 1. You must pass the desired shape as a tuple.
```python
# 2 rows, 3 columns
zeros = np.zeros((2, 3))
ones = np.ones((4, 4))
`
These are commonly used in machine learning to initialize bias vectors or placeholder masks before processing data.
022. Sequences: arange vs linspace
np.arange(start, stop, step) works exactly like Python's range() but returns a NumPy array. It omits the stop value.
np.linspace(start, stop, num) generates num evenly spaced samples between start and stop. Unlike arange, it INCLUDES the stop value by default. This is the go-to function for plotting mathematical graphs in Matplotlib.
033. Memory Allocation and Identity
np.empty(shape) allocates memory for an array but does not initialize it. It will contain whatever random data was left in that memory sector. It's marginally faster than zeros() if you plan to overwrite every single element immediately.
np.eye(N) creates an NxN identity matrix (1s on the diagonal, 0s elsewhere), heavily used in Linear Algebra operations.
?Frequently Asked Questions
What is the difference between np.arange and np.linspace?
Use `np.arange` when you know the exact STEP size between values (e.g., skip by 2). Use `np.linspace` when you know the total NUMBER of values you want (e.g., exactly 100 points), and let NumPy calculate the step size for you.
Why does np.empty() return random numbers?
`np.empty()` does not 'clean' the memory before assigning it to the array. The random numbers you see are just garbage data that previously occupied that RAM space. It's built for raw allocation speed.
Can I create arrays with a specific data type?
Yes! All these generator functions accept a `dtype` argument. For example, `np.zeros((2,2), dtype=int)` will create an array of integer zeros instead of the default float zeros.
