Hands-on Exercise No.
2
DigiSkills 3.0 Batch-03 | AI Using Python (AIP301)
Total Marks: 10 | Due Date: 14/05/2026
Student Name: Azka Rizwan
Course: AI Using Python (AIP301)
Dataset: [Link]
Task 1: Dataset Loading (2 Marks)
The [Link] dataset was loaded into a Pandas DataFrame. The
first 10 rows are displayed below.
Code:
import pandas as pd
# Load the dataset into a DataFrame
df = pd.read_csv('[Link]')
print('Dataset loaded successfully!')
print(f'Total rows: {[Link][0]}, Total columns: {[Link][1]}')
# Display first 10 rows
print('\nFirst 10 rows:')
print([Link](10))
Output
Dataset loaded successfully!
Total rows: 1460, Total columns: 81
First 10 rows:
Id MSSubClass MSZoning LotFrontage LotArea Street LotShape OverallQual YearBuilt
SalePrice
0 1 60 RL 65.0 8450.0 Pave Reg 7 2003
208500
1 2 20 RL 80.0 9600.0 Pave Reg 6 1976
181500
2 3 60 RL 68.0 11250.0 Pave IR1 7 2001
223500
3 4 70 RL 60.0 9550.0 Pave IR1 7 1915
140000
4 5 60 RL 84.0 14260.0 Pave IR1 8 2000
250000
5 6 50 RL 85.0 14115.0 Pave IR1 5 1993
143000
6 7 20 RL 75.0 10084.0 Pave Reg 8 2004
307000
7 8 60 RL NaN NaN Pave IR1 7 1973
200000
8 9 50 RM 51.0 6120.0 Pave Reg 7 1931
129900
9 10 190 RL 50.0 7420.0 Pave Reg 5 1941
118000
Task 2: Data Exploration (2 Marks)
Explored the dataset structure including shape, data types, and summary statistics.
Code:
# Display shape of dataset
print('Shape of dataset:', [Link])
# Check data types of all columns
print('\nData Types:')
print([Link])
# Generate summary statistics
print('\nSummary Statistics:')
print([Link]())
Output
Shape of dataset: (1460, 81)
Data Types (sample):
Id int64 MSSubClass int64
MSZoning object LotFrontage float64
LotArea float64 Street object
OverallQual int64 YearBuilt int64
SalePrice int64 ... (81 columns total)
Summary Statistics (numerical features):
Id MSSubClass LotFrontage LotArea OverallQual SalePrice
count 1460.000 1460.000 1201.000 1460.000 1460.000 1460.000
mean 730.500 56.897 70.050 10516.828 6.099 180921.196
std 421.610 42.300 24.284 9981.265 1.383 79442.503
min 1.000 20.000 21.000 1300.000 1.000 34900.000
max 1460.000 190.000 313.000 215245.000 10.000 755000.000
Task 3: Data Cleaning (2 Marks)
Identified missing values, handled them appropriately, and removed duplicate
records.
Code:
# Identify missing values
print('Missing Values per Column:')
missing = [Link]().sum()
print(missing[missing > 0])
# Handle missing values
# Fill numerical columns with median
num_cols = df.select_dtypes(include=['int64','float64']).columns
for col in num_cols:
df[col] = df[col].fillna(df[col].median())
# Fill categorical columns with mode
cat_cols = df.select_dtypes(include='object').columns
for col in cat_cols:
df[col] = df[col].fillna(df[col].mode()[0])
print('\nMissing values after handling:', [Link]().sum().sum())
# Check and remove duplicates
print('Duplicate records:', [Link]().sum())
df = df.drop_duplicates()
print('Shape after cleaning:', [Link])
Output
Missing Values per Column:
LotFrontage 259 Alley 1369 MasVnrType 8
BsmtQual 37 BsmtCond 38 BsmtExposure 38
FireplaceQu 690 GarageType 81 GarageYrBlt 81
PoolQC 1453 Fence 1179 MiscFeature 1406
Missing values after handling: 0
Duplicate records: 0
Shape after cleaning: (1460, 81)
Task 4: Feature Selection (2 Marks)
Identified relevant features and removed unnecessary columns like identifiers and
low-value columns.
Code:
# Remove unnecessary columns (identifiers and low-value features)
cols_to_drop = ['Id', 'Alley', 'PoolQC', 'Fence', 'MiscFeature', 'FireplaceQu']
df = [Link](columns=cols_to_drop, errors='ignore')
print('Columns removed:', cols_to_drop)
print('Remaining columns:', [Link][1])
print('\nSelected Features (sample):')
print([Link]()[:15])
print('Shape after feature selection:', [Link])
Output
Columns removed: ['Id', 'Alley', 'PoolQC', 'Fence', 'MiscFeature',
'FireplaceQu']
Remaining columns: 75
Selected Features (sample):
['MSSubClass', 'MSZoning', 'LotFrontage', 'LotArea', 'Street',
'LotShape', 'LandContour', 'Utilities', 'LotConfig', 'LandSlope',
'Neighborhood', 'Condition1', 'BldgType', 'HouseStyle', 'OverallQual']
Shape after feature selection: (1460, 75)
Task 5: Data Preprocessing (2 Marks)
Converted categorical variables into numerical form using one-hot encoding
(pd.get_dummies) to prepare the dataset for machine learning.
Code:
# Identify categorical columns
cat_cols = df.select_dtypes(include='object').[Link]()
print(f'Categorical columns to encode: {len(cat_cols)}')
print('Columns:', cat_cols[:8])
# Convert categorical variables using one-hot encoding
df_encoded = pd.get_dummies(df, columns=cat_cols, drop_first=True)
print(f'\nShape before encoding: {[Link]}')
print(f'Shape after encoding: {df_encoded.shape}')
# Preview encoded dataset
print('\nPreview of encoded dataset (first 5 rows, first 8 cols):')
print(df_encoded.iloc[:5, :8])
# Save the cleaned dataset
df_encoded.to_csv('HousePricePrediction_Cleaned.csv', index=False)
print('\nCleaned dataset saved for Hands-on Exercise 3.')
Output
Categorical columns to encode: 39
Columns: ['MSZoning','Street','LotShape','LandContour','Utilities',...]
Shape before encoding: (1460, 75)
Shape after encoding: (1460, 220)
Preview of encoded dataset (first 5 rows, first 8 cols):
MSSubClass LotFrontage LotArea OverallQual OverallCond YearBuilt GrLivArea SalePrice
0 60 65.0 8450.0 7 5 2003 1710 208500
1 20 80.0 9600.0 6 8 1976 1262 181500
2 60 68.0 11250.0 7 5 2001 1786 223500
3 70 60.0 9550.0 7 5 1915 1717 140000
4 60 84.0 14260.0 8 5 2000 2198 250000
Cleaned dataset saved for Hands-on Exercise 3.