-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAss3.py
More file actions
163 lines (135 loc) · 4.78 KB
/
Ass3.py
File metadata and controls
163 lines (135 loc) · 4.78 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import time
# Part 1: Pandas
# Question 1: Filter rows with CPI > 60 and calculate statistics
# Create the DataFrame
data = {'Std Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Roll no': [1, 2, 3, 4],
'CPI': [75.5, 45.3, 68.7, 82.1]}
df = pd.DataFrame(data)
# Filter rows where CPI > 60
filtered_df = df[df['CPI'] > 60]
# Calculate statistics
mean_cpi = df['CPI'].mean()
median_cpi = df['CPI'].median()
std_cpi = df['CPI'].std()
print("Filtered DataFrame:")
print(filtered_df)
print(f"Mean: {mean_cpi}, Median: {median_cpi}, Std Dev: {std_cpi}")
# Question 2: Total books per subject
# Create the DataFrame
data = {'Subjects': ['Mathematics', 'Physics', 'Chemistry', 'Mathematics', 'Physics'],
'Authors': ['R.D.Sharma', 'H.C.Verma', 'O.P.Tandon', 'S.K.Goyal', 'Resnick'],
'No of Books': [15, 12, 8, 10, 5]}
df_books = pd.DataFrame(data)
# Calculate total books per subject
total_books = df_books.groupby('Subjects')['No of Books'].sum()
print("\nTotal Books per Subject:")
print(total_books)
# Question 3: Image pixel data to CSV
# Read the image and convert it to a NumPy array
# Uncomment the following lines to test with an actual image file
# image = Image.open('example.jpg').convert('L') # Convert to grayscale
# image_data = np.array(image)
# Save to CSV
# pd.DataFrame(image_data).to_csv('image_data.csv', index=False)
# Load the CSV back, excluding the last row and last column
# df_image = pd.read_csv('image_data.csv')
# df_image = df_image.iloc[:-1, :-1]
# print("\nImage Data from CSV:")
# print(df_image)
# Question 4: Clean DataFrame
dirty_data = pd.DataFrame({'Name': ['A', 'B', 'C', 'D'],
'Age': [16, 25, 17, 30],
'Salary': [50000, -20000, 40000, 60000]})
# Remove rows where Age < 18 or Salary < 0
cleaned_data = dirty_data[(dirty_data['Age'] >= 18) & (dirty_data['Salary'] >= 0)]
print("\nCleaned DataFrame:")
print(cleaned_data)
# Question 5: Extract hour from timestamp
time_data = pd.DataFrame({'TimeStamp': pd.date_range('2025-01-01', periods=5, freq='H')})
time_data['Hour'] = time_data['TimeStamp'].dt.hour
print("\nTime Data with Hour Column:")
print(time_data)
# Part 2: NumPy Application
# Question 1: Matrix multiplication and speedup
# Create random matrices
P = np.random.rand(10, 10)
Q = np.random.rand(10, 10)
# Using nested loops
start = time.time()
result = np.zeros((P.shape[0], Q.shape[1]))
for i in range(P.shape[0]):
for j in range(Q.shape[1]):
result[i, j] = np.dot(P[i, :], Q[:, j])
t1 = time.time() - start
# Using NumPy vectorized operations
start = time.time()
result_np = P @ Q.T
t2 = time.time() - start
# Speedup
speedup = t1 / t2
print(f"\nt1: {t1:.4f}, t2: {t2:.4f}, Speedup: {speedup:.2f}")
# Question 2: Extract second column and last row
arr = np.random.rand(5, 5)
second_column = arr[:, 1]
last_row = arr[-1, :]
print("\nSecond Column:", second_column)
print("Last Row:", last_row)
# Question 3: Frequency of unique numbers
array = np.random.randint(1, 10, size=(5, 6))
frequency = np.unique(array, return_counts=True)
print("\nFrequency of Unique Numbers:")
print(dict(zip(frequency[0], frequency[1])))
# Question 4: Solve linear equations
n = 5
A = np.random.rand(n, n)
b = np.random.rand(n)
x = np.linalg.solve(A, b)
print("\nSolution to Linear Equations:")
print(x)
# Question 5: Frequency of score 85
scores = np.array([85, 92, 75, 85, 90, 92, 85, 75, 85, 92, 75, 85, 90, 92, 85, 75, 85, 92])
frequency_85 = np.sum(scores == 85)
print(f"\nFrequency of Score 85: {frequency_85}")
# Question 6: Eigenvalues and eigenvectors
A = np.array([[2, -1, 0],
[-1, 2, -1],
[0, -1, 2]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print("\nEigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
# Question 7: Standardize features
X = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
mean = X.mean(axis=0)
std = X.std(axis=0)
X_standardized = (X - mean) / std
print("\nStandardized Features:\n", X_standardized)
# Question 8: Singular Value Decomposition
M = np.array([[1, 2],
[3, 4],
[5, 6]])
U, S, Vt = np.linalg.svd(M)
print("\nSVD Results:")
print("U:\n", U)
print("S:", S)
print("Vt:\n", Vt)
# Part 3: Data Visualization
# Question 1: Bar chart for total books per subject
total_books.plot(kind='bar', color=['blue', 'orange', 'green'])
plt.title("Total Books per Subject")
plt.xlabel("Subject")
plt.ylabel("Number of Books")
plt.show()
# Question 2: Histogram for salary distribution
salaries = [55, 62, 65, 58, 70, 55, 60, 68, 63, 55, 62, 150]
plt.hist(salaries, bins=6, color='purple', edgecolor='black')
plt.title("Distribution of Employee Salaries")
plt.xlabel("Salary (in thousands of dollars)")
plt.ylabel("Frequency")
plt.show()