DATA SCIENCE ASSIGNMENT -1
Name : Angulakshmi R
Reg No : 715523105004
1. A smart city deploys temperature sensors across multiple zones. Daily calibration values
are applied uniformly to sensor readings.
Create a 2D NumPy array representing hourly temperature readings for 5 zones over
24 hours.
Use broadcasting to apply a 1D calibration offset array to all zones.
Identify zones where temperature exceeds a critical limit using Boolean indexing.
Sort zones based on their average temperature using NumPy sorting functions.
Display the top two hottest zones.
Step 1 : Create a 2D NumPy array (5 zones × 24 hours)
import numpy as np
[Link](0) # for reproducibility
temps = [Link](20, 41, size=(5, 24))
print("Hourly Temperature Readings (5 Zones, 24 Hours):")
print(temps)
Step 2 : Apply 1D calibration offset using broadcasting
calibration_offset = [Link](-1, 1, 24)
calibrated_temps = temps + calibration_offset
print("\nCalibrated Temperatures:")
print(calibrated_temps)
Step 3 : Identify zones where temperature exceeds a critical limit
Assume critical limit = 38°C
critical_limit = 38
critical_mask = calibrated_temps > critical_limit
print("\nBoolean Mask (True = exceeds limit):")
print(critical_mask)
Find zones having any temperature above limit:
zones_exceeding = [Link]([Link](critical_mask, axis=1))[0]
print("\nZones exceeding critical temperature:")
print(zones_exceeding)
Step 4: Sort zones based on average temperature
zone_avg = [Link](calibrated_temps, axis=1)
print("\nAverage Temperature of Each Zone:")
print(zone_avg)
sorted_zones = [Link](zone_avg)
print("\nZones sorted by average temperature (cool → hot):")
print(sorted_zones)
Step 5: Display top two hottest zones
top_two_hot = sorted_zones[-2:]
print("\nTop Two Hottest Zones:")
print(top_two_hot)
2. A university maintains student academic records across multiple courses.
Create a structured array to store student ID, name, marks in three subjects, and
total score.
Compute total and average marks using NumPy operations.
Use Boolean indexing to identify students who failed in at least one subject.
Use fancy indexing to extract records of students whose IDs are provided in a
separate list.
Sort students in descending order of total marks.
Step 1: Create a structured NumPy array
import numpy as np
dtype = [
('id', 'i4'),
('name', 'U20'),
('sub1', 'i4'),
('sub2', 'i4'),
('sub3', 'i4'),
('total', 'i4')
]
students = [Link]([
(101, 'Arun', 78, 85, 69, 0),
(102, 'Divya', 45, 32, 40, 0),
(103, 'Kiran', 88, 90, 92, 0),
(104, 'Meena', 35, 70, 60, 0),
(105, 'Ravi', 91, 89, 84, 0)
], dtype=dtype)
print("Student Records:")
print(students)
Step 2: Compute Total and Average Marks
students['total'] = students['sub1'] + students['sub2'] + students['sub3']
average_marks = students['total'] / 3
print("\nTotal Marks:")
print(students['total'])
print("\nAverage Marks:")
print(average_marks)
Step 3: Identify students who failed in at least one subject
fail_mask = (students['sub1'] < 40) | \
(students['sub2'] < 40) | \
(students['sub3'] < 40)
failed_students = students[fail_mask]
print("\nStudents who failed in at least one subject:")
print(failed_students)
Step 4: Fancy indexing using given student IDs
id_list = [101, 103, 105]
selected_students = students[[Link](students['id'], id_list)]
print("\nRecords of given IDs:")
print(selected_students)
Step 5: Sort students by total marks (Descending order)
sorted_students = [Link](students, order='total')[::-1]
print("\nStudents sorted by total marks (High → Low):")
print(sorted_students)
3. An industry monitors machine vibration data collected every second.
1. Store vibration readings for multiple machines in a NumPy array.
2. Normalize the readings using broadcasting.
3. Use Boolean indexing to detect abnormal readings beyond safe limits
4. Extract faulty sensor readings using fancy indexing.
5. Rank machines based on the number of faults detected.
Step 1: Store vibration readings for multiple machines
import numpy as np
[Link](1)
vibration = [Link](0, 10, size=(5, 10))
print("Vibration Readings (Machines × Seconds):")
print(vibration)
Step 2: Normalize readings using broadcasting
min_val = [Link](axis=1, keepdims=True)
max_val = [Link](axis=1, keepdims=True)
normalized_vibration = (vibration - min_val) / (max_val - min_val)
print("\nNormalized Vibration Readings:")
print(normalized_vibration)
Step 3: Detect abnormal readings using Boolean indexing
safe_limit = 7
abnormal_mask = vibration > safe_limit
print("\nAbnormal Readings (True = unsafe):")
print(abnormal_mask)
Step 4: Extract faulty sensor readings using fancy indexing
faulty_indices = [Link](abnormal_mask)
faulty_readings = vibration[faulty_indices]
print("\nFaulty Sensor Readings:")
print(faulty_readings)
Step 5: Rank machines based on number of faults
fault_count = [Link](abnormal_mask, axis=1)
print("\nNumber of faults per machine:")
print(fault_count)
ranking = [Link](fault_count)[::-1]
print("\nMachines ranked by fault count (High → Low):")
print(ranking)
4. A hospital maintains patient vitals monitored at regular intervals.
1. Store patient ID, age, heart rate, and blood pressure in a record array.
2. Access fields using attribute-style access.
3. Use Boolean indexing to identify patients with abnormal vitals.
4. Sort patients based on heart rate severity.
5. Generate a summary report of critical patients.
Step 1: Create a NumPy record array for patient vitals
We store:
● Patient ID
● Age
● Heart Rate (bpm)
● Blood Pressure (mmHg)
import numpy as np
patients = [Link]([
(201, 45, 72, 120),
(202, 60, 95, 140),
(203, 30, 55, 110),
(204, 70, 110, 160),
(205, 50, 65, 130)
],
dtype=[
('id', 'i4'),
('age', 'i4'),
('heart_rate', 'i4'),
('blood_pressure', 'i4')
])
print("Patient Records:")
print(patients)
Step 2: Access fields using attribute-style access
print("\nPatient IDs:")
print([Link])
print("\nHeart Rates:")
print(patients.heart_rate)
Step 3: Identify patients with abnormal vitals (Boolean indexing)
abnormal_mask = (patients.heart_rate < 60) | \
(patients.heart_rate > 100) | \
(patients.blood_pressure > 130)
critical_patients = patients[abnormal_mask]
print("\nCritical Patients:")
print(critical_patients)
Step 4: Sort patients based on heart rate severity (Descending)
sorted_patients = [Link](patients, order='heart_rate')[::-1]
print("\nPatients sorted by heart rate severity (High → Low):")
print(sorted_patients)
Step 5: Generate summary report of critical patients
print("\n--- Critical Patient Summary Report ---")
for p in critical_patients:
print(f"ID: {[Link]}, Age: {[Link]}, HR: {p.heart_rate}, BP: {p.blood_pressure}")