Python Assignment 1
1. Program to find maximum value and minimum value form the
given matrix
import numpy as np
# Create a sample matrix
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Find the maximum and minimum values
max_value = np.max(matrix)
min_value = np.min(matrix)
print(f"Maximum value: {max_value}")
print(f"Minimum value: {min_value}")
Maximum value: 9
Minimum value: 1
2. Program to perform following operations on the given matrix
a. Find the number of rows and columns of a given matrix using NumPy b. Find the sum of values in a matrix
#a. Find the number of rows and columns of a given matrix using NumPy:
import numpy as np
# Sample matrix
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Find number of rows and columns
rows, cols = matrix.shape
print(f"Number of rows: {rows}")
print(f"Number of columns: {cols}")
Number of rows: 2
Number of columns: 3
# b. Find the sum of values in a matrix:
import numpy as np
# Sample matrix
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Sum of all values
matrix_sum = np.sum(matrix)
print(f"Sum of all values in the matrix: {matrix_sum}")
Sum of all values in the matrix: 21
3. Program to perform the following operations on a 3x3 matrix:
#a. Addition of two matrices:
import numpy as np
# Define two 3x3 matrices
matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
# Addition of matrices
result_add = np.add(matrix1, matrix2)
print("Addition of two matrices:")
print(result_add)
Addition of two matrices:
[[10 10 10]
[10 10 10]
[10 10 10]]
#b. Subtraction of two matrices:
import numpy as np
# Define two 3x3 matrices
matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
# Subtraction of matrices
result_sub = np.subtract(matrix1, matrix2)
print("Subtraction of two matrices:")
print(result_sub)
Subtraction of two matrices:
[[-8 -6 -4]
[-2 0 2]
[ 4 6 8]]
#c. Multiplication of two matrices:
import numpy as np
# Define two 3x3 matrices
matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
# Matrix multiplication
result_mult = np.dot(matrix1, matrix2)
print("Multiplication of two matrices:")
print(result_mult)
Multiplication of two matrices:
[[ 30 24 18]
[ 84 69 54]
[138 114 90]]
4. Program to perform the following operations on a 1D array:
#a. Count the frequency of unique values in NumPy array:
import numpy as np
# Sample 1D array
arr = np.array([1, 2, 2, 3, 3, 3, 4])
# Count unique values
unique, counts = np.unique(arr, return_counts=True)
print("Frequency of unique values:")
for value, count in zip(unique, counts):
print(f"{value}: {count}")
Frequency of unique values:
1: 1
2: 2
3: 3
4: 1
#b. Convert the matrix into a list:
import numpy as np
# Sample matrix
matrix = np.array([[1, 2], [3, 4]])
# Convert matrix to list
matrix_list = matrix.tolist()
print(f"Matrix as a list:",matrix_list)
Matrix as a list: [[1, 2], [3, 4]]
#c. Replace negative value with zero in NumPy array:
import numpy as np
# Sample array
arr = np.array([-1, 2, -3, 4, -5])
# Replace negative values with zero
arr[arr < 0] = 0
print(f"Array after replacing negative values with zero: {arr}")
Array after replacing negative values with zero: [0 2 0 4 0]
5. Program to perform statistical operations on the given array:
#a. Compute the median of the flattened NumPy array:
import numpy as np
# Sample array
arr = np.array([[1, 2], [3, 4]])
# Flatten and compute the median
median_value = np.median(arr)
print(f"Median of the flattened array: {median_value}")
Median of the flattened array: 2.5
#b. Find Mean of Numpy Array:
import numpy as np
# Sample array
arr = np.array([1, 2, 3, 4, 5])
# Mean of the array
mean_value = np.mean(arr)
print(f"Mean of the array: {mean_value}")
Mean of the array: 3.0
#c. Compute the variance of the NumPy array:
import numpy as np
# Sample array
arr = np.array([1, 2, 3, 4, 5])
# Variance of the array
variance_value = np.var(arr)
print(f"Variance of the array: {variance_value}")
Variance of the array: 2.0
#d. Compute the standard deviation of the NumPy array:
import numpy as np
# Sample array
arr = np.array([1, 2, 3, 4, 5])
# Standard deviation of the array
std_deviation_value = np.std(arr)
print(f"Standard deviation of the array: {std_deviation_value}")
Standard deviation of the array: 1.4142135623730951
6. Create a 5X2 integer array from a range between 100 to 200
such that the difference between each element is 10:
import numpy as np
# Create a range from 100 to 200 with step size of 10
arr = np.linspace(100, 200, num=10, endpoint=False).astype(int).reshape(5, 2)
print(f"5x2 array with elements from 100 to 200 with a difference of 10:")
print(arr)
5x2 array with elements from 100 to 200 with a difference of 10:
[[100 110]
[120 130]
[140 150]
[160 170]
[180 190]]
7. Return array of item by taking the third column from all rows:
import numpy as np
# Given sample array
sampleArray = np.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]])
# Extract the third column (index 2)
third_column = sampleArray[:, 2]
print(f"Third column from all rows: {third_column}")
Third column from all rows: [33 66 99]
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js