forked from SmithCollegeHCV/MentalHealth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random Forest.py
144 lines (80 loc) · 2.91 KB
/
Random Forest.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
# coding: utf-8
# In[ ]:
import sklearn
from sklearn.ensemble import RandomForestClassifier
import pandas
import numpy
# In[ ]:
#ask for user input for imported pivot file from Isha's R program
pivotfile = input('Name of file you want to import: ') #input must be in ' '
# In[ ]:
#read in pivotfile in pandas dataframe format
rawdata = pandas.read_csv(pivotfile)
# In[4]:
#put codes as Y in numpy array format
Y = numpy.array(rawdata[[0]]) #must be in numpy array format
# In[5]:
ctgHeader = list(rawdata.columns.values)[3:] #create a list of all the ctgs
#print(ctgHeader)
# In[6]:
#select the df of all the binary numbers from the raw data in bool format
X = numpy.array(rawdata[ctgHeader], dtype = bool)
# In[7]:
number_of_forests = 40
# In[ ]:
date = input('Enter date: ')
# In[8]:
file_name = str(date) + " Random Forest Data Variable Importance " + str(number_of_forests) + ".txt"
file = open(file_name, "w")
print(file_name)
# In[15]:
#types of models
bagged_model = RandomForestClassifier(n_estimators = 207,
max_features = "auto")
# Loop over 5, 10, 15, 20, 25, 30
# Open new file for writing
for nth_model in range(1, number_of_forests + 1):
rf_sqrt_model = RandomForestClassifier(n_estimators = nth_model,
max_features = "sqrt")
rf_sqrt_model = rf_sqrt_model.fit(X, rawdata['Code'])
df = pandas.DataFrame(rf_sqrt_model.feature_importances_, ctgHeader)
df = df.sort_values(by = 0, ascending = False)
#df_top10 = df.sort_values(by = 0, ascending = False).iloc[:10, 0]
file.write(str(df) + "\n")
print(df)
# Close file
#print("#trees=", nth_model)
#print(df_top10)
#print(df)
#print()
#rf_sqrt_model_500 = RandomForestClassifier(n_estimators = 500,
# max_features = "sqrt")
#rf_sqrt_model_1000 = RandomForestClassifier(n_estimators = 1000,
# max_features = "sqrt")
# In[17]:
#fitting models
bagged_model = bagged_model.fit(X, rawdata['Code'])
#rf_sqrt_model_500 = rf_sqrt_model_500.fit(X, rawdata['Code'])
#rf_sqrt_model_1000 = rf_sqrt_model_1000.fit(X, rawdata['Code'])
# In[112]:
#making of generic data frame
df = pandas.DataFrame(rf_sqrt_model.feature_importances_, ctgHeader)
#data frame with values sorted in ascending order
df = df.sort_values(by = 0, ascending = False)
# In[113]:
df[2] = rf_sqrt_model_500.feature_importances_
print(df)
# In[114]:
df.sort_values(by = 2, ascending = False).iloc[:10,1]
# In[ ]:
df.sort_values(by = 0, ascending = False).iloc[:10,0]
# In[98]:
#i_tree = 0
#for tree_in_forest in clf.estimators_:
#dot_data = StringIO()
# tree.export_graphviz(tree_in_forest, out_file = dot_data)
# graph = pydot.graph_from_dot_data(dot_data.getvalue())
# f_name = 'tree_' + str(i_tree) + '.svg'
# Image(graph.write_svg(f_name))
# i_tree += 1
# In[ ]: