-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrf.py
148 lines (127 loc) · 5.23 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"Scott's script for training a random forest docs"
import pandas as pd
import numpy as np
import sklearn
import argparse
import tools
from copy import deepcopy
from tools import *
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, cross_val_score, KFold
# Performs a stepwise search for the optimal
# number of features in a trimmed random forest
def tune(model, min_features=1, max_features=200):
out = pd.DataFrame(data=np.empty([max_features - min_features + 1, 2]),
columns=['n_features', 'acc'])
out['n_features'] = range(min_features, max_features + 1)
for i in range(min_features, max_features + 1):
all_ftrs = full_model.feature_importances_
sort_ftrs = np.argsort(all_ftrs)[-i:]
clf = RandomForestClassifier(n_estimators=n_estimators)
clf = clf.fit(x_train[:, sort_ftrs], y_train)
acc = clf.score(x_test[:, sort_ftrs], y_test)
print "Number of features: %s" %i
print "Model accuracy: %s \n" %acc
out['acc'][i - min_features] = acc
return out
#returns the top n features by importance in the TextRF
def print_top_features(model, n=10):
print "\nThe top %i features are..." %n
for x in xrange(n):
print model.top_features[n]
return
# Main class for the text-based random forest;
# has methods for loading and processing data,
# as well as model-specific attributes like accuracy and feature names
class TextRF:
def __init__(self, trees=1000):
#setting attributes for the RF
self.__name__ = 'rf'
self.feature_names = []
self.trees = trees
self.pruned = False
# Main function for training and testing the random forest
def fit(self, x, y, top=100, jobs=-1, verbose=True, prune=True):
#training the RF on the docs
if verbose:
print "Training the random forest..."
rf = RandomForestClassifier(n_estimators=self.trees,
class_weight='balanced_subsample', n_jobs=jobs)
mod = rf.fit(x, y)
importances = mod.feature_importances_
if prune:
# Trimming the tree to the top features
sorted_indices = np.argsort(importances)
trimmed_indices = np.array(sorted_indices[-top:])
self.feature_indices = trimmed_indices
# Pruning the unnecessary features from the training data
X = deepcopy(x[:, trimmed_indices])
# Training a new forest on the pruned data
mod = RandomForestClassifier(n_estimators=self.trees,
class_weight='balanced_subsample',
n_jobs=jobs)
mod.fit(X, y)
# Passing attributes up to the instance
self.feature_importances = importances
self.pruned = True
# Setting the model attribute for the instance
self.mod = mod
# Wrappers for the sklearn functions; admittedly redundant
def score(self, x, y):
if self.pruned:
X = deepcopy(x[:, self.feature_indices])
else:
X = deepcopy(x)
return self.mod.score(X, y)
def predict(self, x):
if self.pruned:
X = deepcopy(x[:, self.feature_indices])
else:
X = deepcopy(x)
return self.mod.predict(X)
def predict_proba(self, x):
if self.pruned:
X = deepcopy(x[:, self.feature_indices])
else:
X = deepcopy(x)
return self.mod.predict_proba(X)
# Running an example of the model
if __name__ == '__main__':
parser = argparse.ArgumentParser()
#positional arguments
parser.add_argument('data',
help='data path for the corpus (in CSV format)')
parser.add_argument('x_name',
help='name of the column holding the text')
parser.add_argument('y_name',
help='name of the column holding the target variable')
# Optional arguments
parser.add_argument('-lm', '--limit_features', default='yes',
help='limit the number of features? (yes or no)')
parser.add_argument('-ft', '--features', type=int, default=10000,
help='number of features for the SVM, if limited')
parser.add_argument('-vc', '--vec_meth', default='tfidf',
help='method for vectorizing the text; count or tfidf')
parser.add_argument('-tr', '--n_trees', type=int, default=1000,
help='number of trees to use in the RF')
parser.add_argument('-ng', '--ngrams', type=int, default=2,
help='max ngram size')
parser.add_argument('-sm', '--split_method', default='train-test',
help='split the data by year, train-test, or cross-val')
parser.add_argument('-sv', '--split_variable',
help='variable to used for splitting the data')
parser.add_argument('-tv', '--test_val',
help='which split_variable to use for the test data')
args = parser.parse_args()
# Loading the data and training the RF
corpus = pd.read_csv(args.data)
data = TextData()
data.process(corpus, args.x_name, args.y_name, args.ngrams,
args.features, args.vec_meth, args.limit_features)
data.split(args.split_method, args.split_variable, args.test_val)
# Fitting the model and getting the statz
mod = TextRF(trees=args.n_trees)
mod.fit(data.X_train, data.y_train)
print "\nModel accuracy is %0.4f" %mod.score(data.X_test, data.y_test)