gbm模型做分类

导入相关的包

python 复制代码
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from lightgbm import LGBMClassifier
from sklearn.preprocessing import PolynomialFeatures

获取df中的格式类型

python 复制代码
object_columns = df.select_dtypes(include='object').columns
for col in object_columns:
    df[col] = df[col].fillna('0')
    df[col] = df[col].map(dict(zip(list(set(df[col])), [i for i in range(len(list(set(df[col]))))])))

多项式特征提取方法

python 复制代码
from sklearn.preprocessing import PolynomialFeatures
df = df.fillna(0)
poly = PolynomialFeatures(degree=3, include_bias=False, interaction_only=True)
x_train = df.drop('slide', axis=1)
y_train = df['slide']
poly_features = poly.fit_transform(x_train)
feature_names = poly.get_feature_names_out()
poly_df = pd.DataFrame(poly_features, columns=feature_names)
X_df = poly_df

模型训练

python 复制代码
train_x, test_x, train_y, test_y = train_test_split(X_df, y_train, test_size=0.2, random_state=42)

model = LGBMClassifier(
    boosting_type='gbdt',  # 基学习器 gbdt:传统的梯度提升决策树; dart:Dropouts多重加性回归树
    n_estimators=500,  # 迭代次数
    learning_rate=0.1,  # 步长
    max_depth=4,  # 树的最大深度
    min_child_weight=1,  # 决定最小叶子节点样本权重和
    # min_split_gain=0.1,  # 在树的叶节点上进行进一步分区所需的最小损失减少
    subsample=1,  # 每个决策树所用的子样本占总样本的比例(作用于样本)
    colsample_bytree=1,  # 建立树时对特征随机采样的比例(作用于特征)典型值:0.5-1
    random_state=27,  # 指定随机种子,为了复现结果
    importance_type='gain',  # 特征重要性的计算方式,split:分隔的总数; gain:总信息增益
    objective='binary',
)

model.fit(train_x, train_y, eval_metric="auc", verbose=50, \
                          eval_set=[(train_x, train_y), (test_x, test_y)], \
                         )
print(classification_report(model.predict(test_x), test_y))

特征重要性

python 复制代码
feature_import_df = pd.DataFrame(zip(model.feature_name_, model.feature_importances_))
feature_import_df.columns = ['feature', 'import_values']
feature_import_df = feature_import_df.sort_values('import_values', ascending=False)
feature_import_df 
相关推荐
IT_陈寒7 分钟前
Vite静态资源加载把我坑惨了
前端·人工智能·后端
后端小肥肠9 分钟前
我把自己蒸馏成小肥肠.skill,相关答疑全能做,一人公司终于能聚焦核心业务
人工智能·agent
天一生水water26 分钟前
Time-Series-Library 仓库的使用
人工智能
HeteroCat26 分钟前
DeepSeek V4 来了:我熬了一中午,把技术报告啃完了
人工智能
阿杰学AI31 分钟前
AI核心知识135—大语言模型之 OpenClaw(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·ai编程·openclaw
薛定e的猫咪34 分钟前
多智能体强化学习求解 FJSP 变体全景:动态调度、AGV 运输、绿色制造与开源代码导航
人工智能·学习·性能优化·制造
机器之心35 分钟前
DeepSeek V4 双版本正式上线!
人工智能·openai
机器之心40 分钟前
机器人马拉松超越人类之后:本体走到尽头,智能成为下半场
人工智能·openai
可观测性用观测云41 分钟前
观测云 Obsy AI Copilot:带上你的 AI 副驾,进入你的观测现场
人工智能
小明的IT世界1 小时前
Agent系列3:改变你做 AI Agent 的方式
人工智能