Experiment LAB 8
Number
Date of Experiment 19/02/2025
Date of 22/02/2025
Submission
Name of student Nishmeet Singh Rajpal
Roll Number 2330293
Section ECSc-2
1. Title of Experiment:
Getting Started With Python libraries
Introduction
The purpose of this lab assignment is to familiarize students with the fundamental
concepts of numerical computing, data manipulation, and visualization using
Python's popular libraries such as NumPy, Pandas, and Matplotlib. These tools are
widely used in scientific computing, data analysis, and machine learning, making
them essential for any student pursuing a career in computer science, engineering,
or data science.
● Programming Language used:
1. PYTHON
Problem Statement & Solution
Lab Assignments
import numpy as np
A = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
B = A.copy()
B[0, 1:] = 999
B[1, 1:] = 999
print("Matrix A:\n", A)
print("Matrix B:\n", B)
print("Arnav Singhal")
print("2330293")
Output
import numpy as np
A = np.array([[3, 2],[10, -1]])
B = np.array([5, 3])
solution = np.linalg.solve(A, B)
print("Solution (x, y):", solution)
print("Arnav Singhal")
print("2330293")
print("Not equal")
Output
import numpy as np
# Generate a 5x5 random matrix
random_matrix = np.random.rand(5, 5)
# Calculate statistics along all axes
max_values = np.max(random_matrix, axis=0) # Column-wise max
min_values = np.min(random_matrix, axis=1) # Row-wise min
mean_values = np.mean(random_matrix) # Overall mean
variance_values = np.var(random_matrix, axis=0) # Column-wise variance
print("Random Matrix:\n", random_matrix)
print("Max values (column-wise):", max_values)
print("Min values (row-wise):", min_values)
print("Mean value:", mean_values)
print("Variance values (column-wise):", variance_values)
print("Arnav Singhal")
print("2330293")
Output
import numpy as np
random_int_matrix = np.random.randint(2, 101, size=(5, 5))
boolean_mask = (random_int_matrix > 20) & (random_int_matrix < 70)
print("Random Integer Matrix:\n", random_int_matrix)
print("Boolean Mask:\n", boolean_mask)
print("Arnav Singhal")
print("2330293")
Output
import numpy as np
A = np.array([1, 2, 3, 4, 5, 6])
B = np.array([7, 8, 9, 10, 12, 17])
mse = np.mean((A - B) ** 2)
print("Mean Squared Error:", mse)
print("Arnav Singhal")
print("2330293")
Output
import pandas as pd
D = {"Rohan": 176, "John": 175, "Harvinder": 180, "Hasan": 174}
X = pd.Series(D)
new_index = ["Tinku", "John", "Ramesh", "Hasan"]
X_reindexed = X.reindex(new_index)
nan_indices = X_reindexed[X_reindexed.isna()].index
print("Series X with new index:\n", X_reindexed)
print("Indices with NaN values:", nan_indices.tolist())
print("Arnav Singhal")
print("2330293")
Output
import numpy as np
data = np.random.normal(loc=0, scale=1, size=20)
q1 = np.percentile(data, 25)
q2 = np.percentile(data, 50) # Median
q3 = np.percentile(data, 75)
count_q1 = np.sum(data <= q1)
count_q2 = np.sum((data > q1) & (data <= q2))
count_q3 = np.sum((data > q2) & (data <= q3))
count_q4 = np.sum(data > q3)
print("Data:", data)
print("Quartiles: Q1 =", q1, ", Q2 =", q2, ", Q3 =", q3)
print("Counts: Q1 =", count_q1, ", Q2 =", count_q2, ", Q3 =", count_q3,
", Q4 =", count_q4)
print("Arnav Singhal")
print("2330293")
Output
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y1 = [0, 100, 200, 300, 400, 500]
y2 = [50, 20, 40, 20, 60, 70]
y3 = [10, 20, 30, 40, 50, 60]
y4 = [200, 350, 250, 550, 450, 150]
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].plot(x, y1)
axs[0, 0].set_title("Line 1")
axs[0, 1].plot(x, y2)
axs[0, 1].set_title("Line 2")
axs[1, 0].plot(x, y3)
axs[1, 0].set_title("Line 3")
axs[1, 1].plot(x, y4)
axs[1, 1].set_title("Line 4")
plt.tight_layout()
plt.show()
print("Arnav Singhal")
print("2330293")
import matplotlib.pyplot as plt
company = ["Apple", "Microsoft", "Google", "AMD"]
profit = [3000, 8000, 1000, 10000]
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.bar(company, profit, color='blue')
plt.title("Bar Plot")
plt.xlabel("Company")
plt.ylabel("Profit")
plt.subplot(1, 2, 2)
plt.barh(company, profit, color='green')
plt.title("Horizontal Bar Plot")
plt.xlabel("Profit")
plt.ylabel("Company")
plt.tight_layout()
plt.show()
Output
import numpy as np
import matplotlib.pyplot as plt
# Generate data
box1 = np.random.normal(100, 10, 200)
box2 = np.random.normal(90, 20, 200)
data = [box1, box2]
plt.figure(figsize=(8, 6))
plt.boxplot(data, labels=["Box 1", "Box 2"])
plt.title("Box Plot")
plt.ylabel("Values")
plt.show()
Conclusion
Through this lab assignment, we have successfully implemented a variety of Python
programs that demonstrate the power and versatility of libraries like NumPy,
Pandas, and Matplotlib.
We began by performing matrix manipulations and solving systems of linear
equations, showcasing the computational capabilities of NumPy. We then explored
statistical analysis on random data, including:
● Finding maximum, minimum, mean, and variance values
● Creating boolean masks for specific conditions
The lab also introduced us to data handling using Pandas, where we:
● Created and manipulated Series objects
● Identified missing values (NaN)
Furthermore, we gained hands-on experience with data visualization by creating:
● Line plots
● Bar plots and horizontal bar plots
● Box plots
These visualization techniques are essential for summarizing and interpreting
data visually.
Conclusion
Overall, this lab has provided valuable insights into the practical applications of
Python in numerical and data-driven tasks. By completing these exercises,
students have not only enhanced their programming skills but also developed a
solid foundation for tackling more complex problems in the future.
These skills will undoubtedly prove invaluable as we continue to explore advanced
topics in data science and computational methods.
Faculty Signature