MCPcopy Index your code
hub / github.com/ujjwalkarn/DataSciencePython / main

Function main

Logistic-Regression/classifier_corrected.py:50–103  ·  view source on GitHub ↗

Fit models and make predictions. We'll use one-hot encoding to transform our categorical features into binary features. y and X will be numpy array objects.

()

Source from the content-addressed store, hash-verified

48
49
50def main():
51 """
52 Fit models and make predictions.
53 We'll use one-hot encoding to transform our categorical features
54 into binary features.
55 y and X will be numpy array objects.
56 """
57 model = linear_model.LogisticRegression(C=3) # the classifier we'll use
58
59 # === load data in memory === #
60 print "loading data"
61 y, X = load_data('train.csv')
62 y_test, X_test = load_data('test.csv', use_labels=False)
63
64 # === one-hot encoding === #
65 # we want to encode the category IDs encountered both in
66 # the training and the test set, so we fit the encoder on both
67 encoder = preprocessing.OneHotEncoder()
68 encoder.fit(np.vstack((X, X_test)))
69 X = encoder.transform(X) # Returns a sparse matrix (see numpy.sparse)
70 X_test = encoder.transform(X_test)
71
72 # if you want to create new features, you'll need to compute them
73 # before the encoding, and append them to your dataset after
74
75 # === training & metrics === #
76 mean_auc = 0.0
77 n = 10 # repeat the CV procedure 10 times to get more precise results
78 for i in range(n):
79 # for each iteration, randomly hold out 20% of the data as CV set
80 X_train, X_cv, y_train, y_cv = cross_validation.train_test_split(
81 X, y, test_size=.20, random_state=i*SEED)
82
83 # if you want to perform feature selection / hyperparameter
84 # optimization, this is where you want to do it
85
86 # train model and make predictions
87 model.fit(X_train, y_train)
88 preds = model.predict_proba(X_cv)[:, 1]
89
90 # compute AUC metric for this CV fold
91 fpr, tpr, thresholds = metrics.roc_curve(y_cv, preds)
92 roc_auc = metrics.auc(fpr, tpr)
93 print "AUC (fold %d/%d): %f" % (i + 1, n, roc_auc)
94 mean_auc += roc_auc
95
96 print "Mean AUC: %f" % (mean_auc/n)
97
98 # === Predictions === #
99 # When making predictions, retrain the model on the whole training set
100 model.fit(X, y)
101 preds = model.predict_proba(X_test)[:, 1]
102 filename = raw_input("Enter name for submission file: ")
103 save_results(preds, filename + ".csv")
104
105if __name__ == '__main__':
106 main()

Callers 1

Calls 2

load_dataFunction · 0.85
save_resultsFunction · 0.85

Tested by

no test coverage detected