How to use LightGBM Classifier and Regressor in Python?
0

How to use LightGBM Classifier and Regressor in Python?

This recipe helps you use LightGBM Classifier and Regressor in Python
In [2]:
## How to use LightGBM Classifier and Regressor in Python
def Snippet_169():
    print()
    print(format('How to use LightGBM Classifier and Regressor in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")
    # load libraries
    from sklearn import datasets
    from sklearn import metrics
    from sklearn.model_selection import train_test_split
    import matplotlib.pyplot as plt
    import seaborn as sns

    plt.style.use('ggplot')

    import lightgbm as ltb
    # load the iris datasets
    dataset = datasets.load_breast_cancer()
    X = dataset.data; y = dataset.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

    # fit a lightGBM model to the data
    model = ltb.LGBMClassifier()
    model.fit(X_train, y_train)
    print(); print(model)
    # make predictions
    expected_y  = y_test
    predicted_y = model.predict(X_test)
    # summarize the fit of the model
    print(); print(metrics.classification_report(expected_y, predicted_y))
    print(); print(metrics.confusion_matrix(expected_y, predicted_y))

    # load the boston datasets
    dataset = datasets.load_boston()
    X = dataset.data; y = dataset.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
    # fit a lightGBM model to the data
    model = ltb.LGBMRegressor()
    model.fit(X_train, y_train)
    print(); print(model)
    # make predictions
    expected_y  = y_test
    predicted_y = model.predict(X_test)
    # summarize the fit of the model
    print(); print(metrics.r2_score(expected_y, predicted_y))
    print(); print(metrics.mean_squared_log_error(expected_y, predicted_y))
    plt.figure(figsize=(10,10))
    sns.regplot(expected_y, predicted_y, fit_reg=True, scatter_kws={"s": 100})
Snippet_169()
**************How to use LightGBM Classifier and Regressor in Python**************

LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,
        learning_rate=0.1, max_depth=-1, min_child_samples=20,
        min_child_weight=0.001, min_split_gain=0.0, n_estimators=100,
        n_jobs=-1, num_leaves=31, objective=None, random_state=None,
        reg_alpha=0.0, reg_lambda=0.0, silent=True, subsample=1.0,
        subsample_for_bin=200000, subsample_freq=0)

              precision    recall  f1-score   support

           0       0.97      0.97      0.97        59
           1       0.98      0.98      0.98        84

   micro avg       0.97      0.97      0.97       143
   macro avg       0.97      0.97      0.97       143
weighted avg       0.97      0.97      0.97       143


[[57  2]
 [ 2 82]]

LGBMRegressor(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,
       learning_rate=0.1, max_depth=-1, min_child_samples=20,
       min_child_weight=0.001, min_split_gain=0.0, n_estimators=100,
       n_jobs=-1, num_leaves=31, objective=None, random_state=None,
       reg_alpha=0.0, reg_lambda=0.0, silent=True, subsample=1.0,
       subsample_for_bin=200000, subsample_freq=0)

0.8460639187768617

0.0320329198176757