0% found this document useful (0 votes)
4 views

Experiment 3

The document outlines various programming exercises using NumPy, a library for numerical computing in Python. It includes tasks such as creating and manipulating arrays, computing statistics, reshaping data, and performing operations on multi-dimensional arrays. Each program is accompanied by its code, results, and a conclusion emphasizing the importance of NumPy in data analysis.

Uploaded by

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

Experiment 3

The document outlines various programming exercises using NumPy, a library for numerical computing in Python. It includes tasks such as creating and manipulating arrays, computing statistics, reshaping data, and performing operations on multi-dimensional arrays. Each program is accompanied by its code, results, and a conclusion emphasizing the importance of NumPy in data analysis.

Uploaded by

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

BHARATIYA VIDYA BHAVAN’S

SARDAR PATEL INSTITUTE OF TECHNOLOGY


Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Name Disha Hemant Deshmukh

UID no. 2023300044

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.

PROGRAM: import numpy as np

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}")

above_average = np.sum(scores > meanScore)


print(f"Number of scores above the original average: {above_average}")

RESULT: Average (Mean) score: 85.3


Highest score: 94
Lowest score: 76
Standard Deviation: 6.034069936618236
Adjusted Scores: [93 97 84 99 90 81 95 94 82 88]
Number of scores above the original average: 5

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]

PROGRAM: import numpy as np

exam1 = np.array([78, 85, 92, 88, 76, 95, 89, 84])


exam2 = np.array([82, 90, 85, 91, 80, 92, 87, 86])

avg1 = np.mean(exam1)
avg2 = np.mean(exam2)

print("Average score of exam 1 is:", avg1)


print("Average score of exam 2 is:", avg2)

combined = np.add(exam1, exam2)


BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

print("Array of combined scores:", combined)

ctr = 0
for x in combined:
if x > 160:
ctr = ctr + 1

print("Number of students with a combined score greater than 160:", ctr)

RESULT: Average score of exam 1 is: 86.625


Average score of exam 2 is: 86.625
Array of combined scores: [160 175 177 179 156 187 176 170]
Number of students with a combined score greater than 160: 6

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.

PROGRAM: import numpy as np


sensorData=np.arange(60)
reshapedData=sensorData.reshape(3,4,5)
print(reshapedData)

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: import numpy as np

arr = np.arange(10, 22)


matrix = arr.reshape(3, 4)
print("3x4 Matrix filled with values from 10 to 21:")
print(matrix)

RESULT: 3x4 Matrix filled with values from 10 to 21:


[[10 11 12 13]
[14 15 16 17]
[18 19 20 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: import numpy as np

temperatures = np.array([12, 15, 11, 10, 9, 14, 13])


average_temp = np.mean(temperatures)
print("Average temperature over the week is:", average_temp)
above_avg_temps = temperatures[temperatures > average_temp]
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

print("Temperatures above the average:", above_avg_temps)


below_freezing_days = np.sum(temperatures < 0)
print("Number of days with temperatures below freezing:",
below_freezing_days)

RESULT: Average temperature over the week is: 12.0


Temperatures above the average: [15 14 13]
Number of days with temperatures below freezing: 0

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

PROGRAM: import numpy as np

imageMatrix = np.random.randint(0, 256, (4, 4))


flattenedArray = imageMatrix.flatten()
print("Original 2D Image Matrix (Grayscale):\n", imageMatrix)
print("\nFlattened 1D Array:\n", flattenedArray)

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

PROBLEM Write a NumPy program to append values to the end of an array.


STATEMENT:

PROGRAM: import numpy as np

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


values_to_append = [5, 6, 7]
new_array = np.append(arr, values_to_append)
print("Original array:", arr)
print("Array after appending values:", new_array)

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.

PROGRAM: import numpy as np


arr_1d = np.array([1,2,3])
arr_2d = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
transposed_2d = np.transpose(arr_2d)
result = np.add(arr_1d,transposed_2d)
print(result)

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

PROGRAM: import numpy as np

array_3d = np.random.randint(0, 10, (3, 3, 3))


array_1d = array_3d.ravel()
print("Original 3D Array:\n", array_3d)
print("\nReshaped 1D Array:\n", array_1d)

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

PROBLEM Write a NumPy program that creates a 1D array of 16 elements, reshape it to


STATEMENT: (4, 4), then change its shape to (2, 8) without changing the underlying data.

PROGRAM: import numpy as np

array_1d = np.arange(1, 17)


array_4x4 = array_1d.reshape(4, 4)
array_2x8 = array_1d.reshape(2, 8)
print("Original 1D Array:\n", array_1d)
print("\nReshaped to (4, 4):\n", array_4x4)
print("\nReshaped to (2, 8):\n", array_2x8)

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]

Reshaped to (4, 4):


[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]

Reshaped to (2, 8):


[[ 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy