如何在clf.predict_proba(X_test)中获得更多小数?

时间:2017-12-22 11:27:03

标签: python numpy machine-learning scikit-learn

我有一个pandas数据帧,用于二进制分类案例(类别A和类别B)。要获得X_train, X_test, y_train, y_test,我会像这样分开70:30:

from sklearn.model_selection import train_test_split
target = pd.DataFrame(data['good'])
features = data.drop('good', axis=1)
X_train, X_test, y_train, y_test = train_test_split(features, 
                                                    target, 
                                                    test_size = 0.3, 
                                                    random_state = 0)

然后我做了RandomForest分类器,用这个代码

from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_jobs=2, random_state=0)
model = clf.fit(X_train, y_train)

像往常一样,你可以通过clf.predict(X_test)来做预测。它给出numpy.ndarray这样的对象

array(['0', '0', '1', '0', '0', '1', '0', '1', '1', '1'], dtype=object)

然后,我想通过clf.predict_proba(X_test)计算预测可能性,结果是

array([[ 0.7  ,  0.3  ],
       [ 0.8  ,  0.2  ],
       [ 0.4  ,  0.6  ],
       [ 0.8  ,  0.2  ],
       [ 0.5  ,  0.5  ],
       [ 0.1  ,  0.9  ],
       [ 0.5  ,  0.5  ],
       [ 0.3  ,  0.7  ],
       [ 0.3  ,  0.7  ],
       [ 0.5  ,  0.5  ]])

我想在clf.predict_proba(X_test)输出中获得更多小数。 (我预计3位小数)例如,

array([[ 0.712  ,  0.288  ],
       [ 0.845  ,  0.155  ... etc

如果答案也将clf.predict(X_test)clf.predict_proba(X_test)转换并合并到pandas数据帧会更好,因为我会继续计算GINI索引。提前致谢

1 个答案:

答案 0 :(得分:0)

增加模型参数中的“ n_estimators”(好像您已将其设置为默认值10)。