m0_61102691 2022-06-11 11:54 采纳率: 0%
浏览 42
已结题

基于特征的多标签分类该如何训练呢

基于特征的多标签分类该如何训练呢
1.有特征x1、x2……xn个特征
2.lable:有01234 一共5类
那我应该如何去训练出呢。
我看了很多资料,大多都是图像的多分类、文本的多分类,但就是不知道如何套上去。

不知道哪位能不能指点下,能不能提供demo示例

  • 写回答

3条回答 默认 最新

  • 白驹_过隙 算法领域新星创作者 2022-06-11 12:24
    关注
    获得5.00元问题酬金

    给你举个随机森林模型的例子

    import joblib
    from sklearn.ensemble import RandomForestClassifier
    import pandas as pd
    from sklearn.model_selection import train_test_split
    import numpy as np
    from collections import Counter
    # 1. 加载数据集
    data = pd.read_csv('data/data137267/train.csv.zip')
    test_df = pd.read_csv('data/data137267/test.csv.zip')
    # 2. 数据基本处理
    data.fillna(0)#如果有空值用0填充
    # 3. 特征值和目标值
    sample = len(data)
    x = data.sample(sample)
    # 确定目标值和特征值
    y = x['Activity'] #预测的那一列
    x = x.drop(["Activity"], axis=1) #除了预测那一列的其他列
    
    # 4. 数据集分割
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, stratify=y, random_state=22)
    #分割训练集测试集
    
    # 5. 随机森林训练和存储
    # 随机森林训练 准确率0.93
    estimator1 = RandomForestClassifier(n_estimators=60,oob_score=True, random_state=22)
    print('训练数据形状:', x_train.shape)
    
    print('随机森林模型:')
    estimator1.fit(x_train, y_train)
    # 6.保存模型
    joblib.dump(estimator1, 'random_forest.pth')
    
    # 7. 模型预测
    from sklearn.metrics import classification_report
    accurary = estimator1.score(x_test, y_test)
    print('OOB:', estimator1.oob_score_)
    print('ACC:', accurary)
    # 打印精度、召回率、f1-score、accuracy
    print(classification_report(y_true=y_test, y_pred=estimator1.predict(x_test)))
    
    
    评论

报告相同问题?

问题事件

  • 系统已结题 6月19日
  • 创建了问题 6月11日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog