-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework4.py
More file actions
128 lines (108 loc) · 3.6 KB
/
Homework4.py
File metadata and controls
128 lines (108 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.svm import SVR
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# Load dataset
housing_path = "Housing (1).csv"
df = pd.read_csv(housing_path)
# Normalize column names (handle case sensitivity / extra spaces)
df.columns = [c.strip().lower() for c in df.columns]
# Select the relevant features and target variable
features = [
'area', 'bedrooms', 'bathrooms', 'stories', 'mainroad', 'guestroom',
'basement', 'hotwaterheating', 'airconditioning', 'parking', 'prefarea'
]
target = 'price'
X = df[features]
y = df[target]
# Split dataset (80/20)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Preprocessing pipeline
# Separate numeric and categorical columns
cat_cols = [c for c in X.columns if X[c].dtype == 'object']
num_cols = [c for c in X.columns if c not in cat_cols]
preprocessor = ColumnTransformer([
('num', StandardScaler(), num_cols),
('cat', OneHotEncoder(drop='if_binary', sparse=False), cat_cols)
])
# Train and evaluate SVR models
kernels = ['linear', 'poly', 'rbf']
metrics = []
for kern in kernels:
model = Pipeline([
('pre', preprocessor),
('svr', SVR(kernel=kern))
])
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
rmse = mean_squared_error(y_test, y_pred, squared=False)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
metrics.append({
'kernel': kern,
'rmse': rmse,
'mae': mae,
'r2': r2
})
print(f"Kernel = {kern}")
print(f" RMSE: {rmse:.4f}")
print(f" MAE : {mae:.4f}")
print(f" R^2 : {r2:.4f}\n")
# Ridge Regression (baseline)
ridge_model = Pipeline([
('pre', preprocessor),
('ridge', Ridge(alpha=1.0))
])
ridge_model.fit(X_train, y_train)
y_pred_ridge = ridge_model.predict(X_test)
rmse_r = mean_squared_error(y_test, y_pred_ridge, squared=False)
mae_r = mean_absolute_error(y_test, y_pred_ridge)
r2_r = r2_score(y_test, y_pred_ridge)
metrics.append({
'kernel': 'ridge',
'rmse': rmse_r,
'mae': mae_r,
'r2': r2_r
})
print("Ridge Regression Results:")
print(f" RMSE: {rmse_r:.4f}")
print(f" MAE : {mae_r:.4f}")
print(f" R^2 : {r2_r:.4f}\n")
# Display comparison table
df_metrics = pd.DataFrame(metrics).set_index('kernel')
print(df_metrics)
# Plot performance metrics
plt.figure(figsize=(7,5))
df_metrics[['rmse','r2']].plot(kind='bar')
plt.title("Housing Price Prediction: SVR vs Ridge Regression")
plt.ylabel("Score (lower RMSE, higher R² better)")
plt.tight_layout()
plt.show()
# Scatter plot (best model)
best_kernel = df_metrics['rmse'].idxmin()
print(f"Best performing model: {best_kernel}")
if best_kernel != 'ridge':
best_model = Pipeline([
('pre', preprocessor),
('svr', SVR(kernel=best_kernel))
])
else:
best_model = ridge_model
best_model.fit(X_train, y_train)
y_best_pred = best_model.predict(X_test)
plt.figure(figsize=(6,6))
plt.scatter(y_test, y_best_pred)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--')
plt.xlabel("Actual Prices")
plt.ylabel("Predicted Prices")
plt.title(f"Predicted vs Actual Housing Prices ({best_kernel})")
plt.tight_layout()
plt.show()