使用tensorflow库处理数据集是否有效?我想操纵它们,比如删除列/行,编辑它们等。
答案 0 :(得分:0)
此示例代码可帮助您开始导入CSV,执行一些基本操作并在tensorflow中运行NN学习。您可以获得data here。
import numpy as np
import pandas as pd
import tensorflow as tf
# Read and manipulate data from CSV
df = pd.read_csv('df.csv')
df = df.dropna(how='any')
df = df.drop('DayOfWeek', axis=1)
df.Customers = df.Customers / 1000.0
df.CompetitionDistance = df.CompetitionDistance / 1000.0
df.Sales = df.Sales / 1000.0
# Parameters
features = 2
hidden = 3
learning_rate = 0.2
# Prepare input and output arrays
train_x = np.array(df[['CompetitionDistance', 'Customers']])
train_y = np.array(df[['Sales']]).reshape([-1])
# Build a simple TF graph
x = tf.placeholder(tf.float32, shape=[None, features], name='x')
y = tf.placeholder(tf.float32, shape=[None], name='y')
W = tf.get_variable(name='W', shape=[features, hidden])
b = tf.get_variable(name='b', shape=[hidden], initializer=tf.zeros_initializer)
z = tf.matmul(x, W) + b
predict = tf.reduce_sum(z, axis=1)
loss = tf.reduce_mean(tf.square(y - predict))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)
# Run the training
with tf.Session() as session:
session.run(tf.global_variables_initializer())
for i in xrange(101):
_, loss_value = session.run([optimizer, loss],
feed_dict={x: train_x, y: train_y})
if i % 10 == 0:
print "epoch=%03i, loss=%.5f" % (i, loss_value)