从Pandas数据帧转换为TensorFlow张量对象

jlt*_*199 15 python pandas tensorflow

我还是Python,机器学习和TensorFlow的新手,但我会尽力向前跳.我可以使用一些帮助.

我的数据目前在Pandas数据框中.如何将其转换为TensorFlow对象?我试过了

dataVar_tensor = tf.constant(dataVar)
depth_tensor = tf.constant(depth)
Run Code Online (Sandbox Code Playgroud)

但是,我得到错误[15780 rows x 9 columns] - got shape [15780, 9], but wanted [].

我敢肯定这可能是一个简单的问题,但我真的可以使用这个帮助.

非常感谢

PS.我在Windows 10上使用Anaconda Python 3.5运行tensorflow 0.12

小智 10

这是我发现的一种适用于 Google Colab 的解决方案:

import pandas as pd
import tensorflow as tf
#Read the file to a pandas object
data=pd.read_csv('filedir')
#convert the pandas object to a tensor
data=tf.convert_to_tensor(data)
type(data)
Run Code Online (Sandbox Code Playgroud)

这将打印如下内容:

tensorflow.python.framework.ops.Tensor
Run Code Online (Sandbox Code Playgroud)


jlt*_*199 7

我想我已经拥有了!:d

我使用.as_matrix()将我的Pandas数据帧转换为Numpy数组

现在,使用

dataVar_tensor = tf.constant(dataVar, dtype = tf.float32, shape=[15780,9])
depth_tensor = tf.constant(depth, 'float32',shape=[15780,1])
Run Code Online (Sandbox Code Playgroud)

似乎工作.我不能说它确实存在,因为我有其他障碍需要克服以使我的代码正常工作,但它有望朝着正确的方向迈出一步.感谢你的帮助

顺便说一下,我在下一个问题转换TensorFlow教程中使用我自己的数据继续尝试让教程处理我自己的数据.


VS_*_*_FF 4

基于numpy数组输入数据,以下工作很容易实现:

import tensorflow as tf
import numpy as np
a = np.array([1,2,3])
with tf.Session() as sess:
    tf.global_variables_initializer().run()

    dataVar = tf.constant(a)
    print(dataVar.eval())

-> [1 2 3]
Run Code Online (Sandbox Code Playgroud)

不要忘记启动sessionandrun()eval()你的张量对象来查看它的内容;否则它只会给你它的一般描述。

我怀疑,由于您的数据位于 DataFrame 而不是简单的数组中,因此您需要尝试使用当前未指定的shape 参数tf.constant(),以帮助它理解 DataFrame 的维度并处理其索引等.?