📜  LightGBM - Python (1)

📅  最后修改于: 2023-12-03 15:02:41.136000             🧑  作者: Mango

LightGBM - Python

LightGBM is a gradient boosting framework that uses tree-based learning algorithms. It is designed to be distributed and efficient, with higher accuracy and speed than other gradient boosting libraries.

Installation

LightGBM can be installed using pip:

pip install lightgbm
Usage

LightGBM can be used for both regression and classification tasks. Here is an example of its usage for a classification task:

import lightgbm as lgb
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load data
data = pd.read_csv('data.csv')

# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(data.iloc[:, :-1], data.iloc[:, -1], test_size=0.2)

# Create and train the model
params = {
    'boosting_type': 'gbdt',
    'objective': 'binary',
    'metric': 'binary_logloss',
    'num_leaves': 31,
    'learning_rate': 0.05
}
train_data = lgb.Dataset(X_train, label=y_train)
model = lgb.train(params, train_data, num_boost_round=100)

# Make predictions on test set
y_pred = model.predict(X_test)
y_pred = [1 if i>0.5 else 0 for i in y_pred]

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
Conclusion

LightGBM is a powerful gradient boosting library that can be used for regression and classification tasks. Its efficient implementation and high accuracy make it a popular choice for machine learning tasks.