-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_pipeline.py
308 lines (271 loc) · 10.6 KB
/
cmd_pipeline.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!usr/bin/env python
#author sidgan
from sklearn.kernel_approximation import RBFSampler
import sklearn.cluster
import optparse
import copper
from sklearn import linear_model
from sklearn.linear_model import SGDClassifier
from sklearn.decomposition import PCA
from sklearn import tree
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import GaussianNB
from sklearn import cross_validation
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn import preprocessing
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.grid_search import GridSearchCV
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.cross_validation import cross_val_score
import random
import csv as csv
import pandas as pd
import numpy as np
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
warnings.simplefilter('ignore', SyntaxWarning)
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
import sklearn
from sklearn.ensemble import AdaBoostClassifier
def cal_score(method, clf, features_test, target_test):
scores = cross_val_score(clf, features_test, target_test)
print method + " : %f " % scores.max()
#print scores.max()
def main():
type_of_problem = ""
split = 0.3
su_train = []
su_test = []
p = optparse.OptionParser()
#take path of training data set
p.add_option('--path_train', '-p', default='/home/user')
#take path of test data set
p.add_option('--path_test', '-s', default ='/home/user')
#what type of problem is it? regression/classification/clustering/dimensionality reduction
p.add_option('--type_of_problem', '-t', default = 'no_input')
#include cross validation true/false
p.add_option('--cross_validation', '-v', default ='True')
#take the numerical values
#p.add_option('--numerical_values', '-n')
#specify target column
p.add_option('--target', '-y')
options, arguments = p.parse_args()
#when user does not provide type of problem
if options.type_of_problem == 'no_input':
global type_of_problem
#ask the user explicitly for type of problem
print "Depending on type of problem, enter c for classification, clu for clustering, r for regression and d for dimensionality reduction."
type_of_problem = raw_input("")
#ask for value of cross validation if previously true
if options.cross_validation=='True':
global split
print "How much cross validation would you like? Enter a number between 0-1."
split = raw_input("")
split = float(split)
#Ask for numerical values
print "Does the data set require imputation? Enter 1 for affirmative."
imputation = raw_input()
imputation = int(imputation)
print "Enter labels for numerical values to be used for analysis, seperated by space"
num_values = raw_input()
num_values = num_values.split()
#load from files
train = pd.read_csv(options.path_train)
test = pd.read_csv(options.path_test)
#Any categories for conversion
#EXAMPLE: sex mapped to male and female
print "Enter labels for categorical values to be converted into numerical values. If none then enter none"
cat = raw_input()
cat = cat.split()
if cat == 'none':
# no categorical values
print ""
else:
print "Preparing data."
print "Converting categorical to numerical values."
for each in cat:
global su_train
# print each
i = train.get(each)
i = pd.get_dummies(i, prefix=each)
#print i
#print i.Sex_female.mean()
if each == cat[0]:
su_train = i
else:
su_train = pd.concat([su_train, i], axis=1)
#print su_train
#FOR TEST DATA
t = test.get(each)
t = pd.get_dummies(t, prefix=each)
if each == cat[0]:
su_test = i
else:
su_test = pd.concat([su_test, i], axis=1)
#print su_train
#load target values
target = train[options.target]
#TRAINING DATA SET
#final data frame with categorical and numerical values
data = pd.concat([train.get(num_values), su_train], axis=1)
#perform imputation if allowed - TRAINING DATA SET
if imputation == 1:
imp = data.dropna().mean()
data = data.fillna(imp)
#TEST DATA SET
#final data frame with categorical and numerical values
test = pd.concat([test.get(num_values), su_test], axis=1)
#perform imputation if allowed
if imputation == 1:
print "Performing imputation."
imp = test.dropna().mean()
test = test.fillna(imp)
#split the training data for cross validation
if options.cross_validation == 'True':
#perform cross validation if v==true
print "Splitting the training data with %f." % split
features_train, features_test, target_train, target_test = train_test_split(data, target, test_size=split, random_state=0)
else:
#no cross validation
features_train = data
features_test = test
target_train = target
target_test = 0
print "Generating Model"
#diffrentiate on the basis of type of problem
if type_of_problem == 'c':
prob = 1
#Naive Bayes
nb_estimator = GaussianNB()
nb_estimator.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("NAIVE BAYES CLASSIFICATION",nb_estimator, features_test, target_test)
#predictions = nb_estimator.predict(test)
#SVC Ensemble
#Ada boost
clf_ada = AdaBoostClassifier(n_estimators=100)
params = {
'learning_rate': [.05, .1,.2,.3,2,3, 5],
'max_features': [.25,.50,.75,1],
'max_depth': [3,4,5],
}
gs = GridSearchCV(clf_ada, params, cv=5, scoring ='accuracy', n_jobs=4)
clf_ada.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("ADABOOST",clf_ada, features_test, target_test)
#predictions = clf_ada.predict_proba(test)
#RANDOM FOREST CLASSIFIER
rf = RandomForestClassifier(n_estimators=100)
rf = rf.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("RANDOM FOREST CLASSIFIER",rf, features_test, target_test)
#predictions = rf.predict_proba(test)
#Gradient Boosting
gb = GradientBoostingClassifier(n_estimators=100, subsample=.8)
params = {
'learning_rate': [.05, .1,.2,.3,2,3, 5],
'max_features': [.25,.50,.75,1],
'max_depth': [3,4,5],
}
gs = GridSearchCV(gb, params, cv=5, scoring ='accuracy', n_jobs=4)
gs.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("GRADIENT BOOSTING",gs, features_test, target_test)
#sorted(gs.grid_scores_, key = lambda x: x.mean_validation_score)
#print gs.best_score_
#print gs.best_params_
#predictions = gs.predict_proba(test)
#KERNEL APPROXIMATIONS - RBF
rbf_feature = RBFSampler(gamma=1, random_state=1)
X_features = rbf_feature.fit_transform(data)
#SGD CLASSIFIER
clf = SGDClassifier(alpha=0.0001, class_weight=None, epsilon=0.1, eta0=0.0,
fit_intercept=True, l1_ratio=0.15, learning_rate='optimal',
loss='hinge', n_iter=5, n_jobs=1, penalty='l2', power_t=0.5,
random_state=None, shuffle=True, verbose=0,
warm_start=False)
clf.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("SGD Regression",clf, features_test, target_test)
#KN Classifier
neigh = KNeighborsClassifier(n_neighbors = 1)
neigh.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("KN CLASSIFICATION",neigh, features_test, target_test)
#predictions = neigh.predict_proba(test)
#Decision Tree classifier
clf_tree = tree.DecisionTreeClassifier(max_depth=10)
clf_tree.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("DECISION TREE CLASSIFIER",clf_tree, features_test, target_test)
#predictions = clf_tree.predict_proba(test)
if type_of_problem == 'r':
prob = 2
#LOGISTIC REGRESSION
logreg = LogisticRegression(C=3)
logreg.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("LOGISTIC REGRESSION",logreg, features_test, target_test)
#predictions = logreg.predict(test)
# SUPPORT VECTOR MACHINES
clf = svm.SVC(kernel = 'linear')
clf.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("LINEAR KERNEL",clf, features_test, target_test)
#print clf.kernel
#for sigmoid kernel
clf= svm.SVC(kernel='rbf', C=2).fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("SVM RBF KERNEL",clf, features_test, target_test)
#predictions = clf.predict(test)
#Lasso
clf = linear_model.Lasso(alpha=.1)
clf.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("LASSO",clf, features_test, target_test)
#elastic net
clf = linear_model.ElasticNet(alpha=.1, l1_ratio=.5, fit_intercept=True, normalize=False, precompute='auto',max_iter=1000, copy_X=True, tol =.0001, warm_start=False, positive=False)
clf.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("ELASTIC NET",clf, features_test, target_test)
#SGD REGRESSION
clf = SGDClassifier(alpha=0.0001, class_weight=None, epsilon=0.1, eta0=0.0,
fit_intercept=True, l1_ratio=0.15, learning_rate='optimal',
loss='hinge', n_iter=5, n_jobs=1, penalty='l2', power_t=0.5,
random_state=None, shuffle=True, verbose=0,
warm_start=False)
clf.fit(features_train, target_train)
if options.cross_validation == 'True':
cal_score("SGD Regression",clf, features_test, target_test)
if type_of_problem == 'clu':
prob = 3
#MINI BATCH K MEANS CLUSTERING
clf = sklearn.cluster.MiniBatchKMeans(init='k-means++', max_iter=100, batch_size=100, verbose=0, compute_labels=True, random_state=None, tol=0.0, max_no_improvement=10, init_size=None, n_init=3, reassignment_ratio=0.01)
clf.fit(features_train, target_train)
#MEAN SHIFT
clf = sklearn.cluster.MeanShift(bandwidth=None, seeds=[features_train, target_train], bin_seeding=False, min_bin_freq=1, cluster_all=True)
#clf.fit([features_train, target_train])
#clf.fit(data, target)
#if options.cross_validation == 'True':
# cal_score("MEAN SHIFT CLUSTERING",clf, features_test, target_test)
#K MEANS CLUSTERING
clf = sklearn.cluster.KMeans( init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances=True, verbose=0, random_state=None, copy_x=True, n_jobs=1)
clf.fit(data)
#if options.cross_validation == 'True':
# cal_score("K MEANS CLUSTERING",clf, features_test, target_test)
if type_of_problem == 'd':
prob = 4
#PCA
pca = PCA(n_components=1)
pca_train = pca.fit(data)
pca_test = pca.transform(test)
#perform classification
#main ends here
if __name__ == '__main__':
main()