zoukankan      html  css  js  c++  java
  • 使用sklearn中的BaggingClassifier去实现bagging分类

    使用sklearn去实现bagging分类
    这里采用3次10折交叉验证

    # test classification dataset
    from sklearn.datasets import make_classification
    # define dataset
    X, y = make_classification(n_samples=1000,   # 样本数目
                               n_features=20,    # 特征数目
                               n_informative=15, #  有效特征数目
                               n_redundant=5,    #冗余特征数目
                               # n_repeated=0,     # 重复特征个数(有效特征和冗余特征的随机组合)
                               # n_classes=3,      # 样本类别
                               # n_clusters_per_class=1, # 簇的个数
                               random_state=5)
    # summarize the dataset
    print(X.shape, y.shape)
    
    # evaluate bagging algorithm for classification
    from numpy import mean
    from numpy import std
    from sklearn.datasets import make_classification
    from sklearn.model_selection import cross_val_score
    from sklearn.model_selection import RepeatedStratifiedKFold
    from sklearn.ensemble import BaggingClassifier
    # define dataset
    X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=5)
    # define the model
    model = BaggingClassifier()
    # evaluate the model
    cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)  #重复三次的10折交叉验证
    n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1, error_score='raise')
    # report performance
    print('Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
    

  • 相关阅读:
    Spring+JCaptcha验证码使用示例
    Hibernate-Session使用的背后
    DWR+Spring配置使用
    Spring+Quartz配置定时任务
    利用HtmlParser解析网页内容
    利用HttpClient4访问网页
    利用Common-Fileupload上传文件图片
    利用Common-BeanUtils封装请求参数
    浮点数的一点东西
    基数排序——浮点数结构体进阶
  • 原文地址:https://www.cnblogs.com/phaLQ/p/15457426.html
Copyright © 2011-2022 走看看