FDS Rec 9-12
FDS Rec 9-12
REG.NO:
DATE :
EX.NO :
To write a program to find the unique element in an array and count the occurrence.
ALGORITHM:
PROGRAM:
import numpy as np
arr = np.array([1, 2, 3, 2, 3, 4, 5, 5, 6, 3, 2, 1, 4])
unique_elements, counts = np.unique(arr, return_counts=True)
print("Unique elements:", unique_elements)
print("Counts of unique elements:", counts)
for i in range(len(unique_elements)):
print(f"Element {unique_elements[i]} occurs {counts[i]} times")
OUTPUT
Unique elements: [1 2 3 4 5 6]
Counts of unique elements: [2 3 3 2 2 1]
Element 1 occurs 2 times
Element 2 occurs 3 times
Element 3 occurs 3 times
Element 4 occurs 2 times
RESULT:
Thus the program to to find the unique element in an array and count the occurrence was
written and executed successfully.
REG.NO:
DATE:
EX.NO : 07
PROGRAM TO IMPLEMENT MATRIX MULTIPLICATION FOR TWO
DIMENSIONAL ARRAYS USING NUMPY
AIM:
To write a program to perform matrix multiplication for two-dimensional arrays using NumPy.
ALGORITHM:
● Import NumPy library.
● Define two matrices as 2D NumPy arrays.
● Check compatibility: Ensure that the number of columns in the first matrix is equal to the
number of rows in the second matrix.
● Multiply matrices using np.dot(matrix1, matrix2).
● Store the result in a new matrix.
● Display the input matrices and the resultant matrix.
PROGRAM:
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6]])
B = np.array([[7, 8],
[9, 10],
[11, 12]])
result = np.dot(A, B)
print("Matrix A:")
print(A)
print("\nMatrix B:")
print(B)
print("\nResultant Matrix (A x B):")
print(result)
OUTPUT:
Matrix A:
[[1 2 3]
[4 5 6]]
Matrix B:
[[ 7 8]
[ 9 10]
[11 12]]
Resultant Matrix (A x B):
[[ 58 64]
[139 154]]
RESULT:
Thus the above program to matrix multiplication for two-dimensional arrays using NumPy
was written and executed successfully.
REG.NO:
DATE:
EX.NO : 08
PROGRAM TO CHECK IF AN ARRAY CONTAINS ALL POSITIVE
NUMBERS USING AN COMPARISON OPERATOR
AIM:
To write a program to check if an array contains all positive numbers using a comparison operator.
ALGORITHM:
● Take the input array.
● Use a comparison operator (> 0) to check if each element of the array is positive.
● Use the .all() method to verify if all elements are positive (i.e., all comparison results
are True).
● Print whether all elements are positive or not based on the result of .all().
PROGRAM:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
if (arr > 0).all():
print("The array contains all positive numbers.")
else:
print("The array does not contain all positive numbers.")
OUTPUT:
The array contains all positive numbers.
RESULT:
Thus the program to check if an array contains all positive numbers using a comparison
operator was written and executed successfully.
REG.NO:
DATE:
EX NO: 09
PROGRAM TO CREATE A RANDOM 3×3 MATRIX AND FIND THE
MAXIMUM VALUE IN THE MATRIX
AIM:
To write a program to create a random 3×3 matrix and find the maximum value in the matrix
using NumPy.
ALGORITHM:
Import the NumPy library.
Generate a random 3×3 matrix:
Use np.random.randint(lower, upper, (3,3)) to create a matrix with random integers.
Find the maximum value in the matrix using np.max(matrix).
Display the matrix.
Display the maximum value.
PROGRAM:
import numpy as np
matrix = np.random.randint(1, 101, (3, 3))
max_value = np.max(matrix)
print("Random 3x3 Matrix:")
print(matrix)
print("\nMaximum Value in the Matrix:", max_value)
OUTPUT:
Random 3x3 Matrix:
[[23 89 45]
[67 12 98]
[56 34 77]]
RESULT:
Thus the program to create a random 3×3 matrix and find the maximum value in the matrix
using NumPy was written and executed successfully.