• #调整随机森林的参数(调整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]]
    '''
  • 相关阅读:
    Kubernetes源码client-go的workqueue
    (转)Golang语言heap剖析及利用heap实现优先级队列
    (转)Kubernetes GC设计原则
    Kubernetes kubefed使用Cluster Selector
    使用kubeadm添加新节点到集群及相关问题解决
    Kubeadm颁发证书延迟到10年
    kubeadm升级Kubernetes证书
    混合kubebuilder与code generator编写CRD
    (转)Go项目的vendor目录是否需要提交?看这一篇就知道了
    Java 将Excel转为et和ett格式
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/5342198.html
Copyright © 2020-2023  润新知