/
ROCKS_submission.py
228 lines (152 loc) · 5.14 KB
/
ROCKS_submission.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# coding: utf-8
# In[41]:
import pandas as pd
import numpy as np
from sklearn import metrics, feature_selection
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.plotly as py
import pytest
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
# In[2]:
df=pd.read_csv('2010 Federal STEM Education Inventory Data Set.csv',header=1,low_memory=False)
#Removing last row of total
df=df[:-1]
# In[3]:
columns = pd.DataFrame(df.columns.tolist())
columns.loc[columns[0].str.startswith('Unnamed:'), 0] = np.nan
columns[0] = columns[0].fillna(method='ffill')
df.columns=columns
df.columns=pd.MultiIndex.from_tuples(df.columns)
# # Step1
# In[4]:
df_req=df[[' C1) Funding FY2008 ',' C2) Funding FY2009 ']]
df_req=df_req.rename(index=str,columns={' C1) Funding FY2008 ':'FY2008',' C2) Funding FY2009 ':'FY2009'})
df_req=df_req.replace(' - ',np.nan)
#df_req
# In[5]:
df_req['FY2008']=df_req['FY2008'].astype(float)
df_req['FY2009']=df_req['FY2009'].astype(float)
df_req=df_req.dropna(axis=0)
df=df.iloc[df_req.index]
# In[6]:
# In[7]:
#Part 1
df_req['% growth']=((df_req['FY2009']-df_req['FY2008'])/df_req['FY2008'])*100
#Part 2
df_req['label']=np.where(df_req['% growth']>=0,1,0)
# In[8]:
# # Step 2
# In[9]:
#Part 1
funding_col=[' C1) Funding FY2008 ',' C2) Funding FY2009 ', ' C3) Funding FY2010 ','Index Number']
non_funding_col=list(set(df.columns)-set(funding_col))
for i in non_funding_col:
#Percentage composition of each attribute value in a column
dff=pd.DataFrame(df[i]).apply(pd.Series.value_counts)/len(df)
#dff.plot(kind='bar')
#plt.show()
#plt.close()
# In[10]:
#Part 2
dff=pd.DataFrame()
tye=type(pd.DataFrame())
for i in non_funding_col:
if type(df[i])==type(pd.DataFrame()):
num_cols=len(df[i].columns)
for j in range(num_cols):
e=pd.get_dummies(df[i].iloc[:,j])
if e.empty:
continue
e=e.iloc[:,0]
dff[str(i)+'_'+str(e.name)]=e
score=metrics.mutual_info_score(e, df_req['label'])
print(i," ",e.name,' ',score)
print()
else:
e=pd.get_dummies(df[i]).iloc[:,0]
dff[str(i)+'_'+str(e.name)]=e
score=metrics.mutual_info_score(e, df_req['label'])
print(i,' ',score)
print()
# # Step 3
# In[11]:
#Part 1
def get_test_size(szes):
return int(0.3*szes)
tst_size=get_test_size(szes=len(dff))
train, test ,label_train, label_test= train_test_split(dff,df_req.iloc[:,3], test_size=tst_size)
print(len(train))
# In[12]:
c=[]
for f in train.columns:
if(any(x in f for x in set(('[', ']', '<')))):
f=f.replace('<','')
print(f)
c.append(f)
train.columns=c
test.columns=c
# In[14]:
#Part 2
model = XGBClassifier()
model.fit(train, label_train)
# In[15]:
label_pred=model.predict(test)
# In[16]:
# In[17]:
def get_thresh(model,train,test,label_test,label_train):
if (len(test)>len(train)) or (len(label_test)>len(label_train)):
raise TypeError('Invalid train and test size')
model1 = XGBClassifier()
if type(model)!=type(XGBClassifier()):
raise TypeError('Invalid model passed')
if (pd.DataFrame(label_train).shape[1]>1) or (pd.DataFrame(label_test).shape[1]>1):
raise TypeError('Multiple columns in label, Invalid shape.')
max_score=0
thrsh=0
thresholds = np.sort(model.feature_importances_)
for thresh in thresholds:
selection = feature_selection.SelectFromModel(model, threshold=thresh,prefit=True)
select_X_train = selection.transform(train)
selection_model = XGBClassifier()
selection_model.fit(select_X_train, label_train)
select_X_test = selection.transform(test)
y_pred = selection_model.predict(select_X_test)
scr=metrics.roc_auc_score(label_test,y_pred)
if(scr>max_score):
max_score=scr
thrsh=thresh
return thrsh
# In[18]:
thrsh=get_thresh(model,train,test,label_test,label_train)
# In[19]:
selection = feature_selection.SelectFromModel(model, threshold=thrsh,prefit=True)
select_X_train = selection.transform(train)
selection_model = XGBClassifier()
selection_model.fit(select_X_train, label_train)
select_X_test = selection.transform(test)
y_pred = selection_model.predict(select_X_test)
print(metrics.roc_auc_score(label_test,y_pred))
# In[20]:
feature_idx = selection.get_support()
feature_name = dff.columns[feature_idx]
print('Number of features selected: ',select_X_train.shape[1])
# In[21]:
#The selected features
print(feature_name)
# In[45]:
#Part 3
def test_get_test_size():
assert get_test_size(200)==60
def test_train_test_get_thresh():
train, test ,label_train, label_test= train_test_split(dff,df_req.iloc[:,3], test_size=0.7)
assert get_thresh(model,train,test,label_test,label_train)
def test_model_get_thresh():
train, test ,label_train, label_test= train_test_split(dff,df_req.iloc[:,3], test_size=0.3)
assert get_thresh(0,train,test,label_test,label_train)
def test_label_get_thresh():
train, test ,label_train, label_test= train_test_split(dff,df_req.iloc[:,3], test_size=0.3)
assert get_thresh(model,train,test,test[:3],label_train)
# In[35]:
# In[ ]: