zoukankan      html  css  js  c++  java
  • #调整随机森林的参数(调整max_features,结果未见明显差异)

    #调整随机森林的参数(调整max_features,结果未见明显差异)
    
    from sklearn import datasets
    X, y = datasets.make_classification(n_samples=10000,n_features=20,n_informative=15,flip_y=.5, weights=[.2, .8])
    
    import numpy as np
    training = np.random.choice([True, False], p=[.8, .2],size=y.shape)
    
    from sklearn.ensemble import RandomForestClassifier
    rf = RandomForestClassifier()
    rf.fit(X[training], y[training])
    preds = rf.predict(X[~training])
    print ("Accuracy:	", (preds == y[~training]).mean())
    
    
    from sklearn.metrics import confusion_matrix
    max_feature_params = ['auto', 'sqrt', 'log2', .01, .5, .99]
    confusion_matrixes = {}
    for max_feature in max_feature_params:
        rf = RandomForestClassifier(max_features=max_feature)
        rf.fit(X[training], y[training])
        print ("Accuracy:	", (preds == y[~training]).mean())
        confusion_matrixes= confusion_matrix(y[~training],rf.predict(X[~training]))
        print(max_feature,confusion_matrixes)
        print('--------------------------------------------------------------------')
    
    
    from sklearn.metrics import confusion_matrix
    y_true = [2, 0, 2, 2, 0, 1]
    y_pred = [0, 0, 2, 2, 0, 2]
    print(confusion_matrix(y_true, y_pred))
    
    y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    print(confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"]))
    '''
    Accuracy:     0.640324214792
    Accuracy:     0.640324214792
    auto [[278 403]
     [306 987]]
    --------------------------------------------------------------------
    Accuracy:     0.640324214792
    sqrt [[280 401]
     [324 969]]
    --------------------------------------------------------------------
    Accuracy:     0.640324214792
    log2 [[304 377]
     [320 973]]
    --------------------------------------------------------------------
    Accuracy:     0.640324214792
    0.01 [[285 396]
     [324 969]]
    --------------------------------------------------------------------
    Accuracy:     0.640324214792
    0.5 [[289 392]
     [305 988]]
    --------------------------------------------------------------------
    Accuracy:     0.640324214792
    0.99 [[294 387]
     [295 998]]
    --------------------------------------------------------------------
    [[2 0 0]
     [0 0 1]
     [1 0 2]]
    [[2 0 0]
     [0 0 1]
     [1 0 2]]
    '''
  • 相关阅读:
    bzoj4513: [Sdoi2016]储能表
    bzoj4000: [TJOI2015]棋盘
    bzoj3067: Hyperdrome
    bzoj4943: [Noi2017]蚯蚓
    bzoj4044: [Cerc2014] Virus synthesis
    bzoj3676: [Apio2014]回文串
    bzoj4543: [POI2014]Hotel加强版
    bzoj1921: [Ctsc2010]珠宝商
    bzoj4754: [Jsoi2016]独特的树叶
    作图的配色
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/5342198.html
Copyright © 2011-2022 走看看