Assignment
September 4, 2025
Anmol Shakya
Assignment 1 Machine Learning
2301330100037
Question 1
We are using the USA Housing dataset which contains information about housing prices along
with features such as average income, house age, number of rooms, number of bedrooms, population,
etc.
[4]:
0 79545.458574 5.682861 7.009188
1 79248.642455 6.002900 6.730821
2 61287.067179 5.865890 8.512727
3 63345.240046 7.188236 5.586729
4 59982.197226 5.040555 7.839388
Avg. Area Number of Bedrooms Area PopulaBon Price \
0 4.09 23086.800503 1.059034e+06
1 3.09 40173.072174 1.505891e+06
2 5.13 36882.159400 1.058988e+06
3 3.26 34310.242831 1.260617e+06
4 4.23 26354.109472 6.309435e+05
1
Address
0
208 Michael Ferry Apt. 674\nLaurabury, NE 3701…
1
188 Johnson Views Suite 079\nLake Kathleen, CA…
2
9127 Elizabeth Stravenue\nDanieltown, WI 06482…
3
USS Barne]\nFPO AP 44820
4
USNS Raymond\nFPO AE 09386
Question 2
Show raw data summary before and after cleaning.
Print key statistics (.describe(), .info() etc.).
Plot missing data heatmap before cleaning and after cleaning (use [Link] or similar).
[5] : # Info and statistics before cleaning
print("Dataset Info:") print([Link]())
print("\nDataset DescripBon:")
print([Link]())
# Checking missing values
print("\nMissing Values:")
print([Link]().sum())
Dataset Info:
<class '[Link]'>
RangeIndex: 5000 entries, 0 to 4999 Data
columns (total 7 columns):
# Column Non-Null Count Dtype
0 Avg. Area Income 5000 non-null float64
1 Avg. Area House Age 5000 non-null float64
2 Avg. Area Number of Rooms 5000 non-null float64
3 Avg. Area Number of Bedrooms 5000 non-null float64
4 Area PopulaBon 5000 non-null float64
5 Price 5000 non-null float64
6 Address 5000 non-null object dtypes: float64(6), object(1) memory usage: 273.6+ KB
2
None
Dataset DescripBon:
Avg. Area Income Avg. Area House Age Avg. Area Number of Rooms \
count 5000.000000 5000.000000 5000.000000
mean 68583.108984 5.977222 6.987792
std 10657.991214 0.991456 1.005833
min 17796.631190 2.644304 3.236194
25% 61480.562388 5.322283 6.299250
50% 68804.286404 5.970429 7.002902
75% 75783.338666 6.650808 7.665871
max 107701.748378 9.519088 10.759588
Avg. Area Number of Bedrooms Area PopulaBon Price
count 5000.000000 5000.000000 5.000000e+03
mean 3.981330 36163.516039 1.232073e+06
std 1.234137 9925.650114 3.531176e+05
min 2.000000 172.610686 1.593866e+04
25% 3.140000 29403.928702 9.975771e+05
50% 4.050000 36199.406689 1.232669e+06
75% 4.490000 42861.290769 1.471210e+06
max 6.500000 69621.713378 2.469066e+06
Missing Values:
Avg. Area Income 0
Avg. Area House Age 0
Avg. Area Number of Rooms 0
Avg. Area Number of Bedrooms 0
Area PopulaBon 0
Price 0
3
Handle missing values (drop/impute with justification).
Encode categorical features using appropriate techniques (Label Encoding / One-Hot Encoding).
Normalize/Standardize numerical columns if necessary.
Split your dataset into train and test (80:20 or 70:30).
##As we have checked there is no missing value USA housing dataset.
4
[7]: Avg. Area Income Avg. Area House Age Avg. Area Number of Rooms \
0 79545.458574 5.682861 7.009188
1 79248.642455 6.002900 6.730821
2 61287.067179 5.865890 8.512727
3 63345.240046 7.188236 5.586729
4 59982.197226 5.040555 7.839388
Avg. Area Number of Bedrooms Area PopulaBon Price
0 4.09 23086.800503 1.059034e+06
1 3.09 40173.072174 1.505891e+06
2 5.13 36882.159400 1.058988e+06
3 3.26 34310.242831 1.260617e+06
4 4.23 26354.109472 6.309435e+05
0.0.1 Normalization / Standardization
Since the dataset features (e.g., Income, Rooms, Population) are on very different scales, we
applied Standardization using StandardScaler.
This scales all numerical features to have mean = 0 and standard deviation = 1, ensuring
that no single feature dominates the regression model.
[9]: from [Link] import StandardScaler
# Separate features (X) and target (y)
X = [Link]("Price", axis=1) y =
df["Price"]
# Standardize features scaler =
StandardScaler()
X_scaled = scaler.fit_transform(X)
print("Shape of X:", X_scaled.shape) print("Shape of y:",
[Link])
Shape of X: (5000, 5)
Shape of y: (5000,)
5
[11]: from sklearn.model_selection import train_test_split
# Split data (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split( X_scaled, y,
test_size=0.2, random_state=42
)
print("Training set size:", X_train.shape) print("TesBng set size:",
X_test.shape)
Training set size: (4000, 5)
TesBng set size: (1000, 5)
0.0.2 Question 3: Linear Regression Implementation
We selected Price as the continuous dependent variable.
Steps performed:
- Generated a correlation heatmap to observe relationships between features and target.
- Trained a Linear Regression model on the training set.
- Plotted Predicted vs Actual values with a 45° reference line.
- Visualized model errors using a Residual plot.
- Evaluated performance using metrics: R2 Score, MAE, MSE, RMSE.
[12]: import seaborn as sns import
[Link] as plt
# Correlation heatmap plt.figure(figsize=(8,6))
corr = [Link]() # correlation matrix
[Link](corr, annot=True, fmt='.2f', cmap='coolwarm', square=True)
[Link]("CorrelaBon Heatmap (including Price)") [Link]()
6
[13]: from sklearn.linear_model import LinearRegression
# Create and train the model model =
LinearRegression() model.fit(X_train,
y_train)
# Make predictions on test data
y_pred = [Link](X_test)
print("Model trained successfully!") print("First 5
predicBons:", y_pred[:5])
Model trained successfully!
First 5 predicBons: [1308587.92699753 1237037.22949428 1243429.34030687
1228900.21360379
7
1063320.9071082 ]
[14]: import [Link] as plt
plt.figure(figsize=(6,6))
[Link]]er(y_test, y_pred, s=20, alpha=0.6) mn =
min(y_test.min(), y_pred.min()) mx = max(y_test.max(),
y_pred.max())
[Link]([mn, mx], [mn, mx], color='red', lw=2) # 45-degree reference line [Link]("Actual
Price") [Link]("Predicted Price")
[Link]("Predicted vs Actual (Linear Regression)") [Link]()
8
[15]: import seaborn as sns
# Calculate residuals
residuals = y_test - y_pred
# Residual plot plt.figure(figsize=(10,4))
# Scatter plot of residuals vs predicted [Link](1,2,1)
[Link]]er(y_pred, residuals, s=20, alpha=0.6) [Link](0,
color='red', linestyle='--') [Link]("Predicted Price")
[Link]("Residuals (Actual - Predicted)") [Link]("Residuals
vs Predicted")
9
# Histogram of residuals
[Link](1,2,2)
[Link](residuals, kde=True) [Link]("Residuals")
[Link]("Residuals DistribuBon")
plt.Bght_layout() [Link]()
[16]: from sklearn import metrics
import numpy as np
# Calculate evaluation metrics r2 = metrics.r2_score(y_test,
y_pred) mae = metrics.mean_absolute_error(y_test, y_pred)
mse = metrics.mean_squared_error(y_test, y_pred) rmse =
[Link](mse)
print("Model EvaluaBon Metrics:")
print("R² Score:", round(r2, 4))
print("MAE:", round(mae, 2)) print("MSE:",
round(mse, 2)) print("RMSE:", round(rmse,
2))
Model EvaluaBon Metrics:
R² Score: 0.918
MAE: 80879.1
MSE: 10089009300.89
RMSE: 100444.06
10
0.1 Question 4: Polynomial Regression
• Apply Polynomial Regression (degree 2 and 3) on the dataset.
• Plot regression curves to compare with Linear Regression.
• Evaluate performance using R², MAE, MSE, RMSE.
• Conclude whether Polynomial Regression improves generalization or causes overfitting.
[19]: # Degree 2 polynomial poly2 =
PolynomialFeatures(degree=2) X_train_poly2 =
poly2.fit_transform(X_train)
X_test_poly2 = [Link](X_test)
# Degree 3 polynomial poly3 =
PolynomialFeatures(degree=3) X_train_poly3 =
poly3.fit_transform(X_train)
X_test_poly3 = [Link](X_test)
[20]: # Train Polynomial Regression (Degree 2) lin_reg2 =
LinearRegression() lin_reg2.fit(X_train_poly2, y_train)
# Predict on training and test set
y_train_pred2 = lin_reg2.predict(X_train_poly2)
y_test_pred2 = lin_reg2.predict(X_test_poly2)
# Train Polynomial Regression (Degree 3) lin_reg3 =
LinearRegression() lin_reg3.fit(X_train_poly3, y_train)
# Predict on training and test set
y_train_pred3 = lin_reg3.predict(X_train_poly3)
y_test_pred3 = lin_reg3.predict(X_test_poly3)
11
mse = mean_squared_error(y_true, y_pred) rmse =
[Link](mse)
print(f"{model_name} Performance:")
print(f" R² Score: {r2:.4f}") print(f"
MAE: {mae:.2f}") print(f" MSE:
{mse:.2f}") print(f" RMSE:
{rmse:.2f}")
print("-"*40)
# Evaluate Degree 2 Polynomial Regression evaluate_model(y_test, y_test_pred2,
"Polynomial Regression (Degree 2)")
# Evaluate Degree 3 Polynomial Regression
evaluate_model(y_test, y_test_pred3, "Polynomial Regression (Degree 3)")
Polynomial Regression (Degree 2) Performance:
R² Score: 0.9179
MAE: 80886.67
MSE: 10099268148.79
RMSE: 100495.12
Polynomial Regression (Degree 3) Performance:
R² Score: 0.9174
MAE: 80942.67
MSE: 10162847726.45
RMSE: 100810.95
[ ]:
12
13