DEV Community

Cover image for NumPy for Machine Learning & Deep Learning
arju10
arju10

Posted on • Edited on

NumPy for Machine Learning & Deep Learning

You can follow github.

Numpy For Machine Learning & Deep Learning

Numpy

Numpy is a python library used to work with arrays and stands for Numarical python. It works on linear algebra, fourier transform, and matrices domain.
Numpy is faster than list because numpy provides array object.

Numpy Functions:

Note: To use numpy always import it.

import numpy as np # Here, numpy is imported as np 
Enter fullscreen mode Exit fullscreen mode

1. zeros(): It creates a array with zeros. Example:

array_zeros = np.zeros((3,3)) print("Array of Zeros: \n", array_zeros) 
Enter fullscreen mode Exit fullscreen mode

Output

Array of Zeros:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

2. ones(): It creates a array with ones. Example:

array_ones = np.ones((2,2)) print("Array of ones: \n",array_ones) 
Enter fullscreen mode Exit fullscreen mode

Output

Array of ones:
[[1. 1.]
[1. 1.]]

3. full(): It creates a array with all elements as 7. Example:

array_full = np.full((2,2),7) print("Array with all elements as 7 : \n",array_full) 
Enter fullscreen mode Exit fullscreen mode

Output

Array with all elements as 7 :
[[7 7]
[7 7]]

4. range(): It creates a array between 0 to 20. Example:

array_range = np.arange(20) print("Array with range of numbers : ",array_range) 
Enter fullscreen mode Exit fullscreen mode

Output

Array with range of numbers :
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

5. random():
Example:

import numpy as np # Create array random_array = np.random.rand(3,3) # Random values in 3 X 3 normal_array = np.random.randn(3,3) # Random values with a normal distribution  print("Randmon Array : \n", random_array) print("\n") print("Normal Array : \n", normal_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Randmon Array :
[[0.14635308 0.23362121 0.28310351]
[0.23209788 0.67298124 0.84145588]
[0.6829577 0.45439367 0.05747398]]

Normal Array :
[[-0.57461744 -0.09078458 -0.50067585]
[ 0.86439072 -1.44615505 0.25666949]
[ 0.01952512 -1.70556208 -0.80816368]]

6. transpose():
Example:

import numpy as np # Create 2D array original_array = np.array([ [1,2], [3,4] ]) # Transpose the array transposed_array = np.transpose(original_array) print("Original Array: \n", original_array) print("Transposed Array: \n", transposed_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Original Array:
[[1 2]
[3 4]]
Transposed Array:
[[1 3]
[2 4]]

7. reshape():
Example:

import numpy as np # Create array original_array = np.array( [1,4,6,7,8,9] ) # Reshaping to different dimensions reshaped_array = original_array.reshape((2,3)) print("Original Array : \n", original_array) print("\n") print("Reshaping Array : \n", reshaped_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Original Array :
[1 4 6 7 8 9]
Reshaping Array :
[[1 4 6]
[7 8 9]]

8. concatenate():
Example:

import numpy as np # Create an array array1 = np.array([1,2,3]) array2 = np.array([4,5,6]) # Concatenating arrays concatenating_array = np.concatenate((array1, array2)) print("Concatenate Array : ", concatenating_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Concatenate Array : [1 2 3 4 5 6]

9. array_split():
Example:

import numpy as np # create an array array = np.array( [1,2,34,4,5,6] ) # Spliting the array into three parts split_arrays = np.array_split(array,3) print("Original Array : ", array) print("Spiting Array : ", split_arrays) 
Enter fullscreen mode Exit fullscreen mode

Output

Original Array : [ 1 2 34 4 5 6]
Spiting Array : [array([1, 2]), array([34, 4]), array([5, 6])]

10. linspace():
Example:

import numpy as np # Generating a linear space array with evenly spaced values linear_space = np.linspace(0,10,5) print("Linear Space : ", linear_space) 
Enter fullscreen mode Exit fullscreen mode

Output

Linear Space : [ 0. 2.5 5. 7.5 10. ]

11. meshgrid():
Example:

import numpy as np # Create arrays x = np.array([1,2,3]) y = np.array([4,5,6]) # Create a meshgrid x,y = np.meshgrid(x,y) print("Meshgrid X : \n",x) print("\n") print("Meshgrid Y : \n",y) 
Enter fullscreen mode Exit fullscreen mode

Output

Meshgrid X :
[[1 2 3]
[1 2 3]
[1 2 3]]

Meshgrid Y :
[[4 4 4]
[5 5 5]
[6 6 6]]

12. clip():
Example:

import numpy as np # Create a array array = np.array([1,2,4,5]) # Clipping values to be between 2 and 4 clipped_array = np.clip(array, 2,4) print("Original Array",array) print("Clipped Array",clipped_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Original Array [1 2 4 5]
Clipped Array [2 2 4 4]

13. flatten():
Example:

import numpy as np # Create a 2D array array_2d = np.array( [ [1,2,3], [4,5,6], [7,8,9] ] ) # Flattening the array flattened_array = array_2d.flatten() print("Flattened Array : ",flattened_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Flattened Array : [1 2 3 4 5 6 7 8 9]

Arithmetic Operations

1. add(): It adds minimum two arrays. Both array size should be same.
2. subtract(): It will minus from one array to another array & Both array size should be same.
3. multiply(): It multiplys between minimum two arrays. Both array size should be same.
4. divide(): It divides an array with another array & Both array size should be same.
Examples:

import numpy as np # Create arrays array1 = np.array([10,20,30]) array2 = np.array([40,50,60]) # Arithmetic Operations added = np.add(array1, array2) subtracted = np.subtract(array1 , array2) multiplied = np.multiply(array1, array2) divided = np.divide(array1, array2) print("Added: ", added) print("Subtracted :", subtracted) print("Multiplied: ", multiplied) print("Divided: ", divided) 
Enter fullscreen mode Exit fullscreen mode

Output

Added: [50 70 90]
Subtracted : [-30 -30 -30]
Multiplied: [ 400 1000 1800]
Divided: [0.25 0.4 0.5 ]

5. power(): A power is a number multiplied by itself a certain number of times, represented by an exponent. For example:
10^2 means 10 multiplied by itself, which is (10 X 10 = 100).
6. sqrt(): A square root of a number is a value that, when multiplied by itself, gives the original number. It is represented by the square root symbol (√).

For example:

  • √121 = 11

In this case, 11 × 11 = 121, so the square root of 121 is 11.

Properties of Square Roots

  1. The square root of a positive number has both a positive and a negative solution (e.g., √25 = ±5), but typically, the positive root is used.
  2. The square root of 0 is 0.
  3. Negative numbers do not have real square roots (they result in imaginary numbers).

Examples :

  • √144 = 12
  • √64 = 8
  • √49 = 7
import numpy as np # Create array array = np.array([2,6,4,3,7]) # Power & Square Root squared = np.power(array, 2) sqrt = np.sqrt(array) print("Squared Array :", squared) print("Square root of array :", sqrt) 
Enter fullscreen mode Exit fullscreen mode

Output

Squared Array :[ 4 36 16 9 49]
Square root of array : [1.41421356 2.44948974 2. 1.73205081 2.64575131]

Trigometric Functions

1. sin():
2. cos():
3. tan():

Example:

import numpy as np # Create array angles = np.array([0, np.pi/2, np.pi]) # Trigometric functions sin_array = np.sin(angles) cos_array = np.cos(angles) tan_arraY = np.tan(angles) print("Sins of angles: ", sin_array) print("Cos of angles : ", angles) print("Tan of angles : ", angles) 
Enter fullscreen mode Exit fullscreen mode

Output

Sins of angles: [0.0000000e+00 1.0000000e+00 1.2246468e-16]
Cos of angles : [0. 1.57079633 3.14159265]
Tan of angles : [0. 1.57079633 3.14159265]

Exponential & Logarithm Function

1. exp():
2. log():

Example:

import numpy as np # Create an array array = np.array([10,20,30]) # Exponential & Logarithm functions exp_array = np.exp(array) log_array = np.log(array) print("Exponential of Array: ",exp_array) print("Logarithm of Array: ",log_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Exponential of Array: [2.20264658e+04 4.85165195e+08 1.06864746e+13]

Logarithm of Array: [2.30258509 2.99573227 3.40119738]

Statistical Functions

1. mean():
2. median():
3. std():
4. var():

import numpy as np # Create array array = np.array( [1,2,3,4,5] ) # Calculating statistical functions mean = np.mean(array) median = np.median(array) std_dev = np.std(array) variance = np.var(array) print("Mean : ", mean) print("Median : ", median) print("Standard deviation : ", std_dev) print("Variance : ", variance) 
Enter fullscreen mode Exit fullscreen mode

Output
Mean : 3.0
Median : 3.0
Standard deviation : 1.4142135623730951
Variance : 2.0

Unique Elements and Their Counts

1. unique(): It finds the unique elements from array. If pass the parameterretuen_counts = True, it will show the number of unique elements from an array.

Example:

import numpy as np # Create an array array = np.array( [14,1,54,5,125,214,1,2,43,4,2,2,41,14,54,214,1] ) # Finding Unique Element and their counts unique_element , counts= np.unique(array, return_counts=True) print("Unique Elements : ", unique_element) print("Counts of Unique Elements: ", counts) 
Enter fullscreen mode Exit fullscreen mode

Output
Unique Elements : [ 1 2 4 5 14 41 43 54 125 214]
Counts of Unique Elements: [3 3 1 1 2 1 1 2 1 2]

Cumulative Sum and Product

1. cumsum():
2. cumprod():

Example:

import numpy as np # Create a array array = np.array([1,2,4,5]) # Calculating cumulative sum and product cumulative_sum = np.cumsum(array) cumulative_product = np.cumsum(array) print("cumulative sum : ", cumulative_sum) print("cumulative product : ", cumulative_product) 
Enter fullscreen mode Exit fullscreen mode

Output

cumulative sum : [ 1 3 7 12]
cumulative product : [ 1 3 7 12]

Broadcasting

Adding a scaler to an array.

Example:

import numpy as np # create array array = np.array( [1,2,3] ) # Braodcasting: Adding a scaler to an array result = array + 10 print("Original Array: ", array) print("Array after broadcasting : ", result) 
Enter fullscreen mode Exit fullscreen mode

Output
Original Array: [1 2 3]
Array after broadcasting : [11 12 13]

Sorting Array sort()

1. sort(): It sorts the array by default ascending order.

import numpy as np # Create array array = np.array( [2,4,5,3,10,7,8] ) # Sort the array sorted_array = np.sort(array,) print("Sorted Arrays: ", sorted_array) 
Enter fullscreen mode Exit fullscreen mode

Output
Sorted Arrays: [ 2 3 4 5 7 8 10]

Array Indexing & Slicing: [row:column]

Basic Indexing and Slicing

import numpy as np # Create a array array = np.array([1,2,4,5]) # Aceesing element by index first_element = array[0] # Aceesing element from the end uing negative inidices last_element = array[-1] second_last_element = array[-2] # Slicing the array slice_array = array[1:4] # Selecting Specific indices selected_elements = array[[1,3]] # Slicing with a step step_slice = array[::2] # Every Second Element  #Modifying values specific indices array[[1,3]] = [100, 200] # Reversing the array using slicing reversed_array = array[::-1] print("First Element: ", first_element) print("Last Element: ", last_element) print("Second Last Element: ", second_last_element) print("Sliced Element: ", slice_array) print("Selected Elements :", selected_elements) print("Sliced Array with Step 2 :", step_slice) print("Modified Array : ",array) print("Reversed Array: ", reversed_array) 
Enter fullscreen mode Exit fullscreen mode

Output

First Element: 1
Last Element: 5
Second Last Element: 4
Sliced Element: [2 4 5]
Selected Elements : [2 5]
Sliced Array with Step 2 : [1 4]
Modified Array : [ 1 100 4 200]
Reversed Array: [5 4 2 1]

2D Array indexing & Slicing

import numpy as np # Create a 2D array array_2d = np.array( [ [1,2,3], [4,5,6], [7,8,9] ] ) # Accessing individual elements element = array_2d[1,2] # Accessing element at row 1, column 2  # Slicing Row row_slice = array_2d[0, : ] # First row  # Slicing Column column_slice = array_2d[:, 1] # Second Column  # Using arrays for indexing rows = np.array([0,1,2]) cols = np.array([2,1,0]) indexed_elements = array_2d[rows, cols] # Multi Dimensional Slicing sub_array = array_2d[1:, 1:3] # Selecting specific rows and columns using slices selected_slice = array_2d[1:, :2] # Slicing every second element in rows and columns [Advance Slicing with Step] step_sliced_element = array_2d[::2, ::2] print("Element at (1,2) : ", element) print("First Row : ", row_slice) print("Second Column: ", column_slice) print("Indexed Elements : ", indexed_elements) print("Sub Array (Slicing Rows & Columns) : \n", sub_array) print("Selected Slice : \n", selected_slice) print("Advanced sliced array with step \n", step_sliced_element) 
Enter fullscreen mode Exit fullscreen mode

Output
Element at (1,2) : 6

First Row : [1 2 3]

Second Column: [2 5 8]

Indexed Elements : [3 5 7]
Sub Array (Slicing Rows & Columns) :
[[5 6]
[8 9]]

Selected Slice :
[[4 5]
[7 8]]

Advanced sliced array with step
[[1 3]
[7 9]]

3D Array Indexing & Slicing

import numpy as np # Create a 3D array array_3d = np.array( [ [ [10,20], [30,40] ], [ [50,60], [70,80] ] ] ) # Accessing elements in a 3D Array element = array_3d[1,0,1] # Accessing element at [1, 0 , 1]  # Slicing in 3D slice_3D = array_3d[:, 0, : ] # First row  print("Element at (1,0,1): ", element) print("\n") print("Sliced 3D Array : \n", slice_3D) 
Enter fullscreen mode Exit fullscreen mode

Output
Element at (1,0,1): 60
Sliced 3D Array :
[[10 20]
[50 60]]

Multi Dimensional Array indexing with mixed types

import numpy as np # Create 3D Array array_3D = np.array([ [[1,2], [3,4]], [[5,6], [7,8]], [[9,10], [11,12]] ] ) # Mixing slicing with direct indexing mixed_index = array_3D[1:, 0, 1] print("Mixed type indexing result: ", mixed_index) 
Enter fullscreen mode Exit fullscreen mode

Output
Mixed type indexing result: [ 6 10]

Boolean Indexing

import numpy as np # Create a array array = np.array( [10,20,30,40,60,80] ) # Boolean Indexing greater_than_20 = array[array > 20] # Boolean Indexing even_numbers = array[array %2==0] print("Original : ", array) print("Elements greater than 20 : ",greater_than_20) print("Even Numbers : ",even_numbers) 
Enter fullscreen mode Exit fullscreen mode

Output
Original : [10 20 30 40 60 80]
Elements greater than 20 : [30 40 60 80]
Elements greater than 20 : [10 20 30 40 60 80]

Fancy Indexing

import numpy as np # Create a array array = np.array([1,2,4,5]) # Selecting Specific indices selected_elements = array[[1,3]] print("Selected Elements :", selected_elements) 
Enter fullscreen mode Exit fullscreen mode

Output
Selected Elements : [2 5]

Combined Boolean and Fancy Indexing

import numpy as np # Creating an array array = np.array([10, 15, 20, 25, 30]) # Boolean condition combined with specific indices result = array[(array > 10) & (array < 30)][[0, 2]] print('Combined Boolean and Fancy Indexing Result:', result) 
Enter fullscreen mode Exit fullscreen mode

Output

Combined Boolean and Fancy Indexing Result: [15 25]

Combined Boolean , Fancy and slice indexing

import numpy as np # Creating a 2D array array_2d = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) # Applying boolean indexing first, then fancy indexing and slicing result = array_2d[array_2d > 30].reshape(-1, 3)[:, [0, 2]] print('Combined Indexing Result:', result) 
Enter fullscreen mode Exit fullscreen mode

Output

Combined Indexing Result:
[[40 60]
[70 90]]

Conditional Indexing

import numpy as np # Create a array array = np.array( [1,2,3,4,5,6] ) # Setting elements that satisfying a condition array[array % 2 == 0] = -1 # Set even numbers to -1  print("Array after conditional indexing : ",array) 
Enter fullscreen mode Exit fullscreen mode

