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_陈寒5 分钟前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端
刘一说10 分钟前
AI科技热点日报 | 2026年6月1日
人工智能·科技
阿里云大数据AI技术11 分钟前
性能提升20倍:阿里云 Milvus 深度优化磁盘索引,重新定义亿级向量检索
人工智能
包子BI大数据24 分钟前
3.openclaw小龙虾简单版安装教程
人工智能·python·ai
zhangfeng113340 分钟前
超算/曙光DCU集群 昆山站 根目录文件夹逐项释义(HTC调度集群环境、国产DCU算力节点)
人工智能·pytorch·机器学习
格桑阿sir42 分钟前
15-大模型智能体开发工程师:深度学习MCP协议(Model Context Protocol)
人工智能·ai·大模型·agent·sse·mcp·streamable http
程序员佳佳44 分钟前
深度解析:向量引擎如何影响AI内容收录?附3个月实测数据
人工智能·gpt·自动化·ai写作·codex
feng14561 小时前
OpenSREClaw - AI 本体论思维
运维·人工智能
zhangxingchao1 小时前
AI应用开发八:RAG相关技术总结
前端·人工智能·后端
码农小旋风1 小时前
国内使用 Claude 的 5 种路径:网页、订阅、API 和企业方案怎么选
人工智能·chatgpt