33import matplotlib as mpl
44import matplotlib .pyplot as plt
55import seaborn as sns
6- from datetime import datetime
76import 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
814warnings .filterwarnings ('ignore' )
915
1016# Set high-resolution defaults
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
450449print ("=" * 80 )
451450print ("STEP 11: BUILDING PREDICTIVE MODELS FOR BENZENE" )
813812plt .close ()
814813
815814print ("\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 } \n R²={ 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