回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我已经阅读了许多建议的解决方案,但我不能使这项工作</p>
<p>我有一系列的预测:</p>
<pre><code>y_prob = best_model.predict_proba(data)
print(y_prob)
array([[0.32],
[0.5 ],
[0.32],
...,
[0.46],
[0.51],
[0.51]], dtype=float32)
print(y_prob.shape)
(48775, 1)
</code></pre>
<p>我一直试图将其作为一列预测添加到原始数据框中,但我尝试的一切都不起作用</p>
<pre><code># attempt 1
data['probability'] = pd.Series(y_prob)
Exception: Data must be 1-dimensional
# attempt 2
data['probability'] = y_prob
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
# attempt 3
data['probability'] = y_prob.tolist()
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
# attempt 4
data['probability'] = [i[0] for i in y_prob]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
# attempt etc etc etc.
</code></pre>
<p>我知道这可能是个愚蠢的错误。。但我就是找不到解决办法</p>
<p>数据维度:</p>
<pre><code>
print(y_prob.shape)
print(data.shape)
(48775, 1)
(48775, 121)
</code></pre>
<p>编辑:从评论中添加建议:</p>
<pre><code>dat['probability'] = pd.Series(y_prob.reshape((y_prob.shape[0],)))
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
data['probability'] = y_prob.ravel()
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
data['probability'] = pd.Series(y_prob.ravel())
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
</code></pre>