Vedant 2024801005 Experiment 3
Vedant 2024801005 Experiment 3
Experiment No. 3
PROBLEM a. Use NumPy to load the dataset into an array for processing.
STATEMENT :
RESULT :
np.loadtxt() reads numerical data from a file, using , as the delimiter to separate values.
Output:
Program 2
data
std = np.std(arr) # Spread of data
var = np.var(arr) # Variance (square of std
deviation)
print("Mean:", mean)
print("Median:", median)
print("Standard Deviation:", std)
print("Variance:", var)
RESULT :
Built-in NumPy functions compute key statistical measures to analyze data distribution.
Output:
Program 3
PROBLEM # Change the shape of the array to a specified dimension, such as converting a
STATEMENT: 1D
print(arr_1d)
RESULT :
The reshape() function modifies the array shape without changing data, and copy() ensures the
original array remains unchanged.
OUTPUT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
Program 4
RESULT:
NumPy applies arithmetic operations element-wise, meaning each element interacts with its
corresponding element.
OUTPUT:
Program 5
PROBLEM # Extract specific elements, rows, or columns from the array using slicing
STATEMENT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
RESULT:
NumPy slicing (:) is used to extract specific rows and columns from a reshaped 2D array.
OUTPUT:
Program 6
PROBLEM # Apply a condition to the array to filter out elements that meet certain criteria.
STATEMENT:
RESULT:
Boolean indexing (arr < 4) filters elements based on a condition, returning values that meet the
criteria.
OUTPUT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
Program 7
print(arr)
RESULT:
The np.sort() function arranges the array elements in increasing order.
OUTPUT:
Program 8
PROBLEM # Identify and count the unique elements present in the array.
STATEMENT:
RESULT:
np.unique() identifies distinct elements, and return_counts=True returns the frequency of each
unique value.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
OUTPUT:
Program 9
RESULT:
np.dot() performs matrix multiplication. The array is transposed (arr.T) before multiplication to
match dimensions.
OUTPUT:
Program 10
RESULT:
Broadcasting allows NumPy to expand smaller arrays automatically to match larger arrays for
element-wise operations.
OUTPUT:
Program 11
PROBLEM # Given stock prices for a week, calculate the daily percentage returns and find
STATEMENT: the highest gain and worst loss. Sample stock prices (closing prices for 7 days)
worst_loss = np.min(daily_returns)
RESULT:
Stock returns are calculated using (new - old) / old * 100. np.max() and np.min() find the highest
and lowest daily changes.
OUTPUT:
Program 12
print("Solution:", x)
RESULT:
np.linalg.solve(A, b) uses matrix algebra to solve the system of linear equations Ax = b.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
OUTPUT:
Program 13
PROBLEM # Problem Statement: You have two numpy arrays representing the scores of
STATEMENT: two different exams for 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.
RESULT:
np.mean() calculates averages, and np.sum(condition) counts students scoring above 160.
OUTPUT:
Program 14
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:
np.arange(1, 61) generates values, and reshape(3,4,5) organizes them into a 3D array.
OUTPUT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
Program 15
PROBLEM # Write a NumPy program to create a 3x4 matrix filled with values from 10 to
STATEMENT: 21.
RESULT:
np.arange(10, 22) creates numbers from 10 to 21, and reshape(3,4) organizes them into a matrix.
OUTPUT:
Program 16
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
PROBLEM # Problem Statement: You have an array of temperatures recorded over a week.
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.
RESULT:
np.mean() computes the average temperature, Boolean indexing selects values above the average,
and np.sum() counts freezing days.
OUTPUT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
Program 17
PROBLEM # 8) Problem Statement: You are working with grayscale images in a 2D matrix
STATEMENT: format. You need to 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:
np.random.randint(0, 256, (4, 4)) generates random grayscale pixel values, and flatten() converts
the 2D matrix into a 1D array.
OUTPUT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
Program 18
RESULT:
np.append() adds new elements to an existing array without modifying the original.
OUTPUT:
Program 19
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
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
# Define a 1D array
array_1d = np.array([10, 20, 30])
print("Transposed Array:")
print(transposed_array)
print("\nResult after Adding 1D Array to Each
Row:")
print(result_array)
RESULT:
The 2D array is transposed using .T, and reshape(1,-1) adjusts the 1D array shape for broadcasting.
OUTPUT:
Program 20
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Science and Engineering
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.
print("Original 3D Array:")
print(array_3d)
print("\nReshaped 1D Array:")
print(reshaped_array)
RESULT:
np.random.randint() generates random numbers, and ravel() flattens the 3D array into a 1D array.
OUTPUT:
Program 21
PROBLEM # 12) Write a NumPy program that creates a 1D array of 16 elements, reshape it
STATEMENT: to (4,4), then change its shape to (2, 8) without changing the underlying data.
# Reshape to (4x4)
array_2d = array_1d.reshape(4, 4)
print("Original 1D Array:")
print(array_1d)
print("\nReshaped to (4, 4):")
print(array_2d)
print("\nReshaped to (2, 8):")
print(reshaped_2d)
RESULT:
reshape(4,4) and reshape(2,8) change the structure of the array without modifying its values.
OUTPUT:
Program 22
PROBLEM 13) A company records daily sales for a week. Compute the total revenue, find
STATEMENT: which day had the highest and lowest sales, and calculate the rolling 3-day
average.
RESULT:
np.sum() calculates total sales, np.argmax() and np.argmin() identify the highest and lowest sales
days, and np.convolve() computes a rolling average.
OUTPUT: