Advertisement

NumPy Operations: A step-by-step Guide with Examples

 

NumPy Operations


In Numpy provides support for arrays and matrices, along with a variety of mathematical functions to operate on these arrays. Below are some step-by-step examples of common NumPy operations to help you get started:



pip install numpy

import numpy as np


Creating NumPy Arrays



You can create NumPy arrays using various methods, such as from lists or using built-in functions.



import numpy as np


my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print("Array from a Python list:")
print(my_array)


my_array2 = np.array([1, 2, 3, 4, 5])
print("\nArray with specific values:")
print(my_array2)


zeros_array = np.zeros(5)  # Creates an array of 5 zeros
print("\nArray of zeros:")
print(zeros_array)


ones_array = np.ones(3)  # Creates an array of 3 ones
print("\nArray of ones:")
print(ones_array)


range_array = np.arange(0, 10, 2)  # Creates an array from 0 to 10 with a step of 2
print("\nArray with a range of values:")
print(range_array)


linspace_array = np.linspace(0, 1, 5)  # Creates an array of 5 evenly spaced values from 0 to 1
print("\nArray of evenly spaced values:")
print(linspace_array)


identity_matrix_values = np.eye(3)  # Creates a 3x3 identity matrix
print("\nIdentity Matrix:")
print(identity_matrix)


Output:




Array from a Python list:
[1 2 3 4 5]

Array with specific values:
[1 2 3 4 5]

Array of zeros:
[0. 0. 0. 0. 0.]

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

Array with a range of values:
[0 2 4 6 8]

Array of evenly spaced values:
[0.   0.25 0.5  0.75 1.  ]

Identity Matrix:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]




Python's NumPy arrays facilitate a diverse set of mathematical computations:



# Arithmetic operations
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result_add = arr1 + arr2 # Element-wise addition
result_mul = arr1 * arr2 # Element-wise multiplication

print(result_add, result_mul)



[5 7 9] [ 4 10 18]



# Array indexing and slicing
import numpy as np

arr = np.array([103, 114, 125, 135, 142, 152])
element = arr[2] # Accessing a single element
subarray = arr[1:4] # Slicing a subarray
print(element, subarray)



125 [114 125 135]



import numpy as np


arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3) # Reshape into a 2x3 array
print(reshaped)



[[1 2 3]
 [4 5 6]]


NumPy Functions



NumPy offers an extensive selection of mathematical functions that enable operations on arrays:



# Mathematical functions
import numpy as np 

arr = np.array([1, 2, 3, 4, 5])

mean = np.mean(arr) # Calculate the mean
print("\nCalculate the mean:")
print(mean)

std_dev = np.std(arr) # Calculate the standard deviation
print("\nCalculate the standard deviation:")
print(std_dev)


maxm_values = np.max(arr) # Find the maximum value
print("\nFind the maximum value:")
print(maxm_values)

min_values = np.min(arr) # Find the minimum value
print("\nFind the minimum value:")
print(min_values)

# Element-wise functions
squared = np.square(arr) # Square each element
print("\nSquare each element:")
print(squared)


Output:




Calculate the mean:
3.0

Calculate the standard deviation:
1.4142135623730951

Find the maximum value:
5

Find the minimum value:
1

Square each element:
[ 1  4  9 16 25]



These are some basic steps and operations you can perform with NumPy. NumPy is a vast library with many more features, so it's a good idea to consult the documentation and explore its capabilities further as you become more comfortable with it. Just copy the code and use it .Happy Coding..!