使用python将数组1D和2D的元组转换为数据帧

2021-12-08 05:47:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这真是个好主意模型.predic返回。?如何在数据帧的列中转换这个元组?你知道吗

(array([1., 1., 1., ..., 1., 1., 1.]), array([[0.46502338, 0.53497662],
        [0.47072865, 0.52927135],
        [0.4696557 , 0.5303443 ],
        ...,
        [0.47139825, 0.52860175],
        [0.46367829, 0.53632171],
        [0.46586898, 0.53413102]]))
<class 'tuple'>

这些对我来说都不管用

pd.DataFrame(dict(class_pred=tuple[0], prob_0=tuple[1], prob_1=tuple[2]))
pd.DataFrame(np.column_stack(tuple),columns=['class_pred','prob_0','prob_1'])

我想得到这样的东西:

class_pred  prob_0    prob_1
1           0.470728  0.5292713

AniSkywalker解决方案非常有效。你知道吗

type(data)
print(data)

tuple
(array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
 array([[0.46502338, 0.53497662],
        [0.47072865, 0.52927135],
        [0.4696557 , 0.5303443 ],
        [0.46511921, 0.53488079],
        [0.46739934, 0.53260066],
        [0.47387646, 0.52612354],
        [0.4737461 , 0.5262539 ],
        [0.47052631, 0.52947369],
        [0.47658316, 0.52341684],
        [0.47222654, 0.52777346]]))
df_pred = pd.DataFrame(data=dict(pred=data[0], prob_0=data[1][:,0], prob_1=data[1][:,1]))
print(df_pred)

    pred    prob_0      prob_1
0   1.0     0.465023    0.534977
1   1.0     0.470729    0.529271
2   1.0     0.469656    0.530344
3   1.0     0.465119    0.534881
4   1.0     0.467399    0.532601
5   1.0     0.473876    0.526124
6   1.0     0.473746    0.526254
7   1.0     0.470526    0.529474
8   1.0     0.476583    0.523417
9   1.0     0.472227    0.527773