Experiment 3
Experiment 3
Experiment No. 3
AIM: NumPy
Program 1
PROBLEM You are given an array representing the scores of students in a class for a
STATEMENT : recent exam. Your tasks are: Given: scores of 10 students: scores = [88, 92,
79, 94, 85, 76, 90, 89, 77, 83]
a. Create an Array: Create a numpy array with the given scores.
b. Compute Statistics:
a. Calculate the average (mean) score.
b. Find the highest and lowest scores.
c. Calculate the standard deviation of the scores to understand the
variability.
c. Modify the Array:
a. Increase each score by 5 points to simulate a curve adjustment.
b. Find the number of scores above the original average score.
scores = np.array([88, 92, 79, 94, 85, 76, 90, 89, 77, 83])
meanScore = np.mean(scores)
print(f"Average (Mean) score: {meanScore}")
maxScore = np.max(scores)
minScore = np.min(scores)
print(f"Highest score: {maxScore}")
print(f"Lowest score: {minScore}")
std_deviation = np.std(scores)
print(f"Standard Deviation: {std_deviation}")
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
adjusted_scores = scores + 5
print(f"Adjusted Scores: {adjusted_scores}")
Program 2
PROBLEM You have two numpy arrays representing the scores of two different exams for
STATEMENT : the same group of students. Your tasks are:
a. Create the Arrays: Define two arrays exam1 and exam2 with the given
scores.
b. Compute the Average Scores: Calculate the average score for each exam.
c. Combine the Scores: Create a new array where each student's combined
score is the sum of their scores in both exams.
d. Find the Number of Students with a Combined Score Greater Than 160:
Count how many students have a combined score exceeding 160.
Given Data:
exam1 = [78, 85, 92, 88, 76, 95, 89, 84]
exam2 = [82, 90, 85, 91, 80, 92, 87, 86]
avg1 = np.mean(exam1)
avg2 = np.mean(exam2)
ctr = 0
for x in combined:
if x > 160:
ctr = ctr + 1
Program 3
PROBLEM You are working with data from multiple sensors recorded over time. The data
STATEMENT: is in a 1D array, but you need to reshape it to analyze it as a matrix of sensor
readings over multiple time steps.
a. Create a 1D Sensor Data Array: Define a 1D array of length 60, where each
value represents a sensor reading.
b. Reshape to 3x4x5: Reshape this 1D array into a 3D array with shape 3x4x5,
where 3 represents time steps, 4 represents different sensors, and 5 represents
readings at each sensor.
c. Display the Reshaped Array: Show the resulting 3D array.
RESULT:
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
[[20 21 22 23 24]
[25 26 27 28 29]
[30 31 32 33 34]
[35 36 37 38 39]]
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
[[40 41 42 43 44]
[45 46 47 48 49]
[50 51 52 53 54]
[55 56 57 58 59]]]
Program 4
PROBLEM Write a NumPy program to create a 3x4 matrix filled with values from 10 to
STATEMENT: 21.
Program 5
PROBLEM You have an array of temperatures recorded over a week. Your tasks are:
STATEMENT: a. Create the Array: Define a numpy array with the given temperatures.
b. Find the Average Temperature: Calculate the average temperature over the
week.
c. Filter Temperatures Above Average: Create an array with temperatures that
are above the average temperature.
d. Find the Number of Days with Temperatures Below Freezing: Count how
many days had temperatures below 0°C.
Given temperatures = [12, 15, 11, 10, 9, 14, 13]
Program 6
PROBLEM You are working with grayscale images in a 2D matrix format. You need to
STATEMENT: prepare these images for machine learning models, which often require the
data in a flattened 1D format.
a. Create a Grayscale Image Matrix: Define a 4x4 grayscale image matrix with
random values between 0 and 255.
b. Flatten the Image Matrix: Convert the 2D image matrix into a 1D array.
c. Display the Results: Show the original 2D image matrix and the flattened
1D array
RESULT: Original
2D Image Matrix
(Grayscale):
[[217 225 46 119]
[ 65 111 54 117]
[ 52 165 74 250]
[171 229 0 25]]
Flattened 1D
Array:
[217 225 46 119 65
111 54 117 52 165
74 250 171 229 0
25]
Program 7
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
RESULT:
Original
array: [1 2 3 4]
Array after
appending values:
[1 2 3 4 5 6 7]
Program 8
PROBLEM Given a 2D array of shape (3, 5) and a 1D array of shape (3, ). Write a Numpy
STATEMENT: program that transposes the 2D array and add the 1D array to each row of the
transposed array.
RESULT: [[ 2 8 14]
[ 3 9 15]
[ 4 10 16]
[ 5 11 17]
[ 6 12 18]]
Program 9
PROBLEM Write a NumPy program to create a 3D array of shape (3, 3, 3), then reshape it
STATEMENT: into a 1D array using ravel () and finally print the result.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
RESULT: Original
3D Array:
[[[5 7 8]
[7 9 0]
[4 9 2]]
[[1 1 9]
[8 5 9]
[4 9 4]]
[[8 7 5]
[2 8 2]
[1 4 5]]]
Reshaped 1D
Array:
[5 7 8 7 9 0 4 9 2 1 1
98594948752
8 2 1 4 5]
Program 10
RESULT:
Original 1D Array:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
[1 2 3 4 5 6 7 8
9 10 11 12 13 14 15
16]
CONCLUSION: NumPy is a powerful and efficient library for numerical and matrix
operations. Its ability to handle multi-dimensional arrays, along with advanced
functionalities like broadcasting, reshaping, and boolean indexing, makes it a
vital tool for tasks in data analysis.