Repository navigation
Expand file tree
/
Copy pathtempCodeRunnerFile.python
More file actions
452 lines (360 loc) · 17 KB
/
Copy pathtempCodeRunnerFile.python
File metadata and controls
452 lines (360 loc) · 17 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression, Lasso, ElasticNet, Ridge
from sklearn.ensemble import StackingRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from scipy import stats
import warnings
warnings.filterwarnings('ignore')
file_path = "C:\\Users\\adity\\pythonlang\\pythonproj.xlsx"
try:
df = pd.read_excel(file_path)
print("File Loaded Successfully!\n")
print(df.iloc[:20,:])
except Exception as e:
print(f"Error loading file: {e}")
exit()
def total_price(df):
required_area = int(input("Enter the required area in sqft: "))
if 'AveragePrice' in df.columns:
df['price_per_sqft'] = df['AveragePrice']
df['total_price'] = df['price_per_sqft'] * required_area
return df[['Areas', 'total_price']].groupby('Areas').mean().sort_values(by='total_price', ascending=False)
else:
print("missing_data")
return df
def calc_price_stats(df):
if 'AveragePrice' in df.columns:
area_stats = df.groupby('Areas')['AveragePrice'].agg(['mean', 'median', 'std', 'min', 'max'])
area_stats['coef_var'] = (area_stats['std'] / area_stats['mean']) * 100
return area_stats.sort_values('mean', ascending=False)
return None
gdp = [7.5, 7.1, 6.8, 6.1, 4.2, 7.3, 8.7, 6.8, 6.0]
ur = [5.0, 4.9, 4.7, 4.5, 6.1, 7.0, 6.6, 6.3, 5.9]
money = [10.5, 11.2, 12, 12.8, 13.8, 14.5, 16.3, 17.5, 18.0]
inflation_rate = [5.5, 5.1, 4.8, 4.6, 4.8, 6.6, 5.3, 6.8, 6.0]
interest_rt = [6.5, 6.2, 6.0, 5.8, 5.5, 6.8, 7.1, 7.3, 7.0]
indf = pd.DataFrame({
"gdp growth": gdp,
"unemployment": ur,
"money_supp": money,
"interest_rate": interest_rt
})
print("\nEconomic Indicators Dataset:")
print(indf)
indf['gdp_money_ratio'] = indf['gdp growth'] / indf['money_supp']
indf['unemployment_interest'] = indf['unemployment'] * indf['interest_rate']
poly_feat = PolynomialFeatures(degree=2, include_bias=False)
X_raw = indf.drop(['gdp_money_ratio', 'unemployment_interest', 'oil_money_index'], axis=1)
X_poly = poly_feat.fit_transform(X_raw)
scaler = StandardScaler()
X = scaler.fit_transform(indf)
y = inflation_rate
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
base_lasso = Lasso(alpha=0.1, random_state=42, max_iter=5000)
base_xgb = XGBRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
base_ridge = Ridge(alpha=1.0, random_state=42)
base_gbm = GradientBoostingRegressor(n_estimators=50, learning_rate=0.1, random_state=42)
stacked_model = StackingRegressor(
estimators=[
('lasso', base_lasso),
('xgb', base_xgb),
('ridge', base_ridge),
('gbm', base_gbm)
],
final_estimator=ElasticNet(alpha=0.1, l1_ratio=0.5, random_state=42, max_iter=5000),
cv=3
)
stacked_model.fit(X_train, y_train)
y_pred = stacked_model.predict(X_test)
y_train_pred = stacked_model.predict(X_train)
r2_test = r2_score(y_test, y_pred)
mse_test = mean_squared_error(y_test, y_pred)
mae_test = mean_absolute_error(y_test, y_pred)
rmse_test = np.sqrt(mse_test)
r2_train = r2_score(y_train, y_train_pred)
mse_train = mean_squared_error(y_train, y_train_pred)
print("\n=== Model Performance Metrics ===")
print(f"Train R2 Score: {r2_train:.4f}")
print(f"Test R2 Score: {r2_test:.4f}")
print(f"Test MSE: {mse_test:.4f}")
print(f"Test RMSE: {rmse_test:.4f}")
print(f"Test MAE: {mae_test:.4f}")
cv_scores = cross_val_score(stacked_model, X, y, cv=3, scoring='r2')
print(f"Cross-Validation R2 Scores: {cv_scores}")
print(f"Mean CV R2: {cv_scores.mean():.4f} (+/- {cv_scores.std() * 2:.4f})")
residuals = y_test - y_pred
residuals_train = y_train - y_train_pred
def plot_inflation():
plt.figure(figsize=(10, 5))
sns.lineplot(x=range(len(y_test)), y=y_test, label='Actual', marker='o', linewidth=2)
sns.lineplot(x=range(len(y_pred)), y=y_pred, label='Predicted', marker='D', linewidth=2)
plt.title("Inflation Rate Prediction - Test Set", fontsize=14, fontweight='bold')
plt.xlabel("Sample Index", fontsize=12)
plt.ylabel("Inflation Rate (%)", fontsize=12)
plt.legend(fontsize=10)
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()
plot_inflation()
def plot_residuals():
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
axes[0].scatter(y_pred, residuals, alpha=0.7, edgecolors='k', s=80)
axes[0].axhline(y=0, color='r', linestyle='--', linewidth=2)
axes[0].set_xlabel('Predicted Values', fontsize=11)
axes[0].set_ylabel('Residuals', fontsize=11)
axes[0].set_title('Residual Plot', fontsize=12, fontweight='bold')
axes[0].grid(alpha=0.3)
stats.probplot(residuals, dist="norm", plot=axes[1])
axes[1].set_title('Q-Q Plot', fontsize=12, fontweight='bold')
axes[1].grid(alpha=0.3)
plt.tight_layout()
plt.show()
plot_residuals()
def plot_feature_importance():
base_models = ['lasso', 'xgb', 'ridge', 'gbm']
feat_names = indf.columns
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
axes = axes.ravel()
for idx, model_name in enumerate(base_models):
model = stacked_model.named_estimators_[model_name]
if hasattr(model, 'feature_importances_'):
importances = model.feature_importances_
elif hasattr(model, 'coef_'):
importances = np.abs(model.coef_)
else:
continue
indices = np.argsort(importances)[::-1]
axes[idx].bar(range(len(importances)), importances[indices], alpha=0.7)
axes[idx].set_xticks(range(len(importances)))
axes[idx].set_xticklabels([feat_names[i] for i in indices], rotation=45, ha='right')
axes[idx].set_title(f'{model_name.upper()} Feature Importance', fontweight='bold')
axes[idx].set_ylabel('Importance')
axes[idx].grid(axis='y', alpha=0.3)
plt.tight_layout()
plt.show()
plot_feature_importance()
def plot_model_comparison():
models_perf = {
'Lasso': base_lasso,
'XGBoost': base_xgb,
'Ridge': base_ridge,
'GBM': base_gbm,
'Stacked': stacked_model
}
model_names = []
r2_scores = []
rmse_scores = []
for name, mdl in models_perf.items():
mdl_temp = mdl
if name != 'Stacked':
mdl_temp.fit(X_train, y_train)
preds = mdl_temp.predict(X_test)
r2 = r2_score(y_test, preds)
rmse = np.sqrt(mean_squared_error(y_test, preds))
model_names.append(name)
r2_scores.append(r2)
rmse_scores.append(rmse)
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
axes[0].bar(model_names, r2_scores, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'], alpha=0.8)
axes[0].set_ylabel('R² Score', fontsize=11)
axes[0].set_title('Model R² Comparison', fontsize=12, fontweight='bold')
axes[0].set_ylim([0, 1])
axes[0].grid(axis='y', alpha=0.3)
axes[1].bar(model_names, rmse_scores, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'], alpha=0.8)
axes[1].set_ylabel('RMSE', fontsize=11)
axes[1].set_title('Model RMSE Comparison', fontsize=12, fontweight='bold')
axes[1].grid(axis='y', alpha=0.3)
plt.tight_layout()
plt.show()
plot_model_comparison()
def Price_vs_inflation():
Prices = np.linspace(50, 100, len(y_pred))
Time = np.linspace(5, 10, len(y_pred))
Time_mesh, Prices_mesh = np.meshgrid(Time, Prices)
Inflation_mesh = np.tile(y_pred, (len(Prices), 1)).T
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(Prices_mesh, Time_mesh, Inflation_mesh, cmap='viridis', alpha=0.8)
ax.set_xlabel('Average Price (Lakhs)', fontsize=11)
ax.set_ylabel('Years', fontsize=11)
ax.set_zlabel('Predicted Inflation Rate', fontsize=11)
ax.set_title('Inflated Price Surface', fontsize=13, fontweight='bold')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.tight_layout()
plt.show()
Price_vs_inflation()
def plot_inflation_trends():
years_forward = 15
base_inflation = np.mean(y_pred)
time_horizon = np.arange(1, years_forward + 1)
inflation_trend = base_inflation + np.random.normal(0, 0.3, years_forward).cumsum() * 0.1
inflation_upper = inflation_trend + 1.5
inflation_lower = inflation_trend - 1.5
plt.figure(figsize=(12, 6))
plt.plot(time_horizon, inflation_trend, 'b-', linewidth=2, label='Expected Inflation')
plt.fill_between(time_horizon, inflation_lower, inflation_upper, alpha=0.3, label='Confidence Interval')
plt.axhline(y=base_inflation, color='r', linestyle='--', linewidth=1.5, label=f'Current Avg: {base_inflation:.2f}%')
plt.xlabel('Years Ahead', fontsize=12)
plt.ylabel('Inflation Rate (%)', fontsize=12)
plt.title('Long-term Inflation Forecast', fontsize=14, fontweight='bold')
plt.legend(fontsize=10)
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()
plot_inflation_trends()
print("\n=== Future Price Prediction ===")
user_loc = input("Enter the location: ")
user_area = int(input("Enter desired area: "))
price = df.loc[df['Areas'] == user_loc, 'AveragePrice'].iloc[0]
area_price = price * user_area
print(f"\nLocation: {user_loc}")
print(f"Area: {user_area} sqft")
print(f"Current Price: ₹{area_price:,.2f}")
avg_inflation = np.mean(y_pred) / 100
GrowthRate = 0.08
real_growth = GrowthRate - avg_inflation
future_years = [3, 5, 7, 10, 15]
future_prices = []
inflation_adjusted = []
for years in future_years:
future_price = area_price * ((1 + real_growth) ** years)
inflated_price = area_price * ((1 + avg_inflation) ** years)
future_prices.append(future_price)
inflation_adjusted.append(inflated_price)
print(f"Price after {years} years: ₹{round(future_price):,} (Real Growth)")
print(f" - Pure Inflation Impact: ₹{round(inflated_price):,}")
def plot_price_difference():
years = future_years
current_price = area_price
x_pos = np.arange(len(years))
width = 0.35
fig, ax = plt.subplots(figsize=(12, 6))
bars1 = ax.bar(x_pos - width/2, [current_price]*len(years), width, label='Current Price', alpha=0.7)
bars2 = ax.bar(x_pos + width/2, future_prices, width, label='Predicted Price', alpha=0.7)
ax.set_ylabel('Price (INR)', fontsize=12)
ax.set_title('Property Price Projection Over Time', fontsize=14, fontweight='bold')
ax.set_xticks(x_pos)
ax.set_xticklabels([f'{yr} years' for yr in years])
ax.legend(fontsize=10)
ax.grid(axis='y', alpha=0.3)
for bar in bars2:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height,
f'₹{int(height/100000)}L',
ha='center', va='bottom', fontsize=9)
plt.tight_layout()
plt.show()
plot_price_difference()
def plot_price_growth_curve():
years_range = np.linspace(0, 15, 100)
price_curve = area_price * ((1 + real_growth) ** years_range)
inflation_curve = area_price * ((1 + avg_inflation) ** years_range)
nominal_curve = area_price * ((1 + GrowthRate) ** years_range)
plt.figure(figsize=(12, 6))
plt.plot(years_range, price_curve, 'g-', linewidth=2.5, label='Real Growth (Growth - Inflation)')
plt.plot(years_range, inflation_curve, 'r--', linewidth=2, label='Pure Inflation Impact')
plt.plot(years_range, nominal_curve, 'b:', linewidth=2, label='Nominal Growth (No Adjustment)')
plt.scatter(future_years, future_prices, s=100, c='green', zorder=5, edgecolors='black')
plt.xlabel('Years', fontsize=12)
plt.ylabel('Property Price (INR)', fontsize=12)
plt.title('Property Price Growth Trajectories', fontsize=14, fontweight='bold')
plt.legend(fontsize=10)
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()
plot_price_growth_curve()
def plot_area_price_heatmap(df):
area_price_df = df[['Areas', 'AveragePrice']].groupby('Areas').mean()
area_price_df = area_price_df.sort_values('AveragePrice', ascending=False)
plt.figure(figsize=(80, 6))
sns.heatmap(area_price_df.T, annot=True, fmt=".0f", cmap="YlOrRd", cbar=True, linewidths=0.8)
plt.title("Average Price per Sqft by Area", fontsize=16, fontweight='bold')
plt.xticks(rotation=45, ha='right', fontsize=10)
plt.tight_layout()
plt.show()
plot_area_price_heatmap(df)
def plot_top_areas(df, top_n=15):
area_stats = df.groupby('Areas')['AveragePrice'].agg(['mean', 'count']).reset_index()
area_stats = area_stats.nlargest(top_n, 'mean')
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
sns.barplot(data=area_stats, y='Areas', x='mean', ax=axes[0], palette='rocket')
axes[0].set_xlabel('Average Price per Sqft (INR)', fontsize=11)
axes[0].set_ylabel('Areas', fontsize=11)
axes[0].set_title(f'Top {top_n} Most Expensive Areas', fontsize=12, fontweight='bold')
axes[0].grid(axis='x', alpha=0.3)
sns.scatterplot(data=area_stats, x='mean', y='count', size='mean',
sizes=(50, 500), alpha=0.6, ax=axes[1], legend=False)
axes[1].set_xlabel('Average Price per Sqft (INR)', fontsize=11)
axes[1].set_ylabel('Number of Properties', fontsize=11)
axes[1].set_title('Price vs Property Count', fontsize=12, fontweight='bold')
axes[1].grid(alpha=0.3)
plt.tight_layout()
plt.show()
plot_top_areas(df)
def plot_price_distribution(df):
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
axes[0, 0].hist(df['AveragePrice'], bins=50, color='skyblue', edgecolor='black', alpha=0.7)
axes[0, 0].set_xlabel('Price per Sqft (INR)', fontsize=11)
axes[0, 0].set_ylabel('Frequency', fontsize=11)
axes[0, 0].set_title('Price Distribution', fontsize=12, fontweight='bold')
axes[0, 0].grid(alpha=0.3)
df['AveragePrice'].plot(kind='box', ax=axes[0, 1], vert=False)
axes[0, 1].set_xlabel('Price per Sqft (INR)', fontsize=11)
axes[0, 1].set_title('Price Boxplot', fontsize=12, fontweight='bold')
axes[0, 1].grid(alpha=0.3)
df['AveragePrice'].plot(kind='density', ax=axes[1, 0], linewidth=2, color='darkblue')
axes[1, 0].set_xlabel('Price per Sqft (INR)', fontsize=11)
axes[1, 0].set_ylabel('Density', fontsize=11)
axes[1, 0].set_title('Price Density Plot', fontsize=12, fontweight='bold')
axes[1, 0].grid(alpha=0.3)
price_percentiles = df['AveragePrice'].quantile([0.25, 0.5, 0.75, 0.9, 0.95])
axes[1, 1].barh(range(len(price_percentiles)), price_percentiles.values, color='coral', alpha=0.7)
axes[1, 1].set_yticks(range(len(price_percentiles)))
axes[1, 1].set_yticklabels(['25th', '50th', '75th', '90th', '95th'])
axes[1, 1].set_xlabel('Price per Sqft (INR)', fontsize=11)
axes[1, 1].set_title('Price Percentiles', fontsize=12, fontweight='bold')
axes[1, 1].grid(axis='x', alpha=0.3)
plt.tight_layout()
plt.show()
plot_price_distribution(df)
def plot_correlation_matrix():
corr_df = indf.corr()
plt.figure(figsize=(10, 8))
mask = np.triu(np.ones_like(corr_df, dtype=bool))
sns.heatmap(corr_df, mask=mask, annot=True, fmt='.2f', cmap='coolwarm',
center=0, square=True, linewidths=1, cbar_kws={"shrink": 0.8})
plt.title('Economic Indicators Correlation Matrix', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.show()
plot_correlation_matrix()
def plot_inflation_components():
fig, axes = plt.subplots(2, 2, figsize=(14, 9))
axes = axes.ravel()
components = ['gdp growth', 'unemployment', 'money_supp', 'interest_rate']
for idx, comp in enumerate(components):
axes[idx].scatter(indf[comp], inflation_rate, alpha=0.6, s=80, edgecolors='k')
axes[idx].set_xlabel(comp.replace('_', ' ').title(), fontsize=10)
axes[idx].set_ylabel('Inflation Rate', fontsize=10)
axes[idx].set_title(f'Inflation vs {comp.replace("_", " ").title()}', fontsize=11, fontweight='bold')
axes[idx].grid(alpha=0.3)
z = np.polyfit(indf[comp], inflation_rate, 1)
p = np.poly1d(z)
axes[idx].plot(indf[comp], p(indf[comp]), "r--", alpha=0.8, linewidth=2)
plt.tight_layout()
plt.show()
plot_inflation_components()
stats_summary = calc_price_stats(df)
if stats_summary is not None:
print("\n=== Area-wise Price Statistics (Top 10) ===")
print(stats_summary.head(10))
print("\n=== Analysis Complete ===")
print(f"Total visualizations generated: 13")
print(f"Model used: Stacked Ensemble (Lasso + XGBoost + Ridge + GBM -> ElasticNet)")
print(f"Average predicted inflation: {np.mean(y_pred):.2f}%")