forked from andresantonioriveros/pyRF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regular_train.py
67 lines (50 loc) · 2.13 KB
/
regular_train.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
67
# coding=utf-8
# Entrena un arbol de decisión clasico y guarda sus resultados
# -------------------------------------------------------------------------------------------------
import argparse
import pickle
import sys
import pandas as pd
from sklearn import cross_validation
import tree
import utils
if __name__ == '__main__':
# Recibo parámetros de la linea de comandos
print ' '.join(sys.argv)
parser = argparse.ArgumentParser()
parser.add_argument('--percentage', required=True, type=str)
parser.add_argument('--folds', required=True, type=int)
parser.add_argument('--training_set_path', required=True, type=str)
parser.add_argument('--result_path', required=True, type=str)
parser.add_argument('--class_filter', nargs='*', type=str)
parser.add_argument('--feature_filter', nargs='*', type=str)
args = parser.parse_args(sys.argv[1:])
percentage = args.percentage
folds = args.folds
training_set_path = args.training_set_path
result_path = args.result_path
class_filter = args.class_filter
feature_filter = args.feature_filter
data = pd.read_csv(training_set_path, index_col=0)
data, y = utils.filter_data(data, index_filter=None, class_filter=class_filter,
feature_filter=feature_filter)
skf = cross_validation.StratifiedKFold(y, n_folds=folds)
results = []
ids = []
for train_index, test_index in skf:
train_X, test_X = data.iloc[train_index], data.iloc[test_index]
train_y, test_y = y.iloc[train_index], y.iloc[test_index]
clf = None
clf = tree.Tree('gain', max_depth=10, min_samples_split=20)
clf.fit(train_X, train_y)
results.append(clf.predict_table(test_X, test_y))
ids.extend(test_X.index.tolist())
result = pd.concat(results)
result['indice'] = ids
result.set_index('indice')
result.index.name = None
result = result.drop('indice', axis=1)
output = open(result_path + '/Arboles/Arbol_' + percentage + '.pkl', 'wb+')
pickle.dump(clf, output)
output.close()
result.to_csv(result_path + '/Predicciones/result_' + percentage + '.csv')