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
| import datetime | |
| from flask_sqlalchemy import SQLAlchemy | |
| from sqlalchemy import Column, DateTime, BigInteger, String, Text | |
| db_crawler = SQLAlchemy() | |
| class Company(db_crawler.Model): | |
| __tablename__ = 'company_lists' | |
| id = Column(BigInteger, primary_key=True) |
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
| lgbm_params = { | |
| 'nthread': 4, | |
| 'n_estimators': 10000, | |
| 'learning_rate': .02, | |
| 'num_leaves': 34, | |
| 'colsample_bytree': .9497036, | |
| 'subsample': .8715623, | |
| 'max_depth': 8, | |
| 'reg_alpha': .041545473, | |
| 'reg_lambda': .0735294, |
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
| def display_folds_importances(feature_importance_df_, n_folds = 5): | |
| n_columns = 3 | |
| n_rows = (n_folds + 1) // n_columns | |
| _, axes = plt.subplots(n_rows, n_columns, figsize=(8 * n_columns, 8 * n_rows)) | |
| for i in range(n_folds): | |
| sns.barplot(x = i, y = 'index', data = feature_importance_df_.reset_index().sort_values(i, ascending = False).head(20), | |
| ax = axes[i // n_columns, i % n_columns]) | |
| sns.barplot(x = 'mean', y = 'index', data = feature_importance_df_.reset_index().sort_values('mean', ascending = False).head(20), | |
| ax = axes[n_rows - 1, n_columns - 1]) | |
| plt.title('LightGBM Features (avg over folds)') |
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
| def cv_scores(df, num_folds, params, stratified = False, verbose = -1, | |
| save_train_prediction = False, train_prediction_file_name = 'train_prediction.csv', | |
| save_test_prediction = True, test_prediction_file_name = 'test_prediction.csv'): | |
| warnings.simplefilter('ignore') | |
| clf = LGBMClassifier(**params) | |
| # Divide in training/validation and test data | |
| train_df = df[df['TARGET'].notnull()] | |
| test_df = df[df['TARGET'].isnull()] |
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
| # Function to calculate missing values by column# Funct // credits Will Koehrsen | |
| def missing_values_table(df): | |
| # Total missing values | |
| mis_val = df.isnull().sum() | |
| # Percentage of missing values | |
| mis_val_percent = 100 * df.isnull().sum() / len(df) | |
| # Make a table with the results | |
| mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1) |
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
| import os | |
| file_path = 'Data/' | |
| print(os.listdir(file_path)) | |
| app_train = pd.read_csv(file_path + 'application_train.csv') | |
| app_test = pd.read_csv(file_path + 'application_test.csv') |
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
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import gc | |
| import time | |
| import warnings | |
| warnings.simplefilter(action = 'ignore', category = FutureWarning) | |
| from sklearn.metrics import roc_auc_score, precision_score, recall_score | |
| from sklearn.model_selection import KFold, StratifiedKFold |
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
| vclf = VotingClassifier(estimators=[('lr', calib_clf_LR), ('svc', calib_clf_SVM), ('rf', calib_clf_RF)], voting='soft', n_jobs = -1) | |
| vclf.fit(Train_X, Train_Y) | |
| print("Log loss (train) on the VotingClassifier :"+str(np.round(log_loss(Train_Y, vclf.predict_proba(Train_X), labels=vclf.classes_), 2))) | |
| print("Log loss (CV) on the VotingClassifier :"+str(np.round(log_loss(CV_Y, vclf.predict_proba(CV_X), labels=vclf.classes_), 2))) | |
| print("Log loss (test) on the VotingClassifier :"+str(np.round(log_loss(Test_Y, vclf.predict_proba(Test_X), labels=vclf.classes_), 2))) | |
| print("Percentage of mis-classified for cv points :"+str(np.round((np.count_nonzero(vclf.predict(CV_X) - CV_Y)/CV_X.shape[0]*100), 2))+"%") | |
| print("Percentage of mis-classified for Test points :"+str(np.round((np.count_nonzero(vclf.predict(Test_X) - Test_Y)/Test_X.shape[0]*100), 2))+"%") | |
| print_confusionMatrix(Test_Y, vclf.predict(Test_X)) |
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
| # Get results | |
| best_alpha = alpha[np.argmin(cv_log_loss)] | |
| lr = SGDClassifier(loss = "log", alpha = best_alpha) | |
| stack_clf = StackingClassifier(classifiers=[calib_clf_NB, calib_clf_LR, calib_clf_SVM, calib_clf_RF], meta_classifier=lr, use_probas=True) | |
| stack_clf.fit(Train_X, Train_Y) | |
| trainLogLoss = log_loss(Train_Y, stack_clf.predict_proba(Train_X)) | |
| print("Train Log Loss on Stacking Classifier = "+str(np.round(trainLogLoss, 4))) |
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
| cv_log_loss = [] | |
| alpha = [10**x for x in range(-3, 0)] | |
| for i in alpha: | |
| lr = SGDClassifier(loss = "log", alpha = i) | |
| stack_clf = StackingClassifier(classifiers=[calib_clf_NB, calib_clf_LR, calib_clf_SVM, calib_clf_RF], meta_classifier=lr, use_probas=True) | |
| stack_clf.fit(Train_X, Train_Y) | |
| cv_log_loss.append(log_loss(CV_Y, stack_clf.predict_proba(CV_X))) | |
| print("Stacking Classifer : For alpha value: "+str(i)+" Log Loss: "+str(np.round(log_loss(CV_Y, stack_clf.predict_proba(CV_X)), 4))) |