-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxgboost-(reduced)ipynb
214 lines (214 loc) · 5.43 KB
/
xgboost-(reduced)ipynb
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"import numpy as np \n",
"import pandas as pd \n",
"import seaborn as sb\n",
"import xgboost as xgb\n",
"\n",
"\n",
"from subprocess import check_output\n",
"#print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#explore the data\n",
"\n",
"\n",
"train = pd.read_csv(\"/home/amal/Téléchargements/train_50.csv\")\n",
"#print(train.head())\n",
"\n",
"test = pd.read_csv(\"/home/amal/Téléchargements/test_50.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:6: FutureWarning: order is deprecated, use sort_values(...)\n"
]
},
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7f045f65f090>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#sum features\n",
"featuresSum = train.sum()\n",
"#print(featuresSum)\n",
"#print(type(train))\n",
"#plot the sum of features in desceding order\n",
"featuresSum.drop(['target', 'id']).order().plot(kind='barh', figsize=(8,16))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7f045f65f090>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Clases are String like \"Class_1\" => we map them as numbers from 0-8 (9 classes)\n",
"\n",
"class_range = range(1, 10)\n",
"class_dict = {}\n",
"\n",
"for n in class_range:\n",
" class_dict['Class_{}'.format(n)] = n-1\n",
"\n",
"#print(class_dict)\n",
"#print(train.head())\n",
"train['target'] = train['target'].map(class_dict)\n",
"#print(train['target'].head())\n",
"\n",
"#next we plot the count for each class unsing seaborn libary\n",
"sb.countplot(x='target', data= train)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#next we create training and testing sets\n",
"X_train = train.drop([\"id\", \"target\"], axis=1)\n",
"Y_train = train[\"target\"].copy()\n",
"X_test = test.drop(\"id\", axis = 1).copy()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 3.09625349e-04 5.26831388e-01 3.93590331e-01 ..., 1.01200612e-02\n",
" 1.39244401e-03 1.31381341e-04]\n",
" [ 8.51289893e-04 9.27847717e-03 2.20594043e-03 ..., 1.58328272e-03\n",
" 1.89369693e-01 9.39092482e-04]\n",
" [ 8.12630897e-05 2.97565421e-04 1.23004473e-04 ..., 1.50788022e-04\n",
" 3.60993843e-04 8.53675738e-05]\n",
" ..., \n",
" [ 3.90368339e-04 5.03351092e-01 2.47188538e-01 ..., 6.10784674e-03\n",
" 2.07993013e-04 2.16426561e-04]\n",
" [ 1.20221834e-04 2.48132199e-01 8.16784650e-02 ..., 3.10692866e-03\n",
" 1.03520440e-04 1.29586289e-04]\n",
" [ 3.58198141e-03 3.11427623e-01 4.54801291e-01 ..., 1.50145888e-01\n",
" 1.47303555e-03 6.85389154e-04]]\n"
]
}
],
"source": [
"# we use the training and test sets to make a prediction based on Boosted Trees => library XgBoost\n",
"# http://xgboost.readthedocs.io/en/latest/model.html\n",
"\n",
"params = {\"objective\": \"multi:softprob\", \"eval_metric\":\"mlogloss\", \"num_class\": 9}\n",
"\n",
"T_train_xgb = xgb.DMatrix(X_train, Y_train)\n",
"X_test_xgb = xgb.DMatrix(X_test)\n",
"\n",
"#number of iterations - initial 20, \n",
"gbm = xgb.train(params, T_train_xgb, 50)\n",
"Y_pred = gbm.predict(X_test_xgb)\n",
"print(Y_pred)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Make a submission based on model \"sample_submission.csv\"\n",
"\n",
"submission = pd.DataFrame({ \"id\": test[\"id\"]})\n",
"\n",
"i = 0\n",
"\n",
"for num in class_range:\n",
" col_name = str(\"Class_{}\".format(num))\n",
" submission[col_name] = Y_pred[:,i]\n",
" i = i + 1\n",
" \n",
"submission.to_csv('/home/amal/Bureau/xg_reduced.csv', index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}