python lgb代码 使用numpy
时间: 2023-11-12 18:33:34 浏览: 118
Python程序设计语言-Python函数
import numpy as np
import lightgbm as lgb
# 准备训练数据和标签
X_train = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
y_train = np.array([0, 1, 0])
# 准备测试数据和标签
X_test = np.array([[2, 3, 4], [5, 6, 7]])
y_test = np.array([1, 0])
# 将数据转换为lgb特定的数据格式
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test)
# 定义模型参数
params = {
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': 'binary_logloss',
'num_leaves': 31,
'learning_rate': 0.05,
'feature_fraction': 0.9
}
# 训练模型
num_round = 100
bst = lgb.train(params, train_data, num_round, valid_sets=[test_data])
# 预测
y_pred = bst.predict(X_test)
# 输出预测结果
print(y_pred)
阅读全文