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 
相关推荐
GISer_Jing6 小时前
AI自动化工作流:智能驱动未来(升级研究生项目!!!)
人工智能·前端框架·自动化
草捏子6 小时前
Agent Skills:让 AI 一次学会、永远记住的能力扩展方案
人工智能
NocoBase7 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
后端小肥肠7 小时前
OpenClaw实战|从识图到公众号内容自动化,我跑通了完整链路
人工智能·aigc·agent
Elastic 中国社区官方博客7 小时前
快速 vs. 准确:衡量量化向量搜索的召回率
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
qq_381338507 小时前
【技术日报】2026-03-18 AI 领域重磅速递
大数据·人工智能
NocoBase7 小时前
开源项目管理工具选型指南(2026年最新)
人工智能·开源·无代码
feasibility.7 小时前
AI 爬虫高手养成:Openclaw+Scrapling 手动部署 + 采集策略(以Walmart 电商平台为例)
人工智能·爬虫·科技·机器人·agi·openclaw·scrapling
程序员老猫7 小时前
前端菜鸡狂喜!DeepSeek+Gemini,嘴炮出完整博客方案
人工智能
AI周红伟8 小时前
周红伟:《OpenClaw安全防控:OpenClaw++Skills智能体安全部署、实操和企业应用实操》
人工智能·阿里云·云计算·腾讯云·openclaw