ValueError:无法为Tensor' Placeholder_37:0'提供形状值(891,),它具有形状'(?,1)'

时间:2018-05-17 07:25:42

标签: python pandas tensorflow kaggle

    import pandas as pd
import tensorflow as tf

PREDICTORS = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]
TARGET = ["Survived"]

def load_file(path):
    data = pd.read_csv(path)
    data["Age"] = data["Age"].fillna(data["Age"].mean())
    data["Sex"] = data["Sex"].apply(lambda sex: 1 if sex == "male" else 0)

    data["Embarked"] = data["Embarked"].fillna("S")

    data.loc[data["Embarked"] == "S", "Embarked"] = 0
    data.loc[data["Embarked"] == "C", "Embarked"] = 1
    data.loc[data["Embarked"] == "Q", "Embarked"] = 2

    data["Fare"] = data["Fare"].fillna(data["Fare"].mean())

    return data

train, test = load_file("../input/train.csv"), load_file("../input/test.csv")
train.head()
test.head()

train_x_data = train.loc[:, ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]]
print(train_x_data.head())

test_x_data = test.loc[:, ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]]
print(test_x_data.head())

train_y_data = train.loc[:, "Survived"]
print(train_y_data.head())

X = tf.placeholder(tf.float32, shape=[None, 7])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([7, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.matmul(X, W) + b

cost = tf.reduce_mean(tf.square(hypothesis - Y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for step in range(2001):
    sess.run(train, feed_dict={X:train_x_data, Y:train_y_data})
你好 我是kaggle的初学者

尝试“泰坦尼克号教程”'在此网站(https://www.kaggle.com/c/titanic

我的问题是源代码的最后一个代码(sess.run(train,feed_dict = {X:train_x_data,Y:train_y_data}))< - 这部分!

计算机错误说我在feed_dict中使用形状时遇到问题 我使用[无,7]形状我可以在源代码中看到,但我没有遇到问题。

有人可以帮我解决问题吗?

0 个答案:

没有答案