【发布时间】:2016-02-23 21:36:57
【问题描述】:
由于某种原因,每当我运行ensemble.RandomForestClassifier() 并使用.predict_proba() 方法时,它都会返回[n_classes, n_samples] 形状的二维数组,而不是应该为per the docs. 的[n_samples, n_classes] 形状
这是我的示例代码:
# generate some sample data
X = np.array([[4, 5, 6, 7, 8],
[0, 5, 6, 2, 3],
[1, 2, 6, 5, 8],
[6, 1, 1, 1, 3],
[2, 5, 3, 2, 0]])
»» X.shape
(5, 5)
y = [['blue', 'red'],
['red'],
['red', 'green'],
['blue', 'green'],
['orange']]
X_test = np.array([[4, 6, 1, 2, 8],
[0, 0, 1, 5, 1]])
»» X_test.shape
(2, 5)
# binarize text labels
mlb = preprocessing.MultiLabelBinarizer()
lb_y = mlb.fit_transform(y)
»» lb_y
[[1 0 0 1]
[0 0 0 1]
[0 1 0 1]
[1 1 0 0]
[0 0 1 0]]
»» lb_y.shape
(5, 4)
到目前为止一切正常。但是当我这样做时:
rfc = ensemble.RandomForestClassifier(random_state=42)
rfc.fit(X, lb_y)
yhat_p = rfc.predict_proba(X_test)
»» yhat_p
[array([[ 0.5, 0.5],
[ 0.7, 0.3]]),
array([[ 0.4, 0.6],
[ 0.5, 0.5]]),
array([[ 0.7, 0.3],
[ 0.7, 0.3]]),
array([[ 0.7, 0.3],
[ 0.6, 0.4]])]
我的yhat_p 大小是[n_classes, n_samples] 而不是[n_samples, n_classes]。有人能告诉我为什么我的输出被转置了吗?注意:.predict() 方法可以正常工作。
【问题讨论】:
标签: python scikit-learn random-forest