<jupyter_start><jupyter_text>pyLigthGBM=======Python wrapper for Microsoft [LightGBM](https://github.com/Microsoft/LightGBM)  Make sure that you have installed LightGBM [Installation-Guide](https://github.com/Microsoft/LightGBM/wiki/Installation-Guide)**GitHub      :  [https://github.com/ArdalanM/pyLightGBM](https://github.com/ArdalanM/pyLightGBM) ****Author of this notebook :** <NAME> -------<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt

import os, gc
import numpy as np
import pandas as pd

from sklearn import metrics, model_selection
from sklearn.preprocessing import LabelEncoder

from pylightgbm.models import GBMRegressor<jupyter_output><empty_output><jupyter_text>DATA-------For this example used **data from Kaggle competition Allstate Claims Severity**  You can download data here : https://www.kaggle.com/c/allstate-claims-severity/data<jupyter_code>df_train = pd.read_csv("data/train.csv.zip")
print('Train data shape', df_train.shape)

df_test = pd.read_csv("data/test.csv.zip")
print('Test data shape', df_test.shape)<jupyter_output>Train data shape (188318, 132)
Test data shape (125546, 131)<jupyter_text>Extracting `loss` from train and `id` from test<jupyter_code>y = np.log(df_train['loss']+1).as_matrix().astype(np.float)
id_test = np.array(df_test['id'])<jupyter_output><empty_output><jupyter_text>Merging train and test data<jupyter_code>df = df_train.append(df_test, ignore_index=True)
del df_test, df_train
gc.collect()

print('Merged data shape', df.shape)<jupyter_output>Merged data shape (313864, 132)<jupyter_text>Droping not useful columns<jupyter_code>df.drop(labels=['loss', 'id'], axis=1, inplace=True)
feature_list = df.columns.tolist()<jupyter_output><empty_output><jupyter_text>Transfrom categorical features `cat` from 1 to 116<jupyter_code>le = LabelEncoder()

for col in df.columns.tolist():
    if 'cat' in col:
        df[col] = le.fit_transform(df[col])<jupyter_output><empty_output><jupyter_text>TRAIN, VALIDATION, TEST-------Split data into train, validation (for early stopping) and test set<jupyter_code>print('train-test split')
df_train, df_test = df.iloc[:len(y)], df.iloc[len(y):]
del df
gc.collect()

print('train-validation split\n')
X = df_train.as_matrix()
X_train, X_valid, y_train, y_valid = model_selection.train_test_split(X, y, test_size=0.2, random_state=42)
X_test = df_test.as_matrix()

del df_train, df_test
gc.collect()

print('Train shape', X_train.shape)
print('Validation shape', X_valid.shape)
print('Test shape', X_test.shape)<jupyter_output>train-test split
train-validation split

Train shape (150654, 130)
Validation shape (37664, 130)
Test shape (125546, 130)<jupyter_text>TRAINING GBMRegressor-------List of parameters and their explanation you can find here https://github.com/Microsoft/LightGBM/wiki/Quick-Start**don't forget to change `exec_path` here**<jupyter_code>seed = 42

gbmr = GBMRegressor(
    exec_path='/path/to/your/LightGBM/lightgbm', # change this to your LighGBM path
    config='',
    application='regression',
    num_iterations=500,
    learning_rate=0.1,
    num_leaves=10,
    tree_learner='serial',
    num_threads=4,
    min_data_in_leaf=10,
    metric='l2',
    feature_fraction=1.0,
    feature_fraction_seed=seed,
    bagging_fraction=1.0,
    bagging_freq=0,
    bagging_seed=seed,
    metric_freq=1,
    early_stopping_round=10
)

gbmr.fit(X_train, y_train, test_data=[(X_valid, y_valid)])
print("Mean Square Error:", metrics.mean_absolute_error(y_true=(np.exp(y_valid)-1), y_pred=(np.exp(gbmr.predict(X_valid))-1)))
print('Best round', gbmr.best_round)<jupyter_output>Best round 500<jupyter_text>FEATURE IMPORTANCE-------TOP 10<jupyter_code>feature_dict = dict(zip(range(len(feature_list)), feature_list))
df_fi = pd.DataFrame(gbmr.feature_importance(), columns=['feature', 'importance'])
df_fi = df_fi.replace({"feature": feature_dict})

del feature_dict, feature_list

top = 10

plt.figure()
df_fi.head(top).plot(kind='barh',
                     x='feature',
                     y='importance',
                     sort_columns=False,
                     legend=False,
                     figsize=(10, 6),
                     facecolor='#1DE9B6',
                     edgecolor='white')

plt.title('XGBoost Feature Importance')
plt.xlabel('relative importance')<jupyter_output><empty_output><jupyter_text>SUBMISSION-------Predicting Test set<jupyter_code>y_test_preds = gbmr.predict(X_test)
y_test_preds=(np.exp(y_test_preds)-1)<jupyter_output>[LightGBM] finished load parameters
[LightGBM] Loaded 500 modles

[LightGBM] finish predict init
[LightGBM] start prediction for data /tmp/tmp35nwyz05/X_to_pred.svm, and data has label
[LightGBM] finish predict<jupyter_text>Make submission file<jupyter_code>df_submission = pd.read_csv('data/sample_submission.csv.zip')
df_submission['loss'] = y_test_preds<jupyter_output><empty_output><jupyter_text>Save submission file<jupyter_code>df_submission.to_csv('submission.csv',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1343612334801762, 'fraction_numerical': 0.021425710853023628, 'mean_word_length': 5.537958115183246, 'pattern_counts': {'":': 1, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '493884', 'license': 'MIT', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1766, 'n_words': 323, 'path': 'notebooks/regression_example_kaggle_allstate.ipynb', 'repo_name': 'ArdalanM/pyLightGBM', 'size': '5002'}