(train='train.csv', test='test.csv', submit='logistic_pred.csv')
| 78 | return mean_auc/N |
| 79 | |
| 80 | def main(train='train.csv', test='test.csv', submit='logistic_pred.csv'): |
| 81 | print "Reading dataset..." |
| 82 | train_data = pd.read_csv(train) |
| 83 | test_data = pd.read_csv(test) |
| 84 | all_data = np.vstack((train_data.ix[:,1:-1], test_data.ix[:,1:-1])) |
| 85 | |
| 86 | num_train = np.shape(train_data)[0] |
| 87 | |
| 88 | # Transform data |
| 89 | print "Transforming data..." |
| 90 | dp = group_data(all_data, degree=2) |
| 91 | dt = group_data(all_data, degree=3) |
| 92 | |
| 93 | y = array(train_data.ACTION) |
| 94 | X = all_data[:num_train] |
| 95 | X_2 = dp[:num_train] |
| 96 | X_3 = dt[:num_train] |
| 97 | |
| 98 | X_test = all_data[num_train:] |
| 99 | X_test_2 = dp[num_train:] |
| 100 | X_test_3 = dt[num_train:] |
| 101 | |
| 102 | X_train_all = np.hstack((X, X_2, X_3)) |
| 103 | X_test_all = np.hstack((X_test, X_test_2, X_test_3)) |
| 104 | num_features = X_train_all.shape[1] |
| 105 | |
| 106 | model = linear_model.LogisticRegression() |
| 107 | |
| 108 | # Xts holds one hot encodings for each individual feature in memory |
| 109 | # speeding up feature selection |
| 110 | Xts = [OneHotEncoder(X_train_all[:,[i]])[0] for i in range(num_features)] |
| 111 | |
| 112 | print "Performing greedy feature selection..." |
| 113 | score_hist = [] |
| 114 | N = 10 |
| 115 | good_features = set([]) |
| 116 | # Greedy feature selection loop |
| 117 | while len(score_hist) < 2 or score_hist[-1][0] > score_hist[-2][0]: |
| 118 | scores = [] |
| 119 | for f in range(len(Xts)): |
| 120 | if f not in good_features: |
| 121 | feats = list(good_features) + [f] |
| 122 | Xt = sparse.hstack([Xts[j] for j in feats]).tocsr() |
| 123 | score = cv_loop(Xt, y, model, N) |
| 124 | scores.append((score, f)) |
| 125 | print "Feature: %i Mean AUC: %f" % (f, score) |
| 126 | good_features.add(sorted(scores)[-1][1]) |
| 127 | score_hist.append(sorted(scores)[-1]) |
| 128 | print "Current features: %s" % sorted(list(good_features)) |
| 129 | |
| 130 | # Remove last added feature from good_features |
| 131 | good_features.remove(score_hist[-1][1]) |
| 132 | good_features = sorted(list(good_features)) |
| 133 | print "Selected features %s" % good_features |
| 134 | |
| 135 | print "Performing hyperparameter selection..." |
| 136 | # Hyperparameter selection loop |
| 137 | score_hist = [] |
no test coverage detected