0% found this document useful (0 votes)
11 views6 pages

FDS Rec 9-12

The document outlines various programming exercises conducted in a computer science laboratory at Panimalar Engineering College. It includes programs for finding unique elements in an array, performing matrix multiplication, checking for positive numbers in an array, and creating a random 3x3 matrix to find its maximum value, all utilizing NumPy. Each section provides an aim, algorithm, program code, output, and a result confirming successful execution.

Uploaded by

shobanasofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

FDS Rec 9-12

The document outlines various programming exercises conducted in a computer science laboratory at Panimalar Engineering College. It includes programs for finding unique elements in an array, performing matrix multiplication, checking for positive numbers in an array, and creating a random 3x3 matrix to find its maximum value, all utilizing NumPy. Each section provides an aim, algorithm, program code, output, and a result confirming successful execution.

Uploaded by

shobanasofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

REG.NO:

DATE :

EX.NO :

PROGRAM TO FIND THE UNIQUE ELEMENT IN AN ARRAY AND


COUNT THE OCCURENCE
AIM:

To write a program to find the unique element in an array and count the occurrence.

ALGORITHM:

● Initialize an array with elements.


● Use np.unique(): Apply np.unique() on the array to get two things:
Unique elements (numbers that appear only once).
Count of occurrences for each unique element.
● Print the unique elements and their count.

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

23AD1413 – FOUNDATIONS OF DATA SCIENCE LABORATORY


PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


Element 5 occurs 2 times
Element 6 occurs 1 times

RESULT:
Thus the program to to find the unique element in an array and count the occurrence was
written and executed successfully.

23AD1413 – FOUNDATIONS OF DATA SCIENCE LABORATORY


PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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)

23AD1413 – FOUNDATIONS OF DATA SCIENCE LABORATORY


PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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.

23AD1413 – FOUNDATIONS OF DATA SCIENCE LABORATORY


PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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.

23AD1413 – FOUNDATIONS OF DATA SCIENCE LABORATORY


PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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]]

Maximum Value in the Matrix: 98

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.

23AD1413 – FOUNDATIONS OF DATA SCIENCE LABORATORY

You might also like