<jupyter_start><jupyter_text>LightGBM のチカラを見せてやろ<jupyter_code>import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import lightgbm as lgb
from lightgbm import LGBMClassifier
#from bayes_opt import BayesianOptimization
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
from sklearn import cross_validation
# Function for Measure Performance# 
from  sklearn  import  metrics
def measure_performance(X,y,clf, show_accuracy=True, show_classification_report=True, show_confusion_matrix=True, show_roc_auc = True):
    y_pred = clf.predict(X)
    y_predprob = clf.predict_proba(X)[:,1]
    if show_accuracy:
        print ("Accuracy:{0:.3f}".format(metrics.accuracy_score(y,y_pred))),"\n"

    if show_classification_report:
        print("Classification report")
        print(metrics.classification_report(y,y_pred)),"\n"
        
    if show_confusion_matrix:
        print("Confusion matrix")
        print(metrics.confusion_matrix(y,y_pred)),"\n"  
        
    if show_roc_auc:
        print("ROC AUC Score")
        print(metrics.roc_auc_score(y,y_predprob)),"\n"
#載入資料
train=pd.read_csv('train.csv',encoding='utf-8')
test=pd.read_csv('test_public.csv',encoding='utf-8')
submit=pd.read_csv('sampleSubmission.csv',encoding='utf-8')
#Data Preparation for LightGBM
import os

# use LabelEncoder to convert categorical features to int type before construct Dataset
from sklearn.preprocessing import LabelEncoder
def label_encoder(input_df, encoder_dict=None):
    """ Process a dataframe into a form useable by LightGBM """
    # Label encode categoricals
    categorical_feats = input_df.columns[input_df.dtypes == 'object']
    for feat in categorical_feats:
        encoder = LabelEncoder()
        input_df[feat] = encoder.fit_transform(input_df[feat].fillna('NULL'))
    return input_df, categorical_feats.tolist(), encoder_dict
application_train, categorical_feats, encoder_dict = label_encoder(train)
X = train.drop(['Class'], axis=1)
y = train.Class

# Prepare dataset 
seed = 7
test_size = 0.3
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=test_size, random_state=seed)
# Grid Search
print('Start training...')
estimator = lgb.LGBMClassifier(objective = 'binary', learning_rate = 0.05, n_estimators = 100, random_state=0)

param_grid = {
    'num_leaves': [30,35,40,45], #[35,40,45]做完是40
    'feature_fraction': [0.2,0.3,0.4],#做過[0.2,0.3,0.4,0.5]出來是0.4
    #'bagging_fraction': [0.6,0.7,0.8],
    'max_depth':[6,7,8],
    'max_bin':[20],
    #'lambda_l1':[0.3,0.6],#V4開始做lambda_l1
    'lambda_l2':[0.08,0.09,0.10],
    'min_split_gain':[0.04,0.05,0.06],#做過[0.04,0.05,0.06]出來0.06
    'min_child_weight':[7]
}

%time LGBM_grid = GridSearchCV(estimator, param_grid)
import warnings
warnings.filterwarnings("ignore")
%time LGBM_grid.fit(X_train, y_train)
print('Best parameters found by grid search are:', LGBM_grid.best_params_)
# Final Model
evals_result = {} 
print('Start predicting...')
LGBM= lgb.LGBMClassifier(objective = 'binary',
                         learning_rate = 0.05, 
                         n_estimators = 100, 
                         random_state=0,
                         num_leaves = LGBM_grid.best_params_['num_leaves'],
                         feature_fraction = LGBM_grid.best_params_['feature_fraction'], 
                         #bagging_fraction = LGBM_grid.best_params_['bagging_fraction'],
                         max_depth = LGBM_grid.best_params_['max_depth'],
                         max_bin = LGBM_grid.best_params_['max_bin'],
                         #lambda_l1 = LGBM_grid.best_params_['lambda_l1'],
                         lambda_l2 = LGBM_grid.best_params_['lambda_l2'],
                         min_split_gain = LGBM_grid.best_params_['min_split_gain'],
                         min_child_weight = LGBM_grid.best_params_['min_child_weight'])
%time LGBM_fit = LGBM.fit(X_train, y_train)
print('Predicting is over')
#v2
LGBM_grid_measure = measure_performance(X = X_test, y = y_test, clf = LGBM_grid_final, show_classification_report=True, show_confusion_matrix=True)

# feature importances
print('Feature importances:', list(LGBM_grid_final.feature_importances_))

# visualization
print('Plot feature importances...')
ax = lgb.plot_importance(LGBM_fit, max_num_features=len(train))
plt.show()
#v1
LGBM_grid_measure = measure_performance(X = X_test, y = y_test, clf = LGBM_grid_final, show_classification_report=True, show_confusion_matrix=True)

# feature importances
print('Feature importances:', list(LGBM_grid_final.feature_importances_))

# visualization
print('Plot feature importances...')
ax = lgb.plot_importance(LGBM_fit, max_num_features=10)
plt.show()
LGBM_pred =LGBM.predict(test)
submit['Class'] = LGBM_pred
submit['Class'] = submit['Class'].astype(int)
submit.to_csv('submit_002.csv', index= False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12169637369391519, 'fraction_numerical': 0.023970497848801474, 'mean_word_length': 4.232583065380493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '184279', 'license': 'MIT', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1713, 'n_words': 287, 'path': '08_Model_Development/Model_Selection/GridSearchCV/LightGBM with GridSearchCV.ipynb', 'repo_name': 'erik1110/Data-Science', 'size': '4881'}