Assignment of Numpy
Task 1: Student Performance Analysis Dataset (Marks of 8 students in 3 subjects):
data= [Link]([.
}}
[85, 18, 921, [76, 85, 88], [90, 82, 94], [65, 70, 72], [88, 90, 85], [92, 95, 98], [70, 68, 75], [80, 85,
821
Questions:
# Student 1
# Student 2 # Student 3 # Student 4 # Student 5 # Student 6
# Student 7
# Student 8
Find the subject-wise mean, median, and standard deviation. Identify the highest and lowest
scoring students in each subject. Normalize the dataset (scale marks between 0 and 1)
Plot:
A bar chart showing total marks of each student.
A line chart comparing subject averages.
CODE:
import numpy as np
import [Link] as plt
# Marks of 8 students in 3 subjects
marks = [Link]([
[85, 78, 92],
[76, 85, 88],
[90, 82, 94],
[65, 70, 72],
[88, 90, 85],
[92, 95, 98],
[70, 68, 75],
[80, 85, 82]
], dtype=float)
num_students = [Link][0]
num_subjects = [Link][1]
# Finding mean, median, and standard deviation for each subject
mean_marks = [Link](marks, axis=0)
median_marks = [Link](marks, axis=0)
std_marks = [Link](marks, axis=0)
print("Mean marks for each subject:", mean_marks)
print("Median marks for each subject:", median_marks)
print("Standard deviation:", std_marks)
1
print()
# Highest and lowest scores in each subject
highest_scores = [Link](marks, axis=0)
lowest_scores = [Link](marks, axis=0)
for i in range(num_subjects):
print(f"Subject {i+1}: Highest - Student {highest_scores[i]+1} ({marks[highest_scores[i], i]}) | "
f"Lowest - Student {lowest_scores[i]+1} ({marks[lowest_scores[i], i]})")
# Normalizing marks between 0 and 1 for each subject
min_marks = [Link](marks, axis=0)
max_marks = [Link](marks, axis=0)
diff = max_marks - min_marks
diff[diff == 0] = 1
normalized = (marks - min_marks) / diff
print("\nNormalized Marks (0-1 scale):\n", normalized)
# Calculating total marks and ranking students
total_marks = [Link](marks, axis=1)
rank = [Link](-total_marks)
print("\nTotal Marks per Student:", total_marks)
print("Rank (from highest to lowest):", (rank + 1))
print()
# Plot 1: Total marks of each student
[Link](figsize=(8, 4))
[Link]([Link](1, num_students + 1), total_marks, color='skyblue')
[Link]("Students")
[Link]("Total Marks")
[Link]("Total Marks of Students")
[Link](axis='y', linestyle='--', linewidth=0.5)
plt.tight_layout()
[Link]()
# Plot 2: Average marks in each subject
[Link](figsize=(6, 3))
subjects = [f"Subject {i+1}" for i in range(num_subjects)]
[Link](subjects, mean_marks, marker='o', color='green')
[Link]("Average Marks by Subject")
[Link]("Subjects")
[Link]("Average Marks")
for i, v in enumerate(mean_marks):
[Link](i, v + 0.5, f"{v:.1f}", ha='center')
2
plt.tight_layout()
[Link]()
OUTPUT:
=== TASK 1: Student Performance Analysis ===
Mean marks for each subject: [80.75 81.625 85.75 ]
Median marks for each subject: [82.5 83.5 86.5]
Standard deviation: [9.14808723 8.70254991 8.52569645]
Subject 1: Highest - Student 6 (92.0) | Lowest - Student 4 (65.0)
Subject 2: Highest - Student 6 (95.0) | Lowest - Student 7 (68.0)
Subject 3: Highest - Student 6 (98.0) | Lowest - Student 4 (72.0)
Normalized Marks (0-1 scale):
[[0.74074074 0.37037037 0.76923077]
[0.40740741 0.62962963 0.61538462]
[0.92592593 0.51851852 0.84615385]
[0. 0.07407407 0. ]
[0.85185185 0.81481481 0.5 ]
[1. 1. 1. ]
[0.18518519 0. 0.11538462]
[0.55555556 0.62962963 0.38461538]]
Total Marks per Student: [255. 249. 266. 207. 263. 285. 213. 247.]
Rank (from highest to lowest): [6 3 5 1 2 8 7 4]
3
Task 2: Weather Data Analysis
Dataset (Temperature of 7 days in 3 cities):
data = [Link]([
[30, 32, 31],
# Day
[28, 29, 30],
Day 2
[33, 35, 34]
#Day 3
[31, 30, 321,
#Day 4
[29, 28, 271,
Day 5
[34, 36, 35],
# Day 6
[32, 31, 33]
])
Questions:
#Day 7
Calculate the average temperature for each city.
Find which city had the highest temperature during the week.
Compute the daily temperature differences (max - min for each row). Plot:
A line graph of daily average temperatures.
A bar graph of city-wise average temperatures.
CODE
# === TASK 2: Weather Data Analysis ===
print("=== TASK 2: Weather Data Analysis ===\n")
4
# Temperatures (rows = days, columns = cities)
temps = [Link]([
[30, 32, 31],
[28, 29, 30],
[33, 35, 34],
[31, 30, 32],
[29, 28, 27],
[34, 36, 35],
[32, 31, 33]
], dtype=float)
# Average temperature of each city
avg_city = [Link](temps, axis=0)
print("Average temperature per city:", avg_city)
# City with highest single temperature
city_max = [Link]([Link](temps, axis=0))
print(f"City with highest single temperature: City {city_max+1} (Max = {[Link](temps[:,
city_max])})")
# Difference between max and min temperature for each day
daily_diff = [Link](temps, axis=1) - [Link](temps, axis=1)
print("Daily temperature difference (Max - Min):", daily_diff)
print()
# Plot 1: Daily average temperature
daily_avg = [Link](temps, axis=1)
[Link](figsize=(7, 3))
[Link]([Link](1, len(daily_avg) + 1), daily_avg, marker='o', color='orange')
[Link]("Daily Average Temperature (All Cities)")
[Link]("Day")
[Link]("Average Temp")
for i, v in enumerate(daily_avg):
[Link](i + 1, v + 0.2, f"{v:.1f}", ha='center')
[Link](linestyle='--', linewidth=0.4)
plt.tight_layout()
[Link]()
# Plot 2: City-wise average temperatures
[Link](figsize=(5, 3))
cities = [f"City {i+1}" for i in range([Link][1])]
[Link](cities, avg_city, color='lightgreen')
[Link]("Average Temperature per City")
[Link]("Temperature (°C)")
for i, v in enumerate(avg_city):
5
[Link](i, v + 0.2, f"{v:.1f}", ha='center')
plt.tight_layout()
[Link]()
OUTPUT:
=== TASK 2: Weather Data Analysis ===
Average temperature per city: [31. 31.57142857 31.71428571]
City with highest single temperature: City 2 (Max = 36.0)
Daily temperature difference (Max - Min): [2. 2. 2. 2. 2. 2. 2.]
6
Task 3: Company Sales Analysis
Dataset (Quarterly sales of 4 products over 5 years):
data= [Link]([
[200, 250, 300, 220], [210, 270, 310, 230],
# Year 1
# Year 2 # Year 3
[250, 290, 320, 240], [260, 300, 330, 250], [280, 320, 350, 270]
# Year 4
# Year 5
])
Questions:
Find the yearly total sales and identify the best sales year.
Compute the product-wise average sales.
Calculate the percentage growth of each product from Year 1 to Year 5.
Plot:
A line graph showing sales growth trends for each product.. A stacked bar chart of product
contributions per year.
CODE
# === TASK 3: Company Sales Analysis ===
print("=== TASK 3: Company Sales Analysis ===\n")
sales = [Link]([
[200, 250, 300, 220],
[210, 270, 310, 230],
[250, 290, 320, 240],
[260, 300, 330, 250],
[280, 320, 350, 270]
], dtype=float)
# Total sales per year
yearly_total = [Link](sales, axis=1)
print("Total sales per year:", yearly_total)
best_year = [Link](yearly_total)
print(f"Best Year: Year {best_year+1} (Total = {yearly_total[best_year]})")
# Average sales per product
avg_product_sales = [Link](sales, axis=0)
print("Average sales per product:", avg_product_sales)
# Growth percentage from Year 1 to Year 5
growth = ((sales[-1] - sales[0]) / sales[0]) * 100
print("Growth from Year 1 to Year 5 (%):", growth)
print()
7
# Plot 1: Product-wise sales trends
years = [Link](1, 6)
[Link](figsize=(8, 4))
for i in range([Link][1]):
[Link](years, sales[:, i], marker='o', label=f"Product {i+1}")
[Link]("Sales Growth Trend (5 Years)")
[Link]("Years")
[Link]("Sales")
[Link]()
[Link](linestyle='--', linewidth=0.4)
plt.tight_layout()
[Link]()
# Plot 2: Stacked bar of product contributions
[Link](figsize=(8, 4))
bottom = [Link](len(years))
for i in range([Link][1]):
[Link](years, sales[:, i], bottom=bottom, label=f"Product {i+1}")
bottom += sales[:, i]
[Link]("Product Contributions per Year")
[Link]("Years")
[Link]("Sales")
[Link]()
plt.tight_layout()
[Link]()
OUTPUT:
=== TASK 3: Company Sales Analysis ===
Total sales per year: [ 970. 1020. 1100. 1140. 1220.]
Best Year: Year 5 (Total = 1220.0)
Average sales per product: [240. 286. 322. 242.]
Growth from Year 1 to Year 5 (%): [40. 28. 16.66666667 22.72727273]
8
9