Lab01 - Aim: Perform Creation, indexing, slicing, concatenation and repetition operations on Python built-in data types: Strings, List, Tuples, Dictionary, Set.
s1 = "Pratham"
s2 = "Bhagat"
print(s1 + " " + s2)
# String join() method
a = ["I", "am", "a", "YCCE", "student"]
print(" ".join(a))
# String format() method
a1 = "Pratham"
a2 = "Year"
print("{} is in second {}".format(a1, a2))
# f-string method
a1 = "Pratham"
a2 = "Year"
print(f"{a1} is in second {a2}")
# List repetition using * operator
a = [1, 2, 3]
print(a * 3)
# List slicing from index 1 to 8
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[1:8])
# List slicing with step value
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[1:9:2])
# List slicing from beginning
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[:4])
# List slicing till end
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[4:])
# List indexing
my_list = ["Banana", "orange", "mango", "watermelon"]
print(my_list[0])
print(my_list[1])
# List concatenation using +
a = [1, 2, 3]
print(a + a + a)
# List extend() method
a = [1, 2, 3]
a.extend([4, 5] * 3)
print(a)
# List comprehension for repetition
a = [1, 2, 3]
print([x for x in a for _ in range(2)])
# Negative index slicing
colors = ["red", "blue", "green", "orange", "blue", "pink", "yellow", "black", "white"]
print(colors[-4:-1])
# Modifying list element
colors = ["red", "blue", "green"]
print("Before change:", colors)
colors[1] = "pink"
print("After change:", colors)
Lab02
import numpy as np
sensor_data = np.array([
[22.5, 23.0, 22.8, 23.2],
[21.0, 21.5, 21.3, 21.8],
[24.1, 24.5, 24.0, 24.3]
])
min_value = np.min(sensor_data)
max_value = np.max(sensor_data)
mean_value = np.mean(sensor_data)
median_value = np.median(sensor_data)
std_deviation = np.std(sensor_data)
variance_value = np.var(sensor_data)
print("Overall IoT Sensor Data Statistics:")
print("Minimum:", min_value)
print("Maximum:", max_value)
print("Mean:", mean_value)
print("Median:", median_value)
print("Standard Deviation:", std_deviation)
print("Variance:", variance_value)
print("\nSensor-wise Mean Temperatures:")
print(np.mean(sensor_data, axis=1))
print("\nTime-wise Mean Temperatures:")
print(np.mean(sensor_data, axis=0))
Lab03
import math
square = 144
root = math.isqrt(square)
num1, num2 = 56, 98
gcd_answer = math.gcd(num1, num2)
angle_degrees = 45
angle_radians = math.radians(angle_degrees)
sin_45 = math.sin(angle_radians)
pi = 3.14159
new_pi = f"{pi:.3f}"
print("the number which is multiplied by itself results in {} is: {}".format(square, root))
print("largest positive integer that can divide {} and {} without leaving a remainder is: {}".format(num1, num2, gcd_answer))
print("{} degrees is: {}".format(angle_degrees, sin_45))
print("{} rounded to three decimal places is: {}".format(pi, new_pi))
import random
num=random.randint(1,100)
prob=random.random()
items=[10,20,30,40,50,60,70,80,90,100]
random.shuffle(items)
selection=random.choice(items)
print(f"Random Integer (1-100): {num}")
print(f"Random Probability (0-1): {prob}")
print(f"Shuffled Collection: {items}")
print(f"Randomly Selected Element: {selection}")
Lab04
lab = r"C:\Users\student\Documents\pratham\lab04(1).txt"
file = open(lab, "r")
print(file.read())
file.close()
# Overwrite the file with new content
lab = r"C:\Users\student\Documents\pratham\lab04(1).txt"
file = open(lab, "w")
file.write("python is easy language\n")
file.write("used in app development\n")
file.close()
print("The file has been overwritten successfully")
# Read another file
labnew = r"C:\Users\student\Documents\pratham\lab04(2).txt"
file = open(labnew, "r")
print(file.read())
file.close()
# Append text to the file
labnew = r"C:\Users\student\Documents\pratham\lab04(2).txt"
file = open(labnew, "a")
file.write("I'm YCCE STUDENT\n")
file.write("Studying in AIDS\n")
file.close()
print("New text has been appended successfully")
# Copy content from one file to another using with statement
with open(r"C:\Users\student\Documents\pratham\lab04(1).txt", "r") as lab:
with open(r"C:\Users\student\Documents\pratham\lab04(2).txt", "w") as labnew:
for line in lab:
labnew.write(line)
print("Content copied to lab04(2).txt successfully.")
# Read and display the copied content
labnew = r"C:\Users\student\Documents\pratham\lab04(2).txt"
file = open(labnew, "r")
print(file.read())
file.close()
Lab05
import numpy as np
arr_1d = np.linspace(5, 100, 20)
matrix = arr_1d.reshape(4, 5)
sliced_matrix = matrix[1:3, -3:]
matrix[-1, -1] = -1
print("Original 4x5 matrix (after replacement):\n", matrix)
print("\nSliced matrix (middle rows, last columns):\n", sliced_matrix)
#next
import numpy as np
math_scores = np.array([85, 90, 78, 92])
science_scores = np.array([88, 82, 95, 89])
joined_matrix = np.vstack((math_scores, science_scores))
student_scores = joined_matrix.T
first_two, last_two = np.split(student_scores, 2)
# Display results
print("Joined Matrix (2x4):\n", joined_matrix)
print("\nStudent Scores (4x2):\n", student_scores)
print("\nFirst Two Students:\n", first_two)
print("\nLast Two Students:\n", last_two)
Lab06
!pip install pillow
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# step 1: load & analyze the image
image_path = r"C:\Users\student\Downloads\highway.png" # change name if needed
img = Image.open(image_path)
img_np = np.array(img)
h, w, c = img_np.shape
print("image resolution (h × w × c):", img_np.shape)
# step 2: strategic roi selection (remove sky)
# crop bottom 50% to remove sky and trees
roi_top = int(h * 0.5)
roi = img_np[roi_top:h, :]
print("roi shape:", roi.shape)
# step 3: numpy slicing (bottom-center active lane)
roi_h, roi_w, _ = roi.shape
center_start = int(roi_w * 0.25)
center_end = int(roi_w * 0.75)
active_lane = roi[:, center_start:center_end]
print("active lane shape:", active_lane.shape)
# step 4: data augmentation (horizontal flip)
flipped_lane = np.fliplr(active_lane)
# step 5: visualization
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.imshow(img_np)
plt.title("original high-resolution image")
plt.axis("off")
plt.subplot(2, 2, 2)
plt.imshow(roi)
plt.title("roi (sky removed)")
plt.axis("off")
plt.subplot(2, 2, 3)
plt.imshow(active_lane)
plt.title("active lane (bottom-center)")
plt.axis("off")
plt.subplot(2, 2, 4)
plt.imshow(flipped_lane)
plt.title("horizontal flipped lane")
plt.axis("off")
plt.tight_layout()
plt.show()
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import os
# 1. array transformation: load the drone top-down image
image_path = r"C:\Users\student\Downloads\images.jpg"
img = Image.open(image_path)
img_np = np.array(img)
h, w, c = img_np.shape
print("image shape (h × w × c):", img_np.shape)
# 2. roi selection: isolate a shipping container / doorway
roi_top = int(h * 0.25)
roi_bottom = int(h * 0.6)
roi_left = int(w * 0.35)
roi_right = int(w * 0.7)
roi = img_np[roi_top:roi_bottom, roi_left:roi_right]
print("roi shape:", roi.shape)
# 3. dual-axis flipping
horizontal_flip = np.fliplr(roi)
vertical_flip = np.flipud(roi)
both_flip = np.flipud(horizontal_flip)
# 4 & 5. visualization (2 × 2 grid)
plt.figure(figsize=(8, 8))
plt.subplot(2, 2, 1)
plt.imshow(roi)
plt.title("original roi")
plt.axis("off")
plt.subplot(2, 2, 2)
plt.imshow(horizontal_flip)
plt.title("horizontal flip")
plt.axis("off")
plt.subplot(2, 2, 3)
plt.imshow(vertical_flip)
plt.title("vertical flip")
plt.axis("off")
plt.subplot(2, 2, 4)
plt.imshow(both_flip)
plt.title("horizontal + vertical flip")
plt.axis("off")
plt.tight_layout()
plt.show()
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# Load the image and convert to NumPy array
image_path = r"C:\Users\student\Downloads\night.jpg"
image = Image.open(image_path)
image_np = np.array(image)
h, w, c = image_np.shape
print(f"Image Resolution: {w}x{h}")
masked_image = image_np.copy()
masked_image[230:280, 290:330, :] = 0
# 3. ROI Extraction
row_start, row_end = int(h*0.4), int(h*0.9)
col_start, col_end = int(w*0.1), int(w*0.9)
cropped_roi = image_np[row_start:row_end, col_start:col_end]
# 4. Display Results
plt.figure(figsize=(12, 6))
# Show Privacy Masked Image
plt.subplot(1, 2, 1)
plt.imshow(masked_image)
plt.title("1. Privacy Masked (Window Obscured)")
plt.axis("off")
# Show Cropped ROI
plt.subplot(1, 2, 2)
plt.imshow(cropped_roi)
plt.title("2. Cropped ROI (Parking Area Only)")
plt.axis("off")
plt.tight_layout()
plt.show()
Lab07
import pandas as pd
df = pd.read_csv(r"C:\Users\student.AIDS-19\Downloads\sales_data.csv")
#visulalize
print("First 10 rows:")
print(df.head(10))
print("\nLast 10 rows:")
print(df.tail(10))
# 3. Explore DataFrame structure
print("Shape (rows, columns):", df.shape)
print("Index:", df.index)
print("Columns:", df.columns)
# 4. Sorting and Ranking
df_sorted = df.sort_values(by='total_revenue', ascending=False)
print("Top 5 orders by Revenue:")
print(df_sorted.head(5))
df['rating_rank'] = df['rating'].rank(ascending=False)
print("\nTop 5 Ranked by Rating:")
print(df[['rating', 'rating_rank']].head(5))
# 5. Statistical Operations
print("Mean Price:", df['price'].mean())
print("Median Price:", df['price'].median())
print("Standard Deviation of Price:", df['price'].std())
print("\nSummary Statistics (Describe):")
print(df['price'].describe())
# 6. Categorical Analysis
print("Unique values in product_category:")
print(df['product_category'].unique())
print("Count of unique values:")
print(df['product_category'].value_counts())
# 7. Rename Columns
df.rename(columns={'customer_region': 'region'}, inplace=True)
df.rename(columns={'payment_method': 'pay_mode', 'total_revenue': 'revenue'}, inplace=True)
print("Columns after rename:", df.columns)
# 8. Select/Delete records and columns
high_rating = df[df['rating'] > 4.0]
print(f"Rows with rating > 4.0: {len(high_rating)}")
if 'review_count' in df.columns:
df = df.drop(columns=['review_count'])
print("Column 'review_count' deleted.")
df = df[df['quantity_sold'] >= 2]
print("Rows with low quantity deleted. New shape:", df.shape)
print("\nFinal DataFrame Head:")
print(df.head())
Lab08
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import zscore
df = pd.read_csv(r"C:\Users\Student\Documents\medical.csv")
print("--- Original Dataset ---")
print(df.head(10))
print("\n")
bp_median = df['Blood Pressure'].median()
df['Blood Pressure'] = df['Blood Pressure'].fillna(bp_median)
def categorize_bmi(bmi):
if bmi < 18.5:
return "Underweight"
elif bmi <= 24.9:
return "Normal"
else:
return "Overweight"
df['BMI_Category'] = df['BMI'].apply(categorize_bmi)
df['HR_Zscore'] = zscore(df['HeartRate'])
df_filtered = df[(np.abs(df['HR_Zscore']) < 3) &
(df['HeartRate'] >= 30) &
(df['HeartRate'] <= 220)].copy()
df_filtered = df_filtered.drop(columns=['HR_Zscore'])
df_filtered['Smoking_History_Flag'] = df_filtered['MedicalHistoryNotes'].str.contains('Smoker|Former', case=False, na=False)
print("--- Processed Dataset ---")
print(df_filtered)
print("\n")
sns.set_theme(style="whitegrid")
plt.figure(figsize=(8, 5))
sns.kdeplot(df_filtered['Age'], fill=True, color='blue')
plt.title('Density Plot of Patient Ages')
plt.xlabel('Age')
plt.ylabel('Density')
plt.show()
plt.figure(figsize=(8, 5))
plt.scatter(df_filtered['Glucose'], df_filtered['Insulin'], color='red', edgecolor='black')
plt.title('Scatter Plot: Glucose vs. Insulin Levels')
plt.xlabel('Glucose Levels (mg/dL)')
plt.ylabel('Insulin Levels (µU/mL)')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r"C:\Users\Student\Documents\Skills.csv")
missing_yoe = df[df['YearsOfExperience'].isnull()]
df = df.dropna(thresh=len(df.columns) - 3)
df['ExperienceLevel'] = df['ExperienceLevel'].map({'Junior': 1, 'Mid': 2, 'Senior': 3})
q99 = df['Salary'].quantile(0.99)
df = df[df['Salary'] <= q99]
df['FavoriteLanguages'] = df['FavoriteLanguages'].str.replace('Javascript', 'JavaScript').str.replace('JS', 'JavaScript').str.replace('js', 'JavaScript')
salary_trend = df.groupby('YearsOfExperience')['Salary'].mean()
salary_trend.plot(kind='line', marker='o')
plt.title('Average Salary vs. Years of Experience')
plt.xlabel('Years of Experience')
plt.ylabel('Average Salary')
plt.show()
df['Salary'].plot(kind='hist', bins=10, edgecolor='black')
plt.title('Global Salary Distribution')
plt.xlabel('Salary')
plt.ylabel('Frequency')
plt.show()
Lab09
sample_space = {1, 2, 3, 4, 5, 6}
even_subset = {num for num in sample_space if num % 2 == 0}
total = len(sample_space)
fav = len(even_subset)
probability = fav / total
print(f"Sample Space: {sample_space}")
print(f"Even Subset: {even_subset}")
print(f"Probability: {probability}")
plt.hist(list(even_subset), bins=np.arange(1, 8) - 0.5, edgecolor='black', alpha=0.75)
plt.title("Distribution of Even Rolls")
plt.xlabel("Dice Faces")
plt.ylabel("Frequency")
plt.xticks([1, 2, 3, 4, 5, 6])
plt.show()
sample_space = {1, 2, 3, 4, 5, 6}
fav_subset = {num for num in sample_space if num > 4}
total = len(sample_space)
fav = len(fav_subset)
probability = fav / total
print(f"Sample Space: {sample_space}")
print(f"Fav Subset: {fav_subset}")
print(f"Probability: {probability}")
plt.hist(fav_subset, bins=np.arange(1, 8) - 0.5, edgecolor='black', alpha=0.75)
plt.title("Distribution of roll with values greater than 4")
plt.xlabel("Dice Faces")
plt.ylabel("Frequency")
plt.xticks([1, 2, 3, 4, 5, 6])
plt.show()
import matplotlib.pyplot as plt
import numpy as np
sample_space = {1, 2, 3, 4, 5, 6}
prime_subset = {num for num in sample_space if num in {2, 3, 5}}
total = len(sample_space)
fav = len(prime_subset)
probability = fav / total
print(f"Sample Space: {sample_space}")
print(f"Prime Subset (Favorable): {prime_subset}")
print(f"Probability of rolling a prime: {probability}")
plt.hist(list(prime_subset), bins=np.arange(1, 8) - 0.5, edgecolor='black', color='lightgreen', alpha=0.75)
plt.title("Distribution of Prime Number Rolls")
plt.xlabel("Dice Faces")
plt.ylabel("Frequency")
plt.xticks([1, 2, 3, 4, 5, 6])
plt.show()
Lab10
import pandas as pd
import numpy as np
# Set seed for reproducibility
np.random.seed(42)
data = {'transaction_id': range(1, 101),
'amount': np.random.uniform(10, 500, 100),
'customer_category': np.random.choice(['New Customers', 'Regular Customers', 'Premium Customers'], 100),
'region': np.random.choice(['North', 'South', 'East', 'West'], 100)}
df = pd.DataFrame(data)
df = df.drop_duplicates(subset='transaction_id')
print("Full Dataset:\n", df.head())
# 1. Simple Random Sampling (10% of data)
random_sample = df.sample(frac=0.1, random_state=42)
print("\nSimple Random Sample (10% of data):\n", random_sample)
# 2. Stratified Sampling by customer category
stratified_sample = df.groupby('customer_category').apply(lambda x: x.sample(frac=0.1, random_state=42))
stratified_sample = stratified_sample.reset_index(drop=True)
print("\nStratified Sample (10% from each customer category):\n", stratified_sample)
# 3. Calculate and print required metrics
print("\nAverage Purchase Value (Random Sample):", random_sample['amount'].mean())
print("\nAverage Purchase Value (Stratified Sample):", stratified_sample['amount'].mean())
print("\nTransaction Distribution by Category:")
print(stratified_sample['customer_category'].value_counts())
print("\nTransaction Distribution by Region:")
print(stratified_sample['region'].value_counts())
Lab11
import numpy as np
from scipy.stats import ttest_1samp
#create points1
waiting_times = [16, 14, 15, 17, 18, 14, 15, 16, 17, 15,
14, 16, 18, 19, 15, 14, 16, 17, 15, 16,
17, 18, 14, 15, 16, 17, 15, 14, 16, 18,
15, 16, 17, 14, 15, 16, 17, 18, 15, 16]
claimed_mean = 15
t_stat, p_value = ttest_1samp(waiting_times, claimed_mean)
p_value /= 2
sample_mean = np.mean(waiting_times)
if sample_mean > claimed_mean:
reject_h0 = p_value < 0.05
else:
reject_h0 = False
print(f"Sample Mean: {sample_mean:.2f}")
print(f"T-Statistic: {t_stat:.2f}")
print(f"P-Value: {p_value:.4f}")
if reject_h0:
print("Reject the null hypothesis: The average waiting time is significantly greater than 15 minutes.")
else:
print("Fail to reject the null hypothesis: There is no evidence that the average waiting time is greater than 15 minutes.")