Python 如何为tensorflow建模数据?
我有以下表格的数据:Python 如何为tensorflow建模数据?,python,python-3.x,tensorflow,neural-network,tensor,Python,Python 3.x,Tensorflow,Neural Network,Tensor,我有以下表格的数据: A B C D E F G 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 1 0 0 ... 1 0 1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 0 0 0 1 0 1 0
A B C D E F G
1 0 0 1 0 0 1
1 0 0 1 0 0 1
1 0 0 1 0 1 0
1 0 1 0 1 0 0
...
1 0 1 0 1 0 0
0 1 1 0 0 0 1
0 1 1 0 0 0 1
0 1 0 1 1 0 0
0 1 0 1 1 0 0
A、B、C、D
是我的输入,E、F、G
是我的输出。我使用TensorFlow在Python中编写了以下代码:
from __future__ import print_function
#from random import randint
import numpy as np
import tflearn
import pandas as pd
data,labels =tflearn.data_utils.load_csv('dummy_data.csv',target_column=-1,categorical_labels=False, n_classes=None)
print(data)
# Build neural network
net = tflearn.input_data(shape=[None, 4])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 3, activation='softmax')
net = tflearn.regression(net)
# Define model
model = tflearn.DNN(net)
#Start training (apply gradient descent algorithm)
data_to_array = np.asarray(data)
print(data_to_array.shape)
#data_to_array= data_to_array.reshape(6,9)
print(data_to_array.shape)
model.fit(data_to_array, labels, n_epoch=10, batch_size=3, show_metric=True)
我收到一个错误,上面写着:
ValueError:无法为Tensor'InputData/X:0'
提供形状(3,6)
的值,该Tensor具有形状'(?,4)
我猜这是因为我的输入数据有7列(0…6),但我希望输入层只将前四列作为输入,并预测数据中的最后3列作为输出。如何对此进行建模?如果数据采用numpy格式,则前4列将使用一个简单的切片:
data[:,0:4]
:
表示“所有行”,0:4
是一系列值0,1,2,3
,前4列
如果数据不是numpy格式,只需将其转换为numpy格式即可轻松切片
这里有一篇关于numpy切片的相关文章:但是,我在哪里输入最后3列(E、F、G),它们是我的输出?您当然也需要为标签声明一个输入。