tensorflow2.0中,tensor类型数据到pandas的DataFrame类型

tf2.0中,网络的预测结果往往是tensor(类型)。tensor虽然也有一系列大操作,但是不便导出到excel。有没有一种操作可以将tensor转pandas的DataFrame呢?
在tf2.0,可以借助Numpy 的narray数组作为媒介。
t e n s o r ⟶   n a r r a y ⟶   D a t a F r a m e tensor \longrightarrow\ narray \longrightarrow\ DataFrame tensor narray DataFrame

  1. t e n s o r ⟷   n a r r a y tensor \longleftrightarrow\ narray tensor narray
import tensorflow as tf
import numpy as np
#narray  to tensor
narray = np.array([[1,2,3],[4,5,6],[7,8,9]])
print ('narray=', narray, type(narray))
tensor=tf.constant(narray)
print ('tensor=' ,tensor, type(tensor))
#tensor  to  narray
n2 = np.array(tensor)
print ('n2=', n2, type(n2))

>>>
>narray= [[1 2 3]
 [4 5 6]
 [7 8 9]] <class 'numpy.ndarray'>
tensor= tf.Tensor(
[[1 2 3]
 [4 5 6]
 [7 8 9]], shape=(3, 3), dtype=int32) <class 'tensorflow.python.framework.ops.EagerTensor'>
n2= [[1 2 3]
 [4 5 6]
 [7 8 9]] <class 'numpy.ndarray'>
  1. n u m p y ⟶ D a t a F r a m e numpy \longrightarrow DataFrame numpyDataFrame
#接上面的代码
import pandas as pd
#Narray to DataFrame
df = pd.DataFrame(n2)
df
>>>
>
	0	1	2
_____________
0	1	2	3
1	4	5	6
2	7	8	9

你可能感兴趣的:(数据处理)