NumPy: Basics
✕Introduction to NumPy
- Stands for Numerical Python.
- Used in Data Analysis, ML, Image Processing etc.
- Main data structure is
ndarraycontaining elements of same data type. - We can think 1D numpy array as
List, 2D asMatrixand 3D array asCube. - Very fast due to optimized C and Fortran code under the hood.
- Supports vectorized operations, broadcasting
- Has pre-built functions for linear algebra, statistics, and more.
Creating NumPy Arrays
- Can be created from Python
list/tupleor NumPy functions. my_list = [1, 2, 3]my_array = np.array(my_list)# ornp.asarray(my_list)print(my_list)print(my_array)print(type(my_list))print(type(my_array))my_tuple = [(4, 5, 6), (7, 8, 0)]my_array2 = np.array(my_tuple, dtype=float)# specify data typeprint(my_array2.shape)print(my_array2.dtype)print(my_array2.ndim)
Examples:
Indexing and Slicing
- Similar to Python lists but supports multi-dimensional indexing and slicing.
arr_1d = np.array([10, 20, 30, 40, 50])print(arr_1d[0])# 1st elementarr_1d[-1] = 80# Reassign Last elementprint(arr_1d[1:4])# Slice from index 1 to 3print(arr_1d[::2])# Every 2nd elementmy_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])print(my_array[0])# First rowprint(my_array[1][2])# ormy_array[1, 2]Elements at 2nd row and 3rd columnprint(arr_1d[1:3])# From index 1 to 2 (exclusive)print(arr_1d[1:])# From index 1 to endprint(arr_1d[:3])# From start to index 2 (exclusive)print(my_array[0:2, 1:3])# Slice 1st two rows and last two columnsprint(my_array[:, 0])# All rows, 1st columnprint(my_array[1, :])# 2nd row, all columnsprint(my_array[::2, ::2])# Every 2nd row and column
Indexing Examples:
Slicing Examples:
Adding Elements to NumPy Arrays
- Can be done using
np.insertandnp.appendfunctions. - np.insert => Add element at specific index, np.append => Add element at end.
arr = np.array([1, 2, 3])new_arr = np.insert(arr, 1, 10)# Insert 10 at index 1, shifts other rightprint(new_arr)# Output: [1, 10, 2, 3]arr_2d = np.array([[1, 2], [3, 4]])new_arr_2d = np.append(arr_2d, [[5, 6]], axis=0)# Append as new rowprint(new_arr_2d)arr_2d = np.array([[1, 2], [3, 4]])new_arr_2d = np.append(arr_2d, [[5], [6]], axis=1)# Append as new columnprint(new_arr_2d)arr = np.array([1, 2, 3])new_arr = np.append(arr, 4)# Append 4 at the endprint(new_arr)# Output: [1, 2, 3, 4]arr_2d = np.array([[1, 2], [3, 4]])new_arr_2d = np.append(arr_2d, [[5, 6]], axis=0)# Append as new rowprint(new_arr_2d)arr_2d = np.array([[1, 2], [3, 4]])new_arr_2d = np.append(arr_2d, [[5], [6]], axis=1)# Append as new columnprint(new_arr_2d)
Insert:
Append:
Removing Elements from NumPy Arrays
- Can be done using
np.deletefunction. arr = np.array([1, 2, 3, 4])new_arr = np.delete(arr, 1)# Delete element at index 1print(new_arr)# Output: [1, 3, 4]arr_2d = np.array([[1, 2], [3, 4], [5, 6]])new_arr_2d = np.delete(arr_2d, 0)# Delete first element and flattenprint(new_arr_2d)arr_2d = np.array([[1, 2], [3, 4], [5, 6]])new_arr_2d = np.delete(arr_2d, 0, axis=0)# Delete first rowprint(new_arr_2d)arr_2d = np.array([[1, 2], [3, 4], [5, 6]])new_arr_2d = np.delete(arr_2d, 1, axis=1)# Delete second column
Examples:
Vectorized Operations in NumPy
- Vectorized operations perform element-wise operations on arrays without loops.
- Common Operations:
+,-,*,/,**,//,%,<,<=,>,>=,==,!=,@(matrix multiplication). arr1 = np.array([1, 2, 3])arr2 = np.array([4, 5, 6])result = arr1 + arr2# Element-wise addition.print(result)# Output: [5, 7, 9]arr_1_2d = np.array([[1, 2], [3, 4]])arr_2_2d = np.array([[5, 6], [7, 8]])result = arr_1_2d * arr_2_2d# Element-wise multiplicationprint(result)# Output: [[5, 12], [21, 32]]mat_result = arr_1_2d @ arr_2_2d# Matrix multiplicationprint(mat_result)# Output: [[19, 22], [43, 50]]
Examples:
Broadcasting in NumPy
- Allows operations on arrays of different shapes and sizes.
arr1 = np.array([[1, 2], [3, 4]])result = arr1 + 10# Broadcasts scalar 10 to all elementsprint(result)# Output: [[11, 12], [13, 14]]arr1 = np.array([[1, 2], [3, 4]])arr2 = np.array([10, 20])result = arr1 + arr2# Broadcasts arr2 across rowsprint(result)# Output: [[11, 22], [13, 24]]arr1 = np.array([[1, 2], [3, 4]])arr2 = np.array([[10], [20]])result = arr1 + arr2# Broadcasts arr2 across columnsprint(result)# Output: [[11, 12], [23, 24]]
Examples:
Mathematical Functions using NumPy
- NumPy provides a wide range of mathematical functions that can be applied to arrays.
- These functions operate element-wise and are optimized for performance.
- For
a1 = np.array([2, 5.2, 8.7]), numerical functions can be applied as follows:
Examples of Common Mathematical Functions in NumPy:
| Function | Description | Example |
|---|---|---|
np.sqrt | Square root of each element in the array. | np.sqrt(a1) => [1.41, 2.28, 2.95] |
np.power | First array elements raised to powers from second array. | np.power(a1, 2) => [4, 27.04, 75.69] |
np.sin | Trigonometric sine of each element in the array. | np.sin(a1) => [0.91, -0.88, 0.67] |
np.arcsin | Inverse sine of each element in the array. | np.arcsin(a1) => [nan, nan, nan] |
np.floor | Floor of each element in the array. | np.floor(a1) => [2.0, 5.0, 8.0] |
np.ceil | Ceiling of each element in the array. | np.ceil(a1) => [2.0, 6.0, 9.0] |
np.round | Round each element in the array to the nearest integer. | np.round(a1) => [2.0, 5.0, 9.0] |
np.abs | Absolute value of each element in the array. | np.abs(a1) => [2.0, 5.2, 8.7] |
np.degrees | Convert angles from radians to degrees. | np.degrees(a1) => [114.59, 298.20, 498.24] |
np.radians | Convert angles from degrees to radians. | np.radians(a1) => [0.03, 0.09, 0.15] |
Examples for applying mathematical functions in NumPy
Apply string functions using NumPy
- NumPy provides a set of vectorized string methods that can be applied to arrays of strings.
- Accessed through the
np.charmodule and operate element-wise on string arrays. - For
s1 = np.array(["hello ", " world", "data science"]), string functions can be applied as:
Examples of Common String Functions in NumPy:
| Function | Description | Example |
|---|---|---|
np.char.upper | Convert all strings to uppercase. | np.char.upper(s1) => ["HELLO ", " WORLD", "DATA SCIENCE"] |
np.char.lower | Convert all strings to lowercase. | np.char.lower(s1) => ["hello ", " world", "data science"] |
np.char.title | Convert all strings to title case. | np.char.title(s1) => ["Hello ", " World", "Data Science"] |
np.char.strip | Remove whitespace from all strings. | np.char.strip(s1) => ["hello", "world", "data science"] |
np.char.startswith | Check if each string starts with given prefix. | np.char.startswith(s1, "h") => [True, False, False] |
np.char.endswith | Check if each string ends with given suffix. | np.char.endswith(s1, "e") => [False, False, True] |
np.char.replace | Replace substring with another. | np.char.replace(s1, " ", "_") => ["hello_", "_world", "data_science"] |
np.char.split | Split string by the specified separator. | np.char.split(s1) => [["hello"], ["world"], ["data", "science"]] |
np.char.join | Join elements of an array with a specified separator. | np.char.join("-", s1) => ["h-e-l-l-o- ", " -w-o-r-l-d", "d-a-t-a- -s-c-i-e-n-c-e"] |
np.char.find | Find the lowest index of the substring. | np.char.find(s1, "o") => [4, 2, -1] |
np.char.count | Count occurrences of a substring. | np.char.count(s1, "o") => [1, 1, 0] |
np.char.str_len | Return the length of each string. | np.char.str_len(s1) => [6, 6, 12] |
np.vectorize(func) | Apply generic function to string. | np.vectorize(func)(s1) => Applies func to each element in s1 |
Examples for applying string functions in NumPy
Aggregate Functions in NumPy
- NumPy provides aggregate functions that operate on arrays to compute summary statistics.
- These functions can be applied to the entire array or along specific axes.
- For
a2 = np.array([[1, 2], [3, 4]]), aggregate functions can be applied as:
Examples of Common Aggregate Functions in NumPy:
| Function | Description | Example |
|---|---|---|
np.sum | Sum of array elements. | np.sum(a2) => 10, np.sum(a2, axis=0) => [4, 6] |
np.mean | Mean of array elements. | np.mean(a2) => 2.5, np.mean(a2, axis=1) => [1.5, 3.5] |
np.median | Median of array elements. | np.median(a2) => 2.5 |
np.std | Standard deviation of array elements. | np.std(a2) => 1.118 |
np.var | Variance of array elements. | np.var(a2) => 1.25 |
np.min | Minimum value in the array. | np.min(a2) => 1 |
np.max | Maximum value in the array. | np.max(a2) => 4 |
np.argmin | Index of minimum value in the array. | np.argmin(a2) => 0 |
np.argmax | Index of maximum value in the array. | np.argmax(a2) => 3 |
np.clip | Clip (limit) the values in the array. | np.clip(a2, 2, 3) => [[2, 2], [3, 3]] |
np.cumsum | Cumulative sum of array elements. | np.cumsum(a2) => [1, 3, 6, 10] |
np.cumprod | Cumulative product of array elements. | np.cumprod(a2) => [1, 2, 6, 24] |
np.cov | Covariance of two arrays. | np.cov(a2, a2) |
np.ptp | Peak to peak (max - min) of array elements. | np.ptp(a2) => 3 |
np.quantile | Quantiles of the array. | np.quantile(a2, 0.5) => 2.5 |
np.average | Weighted average of array elements. | np.average(a2, weights=[0.25, 0.75]) => 3.25 |
Examples for applying aggregate functions in NumPy
NumPy Constants
- NumPy provides several constants that are useful in mathematical computations.
Common NumPy Constants:
| Constant | Description | Value |
|---|---|---|
np.pi | The mathematical constant π (pi). | 3.141592653589793 |
np.inf | Positive infinity. | inf |
-np.inf | Negative infinity. | -inf |
np.nan | Not a Number (NaN) value. | nan |
Examples of commonly used constants in NumPy