Output
Array after conditional indexing : [ 1 -1 3 -1 5 -1]

Ellipsis Indexing

import numpy as np # Create a 3D array array_3D = np.array( [[ [1,2], [3,4], [5,6], [7,8], [9,10], [11,12] ]] ) # Using Ellipisis to seletect all elements along axes result = array_3D[..., 1] # Selecting the second column in each 2D Slices  print("Result using Ellipsis (...): \n", result) 
Enter fullscreen mode Exit fullscreen mode

Output

Result using Ellipsis (...):
[[ 2 4 6 8 10 12]]


Mask Indexing np.ma

import numpy as np # Create array array = np.array([1,-2,3,-4,5]) # Creating a Mask for positive values mask = array > 0 # Applyinh mask to get only positive values positive_values = array[mask] # Masking elements where values are negative masked_array = np.ma.masked_where(array < 0, array) print("Positive Values : ", positive_values) print("Masked Array with negative values hidden: ", masked_array) 
Enter fullscreen mode Exit fullscreen mode

Output

Positive Values : [1 3 5]
Masked Array with negative values hidden: [1 -- 3 -- 5]

Matrix

Indexing using np.diag() np.triu() np.tril

import numpy as np # Create a square Matrix matrix = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) # Extracting the main diagnal diagnoal_elements = np.diag(matrix) # Extracting the upper triangle (above the main diaginal) upper_triangle = np.triu(matrix) # Extracting the lower triangle (below the main diaginal) lower_triangle = np.tril(matrix) print("Diagnoal Elements: \n", diagnoal_elements) print("Upper Triangle of the Matrix: \n", upper_triangle) print("Lower Triangle of the Matrix: \n", lower_triangle) 
Enter fullscreen mode Exit fullscreen mode

Output

Diagnoal Elements: [1 5 9]
Upper Triangle of the Matrix:
[[1 2 3]
[0 5 6]
[0 0 9]]

Lower Triangle of the Matrix:
[[1 0 0]
[4 5 0]
[7 8 9]]

Indexing using np.wehere()

import numpy as np # Create a array array = np.array( [10,20,30,40,60,80] ) # Using np.where to find indices where elements are greater than 25 indices = np.where(array > 25) # Replacing values based on condition where elements are greater than 25 result = np.where(array > 25, 0, array) print("Indices where elements > 25 : ",indices) print("Values where elements > 25: ",array[indices]) print("Array with Values where elements > 25 replaced with 0: ",result) 
Enter fullscreen mode Exit fullscreen mode

Output

Indices where elements > 25 : (array([2, 3, 4, 5]),)
Values where elements > 25: [30 40 60 80]
Array with Values where elements > 25 replaced with 0: [10 20 0 0 0 0]

Indexing using np.take()

import numpy as np # Create a array array = np.array( [10,20,30,40,60,80] ) # Using np.take to select elements at specific indices indices = [0,2,4] result = np.take(array, indices) print("Selected Elements using np.take", result) 
Enter fullscreen mode Exit fullscreen mode

Output

Selected Elements using np.take [10 30 60]

Indexing using advanced meshgrid() **

Example:

import numpy as np # Create arrays x = np.array([1,2,3]) y = np.array([4,5,6]) # Create a meshgrid x,y = np.meshgrid(x,y) indices = np.vstack( [x.ravel(), y.ravel()] ) print("Indices for Meshgrid Advanced Indexing: \n", indices) 
Enter fullscreen mode Exit fullscreen mode

Output

Indices for Meshgrid Advanced Indexing:
[[1 2 3 1 2 3 1 2 3]
[4 4 4 5 5 5 6 6 6]]

#### Multiple condition using np.choose() **

Example:

import numpy as np # Create an array array = np.array([1, 2, 4, 5]) # Extend choices to include placeholder arrays for out-of-bounds indices choices = [array * 2, array + 10, array ** 2, array * 0, array * 0, array * 0] # Use np.choose with the modified choices array result = np.choose(array, choices) print("Result using np.choose:", result) 
Enter fullscreen mode Exit fullscreen mode

Output

Result using np.choose: [11 4 0 0]

Top comments (0)