-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrf.py
66 lines (51 loc) · 2 KB
/
rf.py
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
import json
import joblib
import numpy as np
import pandas as pd
from sklearn.metrics import r2_score
from sklearn.metrics import mean_absolute_error as mae
from sklearn.metrics import median_absolute_error as dae
from sklearn.metrics import mean_absolute_percentage_error as mape
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
# For reproducible results.
np.random.seed(1)
# Load the names of materials used as train data.
with open('data/MOFs/batch_train/clean_names.json', 'r') as fhand:
mof_train = json.load(fhand)['names']
# Load the names of materials used as test data.
with open('data/MOFs/batch_val_test/clean_names.json', 'r') as fhand:
mof_test = json.load(fhand)['names'][5000:]
column_names = [
'MOFname', 'CO2_uptake_P0.15bar_T298K [mmol/g]',
'volume [A^3]', 'weight [u]', 'surface_area [m^2/g]',
'void_fraction', 'void_volume [cm^3/g]', 'largest_free_sphere_diameter [A]',
'largest_included_sphere_along_free_sphere_path_diameter [A]',
'largest_included_sphere_diameter [A]',
]
# Load features and labels.
df = pd.read_csv('data/MOFs/all_MOFs_screening_data.csv')
df.set_index('MOFname', inplace=True)
features = column_names[2:]
target = column_names[1]
reg = RandomForestRegressor(n_jobs=-1)
df_test = df.loc[mof_test]
df_test.replace([np.inf, -np.inf], np.nan, inplace=True)
df_test.dropna(inplace=True)
X_test = df_test.loc[:, features]
y_test = df_test.loc[:, target]
train_sizes = [
100, 500, 1_000, 2_000, 5_000,
10_000, 15_000, 20_000, len(mof_train)
]
for size in train_sizes:
df_train_tmp = df.loc[mof_train[:size]]
df_train = df_train_tmp.replace([np.inf, -np.inf], np.nan)
df_train.dropna(inplace=True)
X_train = df_train.loc[:, features]
y_train = df_train.loc[:, target]
reg.fit(X_train, y_train)
print(size, r2_score(y_test, reg.predict(X_test)))
# Save the trained model.
#with open('rf_model.pkl', 'wb') as fhand:
# joblib.dump(reg, fhand)