lightgbm 代码 python
时间: 2024-12-29 14:26:31 浏览: 10
### 使用Python编写的LightGBM模型代码示例
为了创建并训练一个简单的LightGBM模型,可以按照如下方式操作:
首先安装所需的库[^1]:
```bash
pip install lightgbm scikit-learn pandas numpy
```
加载必要的模块并将数据集分为特征和标签两部分:
```python
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import pandas as pd
import numpy as np
# 加载你的数据集到DataFrame中
data = pd.read_csv('your_dataset.csv')
X = data.drop(columns=['target_column']) # 特征列
y = data['target_column'] # 目标列
# 将数据划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
定义参数并构建LightGBM的数据结构:
```python
params = {
'objective': 'binary', # 对于二分类问题设置目标函数为'binary'
'metric': ['auc'], # AUC作为评估指标之一
}
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test, reference=train_data)
```
训练模型并保存最佳迭代次数的结果:
```python
bst = lgb.train(params,
train_data,
valid_sets=[test_data],
num_boost_round=999,
early_stopping_rounds=10)
best_iteration = bst.best_iteration
print(f"Best iteration: {best_iteration}")
```
利用训练好的模型预测新样本,并计算准确率得分:
```python
predictions_probabilities = bst.predict(X_test, num_iteration=best_iteration)
predicted_classes = (predictions_probabilities >= 0.5).astype(int)
accuracy = accuracy_score(y_test, predicted_classes)
print(f"Accuracy score on testing set is {accuracy:.4f}")
```
阅读全文