Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório

0

I've converted my Pandas dataframe to a Numpy array using df.values

Now, using

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

seems to work. I can't say it does definitively because I have other hurdles to overcome to get my code working, but it's hopefully a step in the right direction. Thanks for all your help

As an aside, my trials of getting the tutorial to work on my own data are continued in my next question Converting TensorFlow tutorial to work with my own data

over 4 years ago · Santiago Trujillo Relatório

0

The following works easily based on numpy array input data:

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]

Don't forget to start the session and run() or eval() your tensor object to see its content; otherwise it will just give you its generic description.

I suspect that since your data is in the DataFrame rather than a simply array, you need to experiment with the shape parameter of tf.constant(), which you are currently not specifying, in order to help it understand the dimensionality of the DataFrame and deal with its indices, etc.?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda