2017-02-16 27 views
11

我對Python,機器學習和TensorFlow仍然很陌生,但是盡我所能以頭朝下的方式跳到正確的位置。儘管我可以使用一些幫助。從Pandas數據框轉換到TensorFlow張量對象

我的數據目前處於Pandas數據框中。我如何將其轉換爲TensorFlow對象?我試過

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

但是,我得到錯誤[15780 rows x 9 columns] - got shape [15780, 9], but wanted []

我敢肯定這可能是一個簡單的問題,但我真的可以使用幫助。

非常感謝

ps。我tensorflow 0.12蟒蛇的Python 3.5在Windows上運行10

+0

你想用這個數據做什麼?它是你想訓練的神經網絡的輸入嗎?從錯誤信息它看起來像常量只是想要一個常數,所以一個int或一個浮點數,而不是矩陣 – rAyyy

+0

@rAyyy是的,我的計劃是最終輸入到神經網絡。目前,我只是試圖從教程中獲取MNIST示例,並使其適用於我自己的數據。我正在閱讀從csv文件使用pandas.read_csv() – jlt199

回答

2

容易根據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] 

不要忘了啓動sessionrun()eval()您的張量對象看到它的內容;否則它只會給你它的通用描述。

我懷疑,因爲你的數據在數據幀,而不是一個簡單的數組,你需要的tf.constant()shapeparameter,您目前不指定實驗,以幫助其理解數據框的維和處理其指數等?

+0

謝謝。我正在運行一個InteractiveSession,我嘗試了'dataVar_tensor = tf.constant(dataVar,dtype = tf.float32,shape = [15780,9])的幾種不同的變體''但是到目前爲止沒有運氣 – jlt199

6

我想我明白了! :d

我已經轉換我的熊貓數據幀到numpy的使用數組.as_matrix()

現在,使用

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

似乎工作。我不能說它確實是因爲我有其他障礙需要克服才能讓我的代碼正常工作,但希望它朝着正確的方向邁出了一步。感謝您的幫助

順便說一句,我得到的教程,對我自己的數據工作的審判繼續在我的下一個問題Converting TensorFlow tutorial to work with my own data

+0

我轉換了一個熊貓系列(y_train)的整數,張量,然後到one_hot如下: dataVar_tensor = tf.Variable(y_train.as_matrix(),dtype = tf.int32) result = tf.one_hot(dataVar_tensor,depth) – Vaibhav

相關問題