-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathvectorize_and_predict_inplace.py
186 lines (137 loc) · 4.19 KB
/
vectorize_and_predict_inplace.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
"vectorize categorical variables on the real train and test set, train an SVM, output predictions"
"inplace refers to encoding categorical variables as -1/0/1"
import numpy as np
import pandas as pd
import sqlite3
from math import sqrt
from sklearn.feature_extraction import DictVectorizer as DV
from sklearn.svm import SVC
###
# dirs need trailing slashes
data_dir = '/path/to/your/data/dir/'
predictions_dir = '/path/to/your/predictions/dir/'
train_file = data_dir + 'train.csv'
test_file = data_dir + 'test.csv'
output_file = predictions_dir + 'p_svm.csv'
###
train = pd.read_csv( train_file )
test = pd.read_csv( test_file )
# fix missing YOB
train.YOB[ train.YOB.isnull() ] = 0
train.YOB[train.YOB < 1920] = 0
train.YOB[train.YOB > 2004] = 0
test.YOB[ test.YOB.isnull() ] = 0
test.YOB[test.YOB < 1920] = 0
test.YOB[test.YOB > 2004] = 0
# numeric x
numeric_cols = [ 'YOB', 'votes' ]
x_num_train = train[ numeric_cols ].as_matrix()
x_num_test = test[ numeric_cols ].as_matrix()
# scale to <0,1>
max_train = np.amax( x_num_train, 0 )
max_test = np.amax( x_num_test, 0 )
print "max. numerical train/test", max_train, max_test
x_num_train = x_num_train / max_train
x_num_test = x_num_test / max_train
# y
y_train = train.Happy
ids = test.UserID
# categorical
cat_train = train.drop( numeric_cols + [ 'UserID', 'Happy' ], axis = 1 )
cat_test = test.drop( numeric_cols + [ 'UserID' ], axis = 1 )
cat_train.fillna( 0, inplace = True )
cat_test.fillna( 0, inplace = True )
# Gender to -1,0,1
"""
cat_train.Gender[ cat_train.Gender == 'Male' ] = 1
cat_train.Gender[ cat_train.Gender == 'Female' ] = -1
cat_test.Gender[ cat_test.Gender == 'Male' ] = 1
cat_test.Gender[ cat_test.Gender == 'Female' ] = -1
"""
# WARNING: hacks ahead
yeses = [ 'Public', 'Science', 'Try first', 'Giving', 'Idealist', 'Cool headed', 'Odd hours', 'Happy', 'P.M.', 'Start', 'Circumstances',
'TMI', 'Talk', 'Technology', 'Demanding', 'PC', 'Cautious', 'Yes!', 'Space', 'In-person', 'Yay people!', 'Own', 'Optimist', 'Mom', 'Nope' ]
"""
# check if we got questions covered
# possible exercise in refactoring
for col in cat_train.columns:
if not col.startswith( 'Q' ):
continue
uniq = train[col].unique()
found = False
for y in [ 'Yes' ] + yeses:
if y in uniq:
found = True
break
if found:
continue
print col, uniq
"""
# training set
# insert ones
for col in cat_train.columns:
if not col.startswith( 'Q' ):
continue
for y in yeses + [ 'Yes' ]:
i = cat_train[col] == y
cat_train[col][i] = 1
# insert -1
for col in cat_train.columns:
if not col.startswith( 'Q' ):
continue
not_y = ~ cat_train[col].isin(( 0, 1 ))
cat_train[col][not_y] = -1
"""
# inspect
for col in cat_train.columns:
if not col.startswith( 'Q' ):
continue
print cat_train[col].unique()
"""
# testing set
# insert ones
for col in cat_test.columns:
if not col.startswith( 'Q' ):
continue
for y in yeses + [ 'Yes' ]:
i = cat_test[col] == y
cat_test[col][i] = 1
# insert -1
for col in cat_test.columns:
if not col.startswith( 'Q' ):
continue
not_y = ~ cat_test[col].isin(( 0, 1 ))
cat_test[col][not_y] = -1
"""
# inspect
for col in cat_test.columns:
if not col.startswith( 'Q' ):
continue
print cat_test[col].unique()
"""
x_cat_train = cat_train.T.to_dict().values()
x_cat_test = cat_test.T.to_dict().values()
# vectorize
vectorizer = DV( sparse = False )
vec_x_cat_train = vectorizer.fit_transform( x_cat_train )
vec_x_cat_test = vectorizer.transform( x_cat_test )
# complete x
x_train = np.hstack(( x_num_train, vec_x_cat_train ))
x_test = np.hstack(( x_num_test, vec_x_cat_test ))
x_train = np.dot( x_train, a )
x_test = np.dot( x_test, a )
if __name__ == "__main__":
print "training..."
C = 0.19
gamma = 0.0028
shrinking = True
#auto_class_weights = False
probability = True
verbose = True
svc = SVC( C = C, gamma = gamma, shrinking = shrinking, probability = probability, verbose = verbose )
svc.fit( x_train, y_train )
p = svc.predict_proba( x_test )
p = p[:,1]
# make sure both y and p are of shape (n,1) and not (n,)
ids_and_p = np.hstack(( ids.reshape(( -1, 1 )), p.reshape(( -1, 1 ))))
np.savetxt( output_file, ids_and_p, fmt = [ '%d', '%.10f' ], delimiter = ',', header = 'UserID,Probability1', comments = '' )