Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from Pandas dataframe to TensorFlow tensor object

I'm still new to Python, Machine Learning and TensorFlow, but doing my best to jump right in head-first. I could use some help though.

My data is currently in a Pandas dataframe. How can I convert this to TensorFlow object? I've tried

dataVar_tensor = tf.constant(dataVar) depth_tensor = tf.constant(depth) 

But, I get errors [15780 rows x 9 columns] - got shape [15780, 9], but wanted [].

I'm sure this is probably a straightforward question, but I could really use the help.

Many thanks

ps. I'm running tensorflow 0.12 with Anaconda Python 3.5 on Windows 10

like image 446
jlt199 Avatar asked Feb 16 '17 23:02

jlt199


People also ask

Can TensorFlow work with DataFrame?

TensorFlow tensors require that all elements have the same dtype . So, in this case, you need to start treating it as a dictionary of columns, where each column has a uniform dtype . A DataFrame is a lot like a dictionary of arrays, so typically all you need to do is cast the DataFrame to a Python dict.

Is PyArrow faster than pandas?

There's a better way. It's called PyArrow — an amazing Python binding for the Apache Arrow project. It introduces faster data read/write times and doesn't otherwise interfere with your data analysis pipeline. It's the best of both worlds, as you can still use Pandas for further calculations.


1 Answers

Here is one solution I found that works on 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) 

This will print something like:

tensorflow.python.framework.ops.Tensor 
like image 76
Thedarknight Avatar answered Oct 05 '22 23:10

Thedarknight