Skip to content

Commit bc67d6b

Browse files
committed
Image files
1 parent 81f35f5 commit bc67d6b

1 file changed

Lines changed: 49 additions & 8 deletions

File tree

UniversityProject/Data_analysis.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
import matplotlib as mpl
44
import matplotlib.pyplot as plt
55
import seaborn as sns
6-
from datetime import datetime
76
import warnings
7+
from sklearn.model_selection import train_test_split, cross_val_score
8+
from sklearn.linear_model import LinearRegression, Ridge, Lasso
9+
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
10+
from sklearn.preprocessing import StandardScaler
11+
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
12+
import xgboost as xgb
13+
import lightgbm as lgb
814
warnings.filterwarnings('ignore')
915

1016
# Set high-resolution defaults
@@ -439,13 +445,6 @@
439445

440446
# STEP 11: BUILD PREDICTIVE MODELS
441447

442-
from sklearn.model_selection import train_test_split, cross_val_score
443-
from sklearn.linear_model import LinearRegression, Ridge, Lasso
444-
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
445-
from sklearn.preprocessing import StandardScaler
446-
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
447-
import xgboost as xgb
448-
import lightgbm as lgb
449448

450449
print("="*80)
451450
print("STEP 11: BUILDING PREDICTIVE MODELS FOR BENZENE")
@@ -813,3 +812,45 @@
813812
plt.close()
814813

815814
print("\n✓ Analysis Complete!")
815+
816+
# STEP 13: COMPREHENSIVE MODEL PREDICTIONS VISUALIZATION
817+
818+
print("="*80)
819+
print("STEP 13: DETAILED MODEL PREDICTIONS COMPARISON")
820+
print("="*80)
821+
822+
# Create a large comparison figure with predictions from all models
823+
fig, axes = plt.subplots(3, 2, figsize=(18, 14), dpi=200)
824+
axes = axes.flatten()
825+
826+
model_list = ['Linear Regression', 'Ridge', 'Random Forest', 'Gradient Boosting', 'XGBoost', 'LightGBM']
827+
colors_model = ['#FF6B6B', '#4ECDC4', '#95E1D3', '#F7DC6F', '#BB86FC', '#FF6B9D']
828+
829+
for idx, (model_name, color) in enumerate(zip(model_list, colors_model)):
830+
ax = axes[idx]
831+
y_pred = results[model_name]['Pred']
832+
833+
# Scatter plot: Actual vs Predicted
834+
scatter = ax.scatter(y_test.values, y_pred, alpha=0.5, s=50, c=y_test.values,
835+
cmap='viridis', edgecolors='black', linewidth=0.5)
836+
837+
# Perfect prediction line
838+
min_val = min(y_test.min(), y_pred.min())
839+
max_val = max(y_test.max(), y_pred.max())
840+
ax.plot([min_val, max_val], [min_val, max_val], 'r--', linewidth=2.5, label='Perfect Prediction', alpha=0.8)
841+
842+
# Calculate metrics
843+
r2 = results[model_name]['R2']
844+
rmse = results[model_name]['RMSE']
845+
mae = results[model_name]['MAE']
846+
ax.set_xlabel('Actual Benzene (µg/m³)', fontsize=11, fontweight='bold')
847+
ax.set_ylabel('Predicted Benzene (µg/m³)', fontsize=11, fontweight='bold')
848+
ax.set_title(f'{model_name}\nR²={r2:.3f}, RMSE={rmse:.3f}, MAE={mae:.3f}', fontsize=12, fontweight='bold')
849+
ax.legend(fontsize=10)
850+
ax.grid(True, alpha=0.3, linestyle='--')
851+
cbar = plt.colorbar(scatter, ax=ax)
852+
cbar.set_label('Actual Value', fontsize=10)
853+
plt.tight_layout()
854+
plt.savefig('08_Detailed_Model_Predictions_Comparison.png', dpi=200, bbox_inches='tight')
855+
print("✓ Saved: 08_Detailed_Model_Predictions_Comparison.png")
856+
plt.close()

0 commit comments

Comments
 (0)