-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticnet_sklearn_cv.seq
43 lines (39 loc) · 1.45 KB
/
elasticnet_sklearn_cv.seq
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
import sys
import python
pydef get_all_training_files_in_folder(directory) -> list[tuple[str, str]]:
import os
files = []
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
if os.path.isfile(item_path):
if item.endswith(".csv"):
files.append((item_path, item_path[:-4] + ".labels"))
return files
pydef training(directory, train_files):
from sklearn.linear_model import ElasticNetCV
import os
import numpy as np
y_list = []
X_list = []
for csv_f, lbl_f in train_files:
with open(lbl_f) as lbl_file:
for line in lbl_file:
age, Fage = line.rstrip().split(',')
y_list.append(Fage)
with open(csv_f) as csv_file:
X = np.genfromtxt(csv_file, dtype=float, delimiter=',')
X_list.append(X)
X_train = np.concatenate(X_list)
y_train = np.array(y_list, dtype=np.float)
regr = ElasticNetCV(cv=5, random_state=0, l1_ratio=[.1, .25, .5, .7, .9, .95, .99, 1])
regr.fit(X_train, y_train)
np.save(os.path.join(directory, "enet_cv_betas.npy"), regr.coef_)
np.save(os.path.join(directory, "enet_cv_intercept.npy"), regr.intercept_)
print(regr.alpha_)
print(regr.l1_ratio_)
print(regr.n_iter_)
print(regr.mse_path_)
input_folder = sys.argv[1]
save_folder = sys.argv[2]
train_files = get_all_training_files_in_folder(input_folder)
training(save_folder, train_files)