Created
November 19, 2019 08:18
-
-
Save anirudhshenoy/931b1272dc4e745059dff79cb2852308 to your computer and use it in GitHub Desktop.
Stacking all the models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.neighbors import KNeighborsClassifier | |
| from sklearn.naive_bayes import MultinomialNB | |
| from xgboost import XGBClassifier | |
| from sklearn.svm import SVC | |
| # Define all models | |
| lr = SGDClassifier(loss = 'log', alpha = 0.1, penalty = 'elasticnet') | |
| svm = SVC(C = 10, kernel = 'poly', degree = 2, probability = True) | |
| nb = MultinomialNB(alpha = 10000, class_prior = [0.5, 0.5]) | |
| knn = KNeighborsClassifier(n_neighbors = 7, weights = 'distance', n_jobs = -1) | |
| rf = RandomForestClassifier(n_estimators = 250, min_samples_split = 5, max_depth = 15, n_jobs = -1) | |
| xgb = XGBClassifier(n_estimators = 100, learning_rate = 0.3, max_depth = 1, n_jobs = -1) | |
| model_dict = dict(zip(['LR', 'SVM', 'NB', 'KNN', 'RF', 'XGB'], [lr, svm, nb, knn, rf, xgb])) | |
| for model_name, model in model_dict.items(): | |
| print('Training {}'.format(model_name)) | |
| model.fit(train_features, y_train) | |
| model_weights = { 'LR' : 0.9, | |
| 'SVM' : 0.9, | |
| 'NB' : 0.8, | |
| 'KNN' : 0.75, | |
| 'RF' : 0.75, | |
| 'XGB' : 0.6, | |
| 'simple_nn' : 0.7 | |
| } | |
| y_pred_prob = 0 | |
| for model_name, model in model_dict.items(): | |
| y_pred_prob += (model.predict_proba(test_features)[:,1] * model_weights[model_name]) | |
| y_pred_prob += (simple_nn.predict(test_features.todense()).ravel() * model_weights['simple_nn']) | |
| y_pred_prob /= sum(model_weights.values()) | |
| print_model_metrics(y_test, y_pred_prob) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment