-
Notifications
You must be signed in to change notification settings - Fork 0
/
hack.py
224 lines (140 loc) · 5.35 KB
/
hack.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
import pandas as pd
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
train=pd.read_csv('fr/train/invoice_train.csv')
train_inv=pd.read_csv('fr/train/client_train.csv')
test_inv=pd.read_csv('fr/test/client_test.csv')
test=pd.read_csv('fr/test/invoice_test.csv')
X_test=test.drop(['invoice_date'],axis=1)
X_test=X_test.drop(['client_id'],axis=1)
def counter_type(x):
if x=="ELEC":
return 1
else:
return 0
############################################################
training_set= pd.concat([train_inv, train], axis=1, join='inner')
testing_set= pd.concat([test_inv, test], axis=1, join='inner')
############################################################
training_set['counter_type']=training_set['counter_type'].apply(lambda x:counter_type(x))
testing_set['counter_type']=testing_set['counter_type'].apply(lambda x:counter_type(x))
####### DROPING
#test_set=test_set.drop(['client_id','invoice_date','old_index',
# 'new_index','creation_date',
# 'months_number'],axis=1)
Y = training_set["target"].values
training_set=training_set.drop(['client_id','invoice_date',
'creation_date',
'target'],axis=1)
testing_set=testing_set.drop(['client_id','invoice_date','creation_date'
],axis=1)
#y=train_inv.iloc[:,5:]
#train['invoice_date'] = pd.to_datetime(train['invoice_date'])
#
#
#import time
#import datetime
# = "01/12/2011"
#time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
#s=train['invoice_date'].values
trainii=training_set.values
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=630, max_depth=30,
random_state=100)
clf.fit(training_set, Y)
### helper code
for i in training_set.columns:
print(i,training_set[i].unique())
print("%%%%%%%%")
#
#y_pred=clf.predict(testing_set)
probs = clf.predict_proba(testing_set)
# keep probabilities for the positive outcome only
probs = probs[:, 1]
#probs
#########
# create submission DataFrame
submission = pd.DataFrame({'client_id':test_inv['client_id'],'target':probs})
#Visualize the first 5 rows
submission.head()
filename = '8-th-BERRIMI_Mohamed-Sub.csv'
submission.to_csv(filename,index=False)
print('Saved file: ' + filename)
############ Feature scaling
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
from sklearn import preprocessing
normalized_X = preprocessing.normalize(training_set)
normalized_X_test = preprocessing.normalize(testing_set)
##### Fit scaled data to RF classifier
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=30, max_depth=8,
random_state=0)
clf.fit(normalized_X, Y)
probs = clf.predict_proba(testing_set)
# keep probabilities for the positive outcome only
probs = probs[:, 1]
# create submission DataFrame
submission = pd.DataFrame({'client_id':test_inv['client_id'],'target':probs})
#Visualize the first 5 rows
submission.head()
filename = '2nd-BERRIMI_Mohamed-Sub.csv'
submission.to_csv(filename,index=False)
print('Saved file: ' + filename)
############ XGBOOST
from xgboost import XGBClassifier
model = XGBClassifier()
model.fit(normalized_X, Y)
probs = clf.predict_proba(testing_set)
# keep probabilities for the positive outcome only
probs = probs[:, 1]
# create submission DataFrame
submission = pd.DataFrame({'client_id':test_inv['client_id'],'target':probs})
#Visualize the first 5 rows
submission.head()
filename = '3rd-BERRIMI_Mohamed-Sub.csv'
submission.to_csv(filename,index=False)
print('Saved file: ' + filename)
##########"
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
Train_set = sc.fit_transform(training_set)
Test_SET = sc.transform(testing_set)
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=300, max_depth=2,
random_state=0)
clf.fit(X_train, Y)
probs = clf.predict_proba(X_test)
# keep probabilities for the positive outcome only
probs = probs[:, 1]
# create submission DataFrame
submission = pd.DataFrame({'client_id':test_inv['client_id'],'target':probs})
#Visualize the first 5 rows
submission.head()
filename = '314-BERRIMI_Mohamed-Sub.csv'
submission.to_csv(filename,index=False)
print('Saved file: ' + filename)
######## Deep learning
import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Activation, BatchNormalization, regularizers
from keras.layers.noise import GaussianNoise
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.utils.np_utils import to_categorical
y_trainHot = to_categorical(Y, num_classes = 2)
probs = classifier.predict_proba(testing_set)
# keep probabilities for the positive outcome only
probs = probs[:, 1]
# create submission DataFrame
submission = pd.DataFrame({'client_id':test_inv['client_id'],'target':probs})
#Visualize the first 5 rows
submission.head()
filename = '6th-BERRIMI_Mohamed-Sub.csv'
submission.to_csv(filename,index=False)
print('Saved file: ' + filename)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(training_set)
X_test = sc.transform(testing_set)