NumPy Creating Arrays

NumPy Creating Arrays
In NumPy Creating Arrays is the first and most important step because almost all operations are performed on arrays. NumPy provides multiple ways to create arrays depending on your use case.
Before starting, always import NumPy:
1. Creating an Array from a Python List
1D Array
2D Array (Matrix)
3D Array
2. Creating Arrays with Default Values
np.zeros() – Array filled with zeros
np.ones() – Array filled with ones
np.full() – Array filled with a specific value
3. Creating Arrays with a Range of Numbers
np.arange() – Similar to Python range()
np.linspace() – Evenly spaced values
👉 Best for scientific & mathematical calculations
4. Creating Identity & Diagonal Matrices
np.eye() – Identity matrix
np.diag() – Diagonal matrix
5. Creating Random Arrays
np.random.rand() – Random values (0 to 1)
np.random.randn() – Random values (normal distribution)
np.random.randint() – Random integers
6. Creating Arrays Like Another Array
np.zeros_like()
np.ones_like()
7. Creating an Empty Array
np.empty() (contains garbage values)
⚠️ Use only when performance is critical
8. Checking Array Properties
Summary Table
| Function | Purpose |
|---|---|
np.array() | Create from list |
np.zeros() | Zeros array |
np.ones() | Ones array |
np.full() | Fixed value |
np.arange() | Range |
np.linspace() | Even spacing |
np.eye() | Identity matrix |
np.random.* | Random arrays |
