Sara Hantgan commited on
Commit
9b69ef4
·
1 Parent(s): c5efbf9

Add H2O AutoML training script

Browse files
Files changed (1) hide show
  1. train_automl_h2o.py +51 -0
train_automl_h2o.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This script loads sanitized molecular fingerprint data, runs H2O AutoML
3
+ to predict Ki values (nM) for serotonin receptor ligands, and saves the
4
+ test set predictions.
5
+
6
+ Expected input file: fingerprints_with_ki.csv
7
+ Output: h2o_test_predictions.csv
8
+ """
9
+
10
+ import h2o
11
+ import pandas as pd
12
+ from h2o.automl import H2OAutoML
13
+
14
+ # Initialize H2O cluster
15
+ h2o.init(max_mem_size="8G")
16
+
17
+ # Load fingerprinted dataset (must be in same directory or provide full path)
18
+ df = pd.read_csv("fingerprints_with_ki.csv")
19
+
20
+ # Convert to H2OFrame
21
+ df_h2o = h2o.H2OFrame(df)
22
+
23
+ # Ensure target column is numeric
24
+ df_h2o["Ki_nM"] = df_h2o["Ki_nM"].asnumeric()
25
+
26
+ # Split into training and test sets
27
+ train, test = df_h2o.split_frame(ratios=[0.8], seed=42)
28
+
29
+ # Define predictors and response
30
+ x = df_h2o.columns[:-1] # all fingerprint columns
31
+ y = "Ki_nM"
32
+
33
+ # Run AutoML
34
+ aml = H2OAutoML(
35
+ max_models=20,
36
+ max_runtime_secs=600,
37
+ seed=1,
38
+ sort_metric="RMSE"
39
+ )
40
+ aml.train(x=x, y=y, training_frame=train)
41
+
42
+ # Evaluate model
43
+ perf = aml.leader.model_performance(test_data=test)
44
+ print("\n✅ Test Set Performance:")
45
+ print("R² Score:", perf.r2())
46
+ print("RMSE:", perf.rmse())
47
+
48
+ # Save predictions
49
+ preds = aml.leader.predict(test)
50
+ h2o.export_file(preds, path="h2o_test_predictions.csv", force=True)
51
+