| Keyword |
Description |
Syntax |
Example |
| Import NumPy |
Imports the NumPy library, usually aliased as np. |
import numpy as np |
import numpy as np |
| np.array() |
Creates a NumPy array from a Python list (1D or Multi-dimensional). |
np.array(list) |
# Sepal lengths for 5 flowers sepal_len = np.array([5.1, 4.9, 4.7, 4.6, 5.0]) |
| np.zeros() |
Creates an array filled with zeros. Useful for initializing weights. |
np.zeros(shape) |
# Initialize placeholders zeros = np.zeros(5) |
| np.ones() |
Creates an array filled with ones. |
np.ones(shape) |
ones = np.ones(5) |
| np.empty() |
Creates an array without initializing entries (contains garbage values). |
np.empty(shape) |
empty = np.empty(3) |
| np.arange() |
Creates an array with a sequence of numbers (start, stop, step). |
np.arange(start, stop, step) |
# Create index IDs 0, 2, 4, 6, 8 ids = np.arange(0, 10, 2) |
| Keyword |
Description |
Syntax |
Example |
| shape |
Returns a tuple representing the dimensions of the array (rows, cols). |
arr.shape |
# (150, 4) for 150 flowers, 4 features print(data.shape) |
| ndim |
Returns the number of dimensions (axes). |
arr.ndim |
print(data.ndim) # 2 |
| size |
Returns the total number of elements in the array. |
arr.size |
print(data.size) # 600 |
| dtype |
Returns the data type of the elements (e.g., float64, int32). |
arr.dtype |
print(data.dtype) # float64 |
| Keyword |
Description |
Syntax |
Example |
| Arithmetic (+ - * /) |
Performs element-wise addition, subtraction, multiplication, and division. |
arr1 + arr2 |
# Convert cm to mm sepal_mm = sepal_cm * 10 |
| np.dot() |
Calculates the dot product of two arrays (matrix multiplication). |
np.dot(a, b) |
# Weighted sum of features score = np.dot(features, weights) |
| np.sqrt() |
Computes the square root of each element. |
np.sqrt(arr) |
variance = np.array([0.25, 0.16]) std_dev = np.sqrt(variance) |
| Trig Functions |
Sine, Cosine, etc. (Inputs should often be in radians). |
np.sin(arr) |
np.sin(np.radians(angles)) |
| Aggregation |
Sum, min, max, mean of array elements. |
arr.sum(), arr.mean() |
total_len = sepal_len.sum() avg_len = sepal_len.mean() |
| Keyword |
Description |
Syntax |
Example |
| 1D Indexing |
Access specific element by integer index (0-based). |
arr[i] |
# First flower's length print(sepal_len[0]) |
| 1D Slicing |
Access a subset of the array [start:stop:step]. |
arr[start:stop] |
# First 3 flowers print(sepal_len[0:3]) |
| 2D Indexing |
Access element at [row, column]. |
arr[row, col] |
# 2nd flower (row 1), 1st feature (col 0) val = iris_data[1, 0] |
| 2D Slicing |
Slice rows and columns simultaneously. |
arr[r_start:r_end, c_start:c_end] |
# First 2 flowers, first 2 features subset = iris_data[:2, :2] |