6868
6969def create_reduced_ml_function (func_class , reduced_space ):
7070 """Create ML function instance with reduced search space for faster evaluation."""
71- # Create instance
72- func = func_class (metric = "score" )
71+ # Create instance with appropriate metric
72+ if 'Classifier' in func_class .__name__ :
73+ func = func_class (metric = "accuracy" ) # Use accuracy for classification
74+ else :
75+ func = func_class (metric = "neg_mean_squared_error" ) # Use MSE for regression
7376
7477 # Override search_space method with reduced space
7578 original_search_space = func .search_space
@@ -92,71 +95,58 @@ def generate_ml_plots():
9295 print (f"Processing { func_name } ..." )
9396 print (f"{ '=' * 50 } " )
9497
95- try :
96- # Create function instance with reduced search space
97- func_class = config ['class' ]
98- reduced_space = config ['reduced_search_space' ]
99- ml_func = create_reduced_ml_function (func_class , reduced_space )
98+ # Create function instance with reduced search space
99+ func_class = config ['class' ]
100+ reduced_space = config ['reduced_search_space' ]
101+ ml_func = create_reduced_ml_function (func_class , reduced_space )
102+
103+ # Create function-specific output directory
104+ func_output_dir = os .path .join (ml_output_dir , ml_func ._name_ )
105+ os .makedirs (func_output_dir , exist_ok = True )
106+
107+ print (f"Search space: { ml_func .search_space ()} " )
108+
109+ # 1. Generate hyperparameter vs hyperparameter plots
110+ print ("\n 1. Generating hyperparameter interaction plots..." )
111+ for param1 , param2 in config ['hyperparameter_pairs' ]:
112+ print (f" Creating { param1 } vs { param2 } plot..." )
100113
101- # Create function-specific output directory
102- func_output_dir = os .path .join (ml_output_dir , ml_func ._name_ )
103- os .makedirs (func_output_dir , exist_ok = True )
114+ fig = plotly_ml_hyperparameter_heatmap (
115+ ml_func , param1 , param2 ,
116+ title = f"{ ml_func .name } - { param1 } vs { param2 } "
117+ )
104118
105- print (f"Search space: { ml_func .search_space ()} " )
119+ # Save as image
120+ output_path = os .path .join (func_output_dir , f"{ param1 } _vs_{ param2 } _heatmap.jpg" )
121+ fig .write_image (output_path , format = "jpeg" , width = 900 , height = 700 )
106122
107- # 1. Generate hyperparameter vs hyperparameter plots
108- print ( " \n 1. Generating hyperparameter interaction plots... " )
109- for param1 , param2 in config [ 'hyperparameter_pairs' ]:
110- print ( f" Creating { param1 } vs { param2 } plot..." )
111-
112- try :
113- fig = plotly_ml_hyperparameter_heatmap (
114- ml_func , param1 , param2 ,
115- title = f" { ml_func . name } - { param1 } vs { param2 } "
116- )
117-
118- # Save as image
119- output_path = os . path . join ( func_output_dir , f" { param1 } _vs_ { param2 } _heatmap.jpg" )
120- fig . write_image ( output_path , format = "jpeg" , width = 900 , height = 700 )
121-
122- # Also save to main images directory for README
123- main_output_path = os . path . join ( output_dir , f" { ml_func . _name_ } _ { param1 } _vs_ { param2 } _heatmap.jpg" )
124- fig . write_image ( main_output_path , format = "jpeg" , width = 900 , height = 700 )
125-
126- print ( f" ✓ Saved { param1 } vs { param2 } heatmap" )
127-
128- except Exception as e :
129- print ( f" ✗ Error creating { param1 } vs { param2 } plot: { e } " )
123+ # Also save to main images directory for README
124+ main_output_path = os . path . join ( output_dir , f" { ml_func . _name_ } _ { param1 } _vs_ { param2 } _heatmap.jpg " )
125+ fig . write_image ( main_output_path , format = "jpeg" , width = 900 , height = 700 )
126+
127+ print ( f" ✓ Saved { param1 } vs { param2 } heatmap" )
128+
129+ # 2. Generate dataset vs hyperparameter plots
130+ print ( " \n 2. Generating dataset analysis plots..." )
131+ for hyperparameter in config [ 'dataset_analyses' ]:
132+ print ( f" Creating dataset vs { hyperparameter } plot..." )
133+
134+ fig = plotly_dataset_hyperparameter_analysis (
135+ ml_func , hyperparameter ,
136+ title = f" { ml_func . name } - Dataset vs { hyperparameter } "
137+ )
138+
139+ # Save as image
140+ output_path = os . path . join ( func_output_dir , f"dataset_vs_ { hyperparameter } _analysis.jpg" )
141+ fig . write_image ( output_path , format = "jpeg" , width = 1000 , height = 700 )
142+
143+ # Also save to main images directory for README
144+ main_output_path = os . path . join ( output_dir , f" { ml_func . _name_ } _dataset_vs_ { hyperparameter } _analysis.jpg" )
145+ fig . write_image ( main_output_path , format = "jpeg" , width = 1000 , height = 700 )
130146
131- # 2. Generate dataset vs hyperparameter plots
132- print ("\n 2. Generating dataset analysis plots..." )
133- for hyperparameter in config ['dataset_analyses' ]:
134- print (f" Creating dataset vs { hyperparameter } plot..." )
147+ print (f" ✓ Saved dataset vs { hyperparameter } analysis" )
135148
136- try :
137- fig = plotly_dataset_hyperparameter_analysis (
138- ml_func , hyperparameter ,
139- title = f"{ ml_func .name } - Dataset vs { hyperparameter } "
140- )
141-
142- # Save as image
143- output_path = os .path .join (func_output_dir , f"dataset_vs_{ hyperparameter } _analysis.jpg" )
144- fig .write_image (output_path , format = "jpeg" , width = 1000 , height = 700 )
145-
146- # Also save to main images directory for README
147- main_output_path = os .path .join (output_dir , f"{ ml_func ._name_ } _dataset_vs_{ hyperparameter } _analysis.jpg" )
148- fig .write_image (main_output_path , format = "jpeg" , width = 1000 , height = 700 )
149-
150- print (f" ✓ Saved dataset vs { hyperparameter } analysis" )
151-
152- except Exception as e :
153- print (f" ✗ Error creating dataset vs { hyperparameter } plot: { e } " )
154-
155- print (f"✓ Completed { func_name } analysis" )
156-
157- except Exception as e :
158- print (f"✗ Error processing { func_name } : { e } " )
159- continue
149+ print (f"✓ Completed { func_name } analysis" )
160150
161151 print (f"\n ✓ ML plot generation complete! Images saved to:" )
162152 print (f" - Individual function plots: { ml_output_dir } " )
@@ -166,37 +156,37 @@ def generate_sample_plots():
166156 """Generate a few sample plots for testing."""
167157 print ("Generating sample ML plots for testing..." )
168158
169- # Test with KNeighborsClassifier (smallest/fastest)
170- try :
171- ml_func = create_reduced_ml_function (
172- KNeighborsClassifierFunction ,
173- {
174- 'n_neighbors' : [ 3 , 10 , 20 ], # Very small for testing
175- 'algorithm ' : ['auto' , 'ball_tree' ],
176- 'cv ' : [3 ],
177- }
178- )
179-
180- print ( "Creating sample hyperparameter plot..." )
181- fig1 = plotly_ml_hyperparameter_heatmap (
182- ml_func , 'n_neighbors' , 'algorithm' ,
183- title = "Sample: KNN Hyperparameter Analysis"
184- )
185- fig1 . write_image ( os . path . join ( output_dir , "sample_knn_hyperparams.jpg" ),
186- format = "jpeg" , width = 900 , height = 700 )
187-
188- print ( "Creating sample dataset analysis plot..." )
189- fig2 = plotly_dataset_hyperparameter_analysis (
190- ml_func , 'n_neighbors' ,
191- title = "Sample: Dataset vs n_neighbors"
192- )
193- fig2 . write_image ( os . path . join ( output_dir , "sample_knn_datasets.jpg" ),
194- format = "jpeg" , width = 1000 , height = 700 )
195-
196- print ( "✓ Sample plots generated successfully!" )
197-
198- except Exception as e :
199- print (f"✗ Error generating sample plots: { e } " )
159+ # Create with proper metric
160+ ml_func = KNeighborsClassifierFunction ( metric = "accuracy" )
161+
162+ # Override search space with small values for testing
163+ def reduced_search_space_method ( ** kwargs ):
164+ return {
165+ 'n_neighbors ' : [3 , 10 , 20 ], # Very small for testing
166+ 'algorithm ' : ['auto' , 'ball_tree' ],
167+ 'cv' : [ 3 ],
168+ 'dataset' : ml_func . dataset_default # Keep all datasets
169+ }
170+
171+ ml_func . search_space = reduced_search_space_method
172+
173+ print ( "Creating sample hyperparameter plot..." )
174+ fig1 = plotly_ml_hyperparameter_heatmap (
175+ ml_func , 'n_neighbors' , 'algorithm' ,
176+ title = "Sample: KNN Hyperparameter Analysis"
177+ )
178+ fig1 . write_image ( os . path . join ( output_dir , "sample_knn_hyperparams.jpg" ),
179+ format = "jpeg" , width = 900 , height = 700 )
180+
181+ print ( "Creating sample dataset analysis plot..." )
182+ fig2 = plotly_dataset_hyperparameter_analysis (
183+ ml_func , 'n_neighbors' ,
184+ title = "Sample: Dataset vs n_neighbors"
185+ )
186+ fig2 . write_image ( os . path . join ( output_dir , "sample_knn_datasets.jpg" ),
187+ format = "jpeg" , width = 1000 , height = 700 )
188+
189+ print ("✓ Sample plots generated successfully! " )
200190
201191if __name__ == "__main__" :
202192 # Check if we should generate sample plots first (faster for testing)
0 commit comments