现有训练集和测试集,帮我写一个lgb模型
时间: 2023-08-28 13:20:25 浏览: 101
好的,以下是一个简单的LGB模型的代码示例:
```python
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 假设你已经有了训练集和测试集的数据
X_train, X_test, y_train, y_test = train_test_split(train_data, train_labels, test_size=0.2, random_state=42)
# 将数据转换成LGB需要的数据格式
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test)
# 设置LGB的参数
params = {
'objective': 'multiclass',
'num_class': len(set(y_train)),
'metric': 'multi_logloss',
'boosting_type': 'gbdt',
'num_leaves': 31,
'learning_rate': 0.05,
'feature_fraction': 0.9,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'verbose': 0
}
# 训练LGB模型
model = lgb.train(params,
train_data,
num_boost_round=1000,
valid_sets=[train_data, test_data],
early_stopping_rounds=100,
verbose_eval=100)
# 预测测试集
y_pred = model.predict(X_test)
predictions = []
for i in range(len(y_pred)):
predictions.append(np.argmax(y_pred[i]))
# 计算准确率
accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
```
请注意,这只是一个简单的LGB模型示例,您可能需要根据您的数据和问题调整参数和模型结构。
阅读全文