【发布时间】:2019-03-22 13:09:46
【问题描述】:
我正在尝试学习本教程:https://www.youtube.com/watch?v=G7oolm0jU8I&list=PLIivdWyY5sqJxnwJhe3etaK7utrBiPBQ2&index=3
但由于他正在使用旧的 tf 函数进行导入,并且他已经在导入一个经过大量修改的 .csv 文件,因此我尝试导入原始文件,使用 pandas 对其进行更改,然后在线性模型中使用它。
这就是我所做的:
filename = "iris.data"
data = pd.read_csv(filename, names=["feature1", "feature2", "feature3", "feature4", "target"])
data['target'] = data['target'].str.replace('Iris-setosa','1')
data['target'] = data['target'].str.replace('Iris-virginica','2')
data['target'] = data['target'].str.replace('Iris-versicolor','3')
data['target'] = pd.to_numeric(data['target'])
training_data: pd.DataFrame= data.loc[:120]
eval_data: pd.DataFrame = data.loc[120:150]
这给了我两个熊猫数据框。现在我尝试在 TF 中使用 training_data:
feature1 = tf.feature_column.numeric_column("feature1")
feature2 = tf.feature_column.numeric_column("feature2")
feature3 = tf.feature_column.numeric_column("feature3")
feature4 = tf.feature_column.numeric_column("feature4")
feat_cols = [feature1, feature2, feature3, feature4]
input_fn = tf.estimator.inputs.pandas_input_fn(
x=training_data[['feature1', 'feature2', 'feature3', 'feature4']],
y=training_data['target'],
batch_size=128,
num_epochs=1,
shuffle=True,
queue_capacity=1000,
num_threads=1,
target_column='targetx'
)
classifier = tf.estimator.LinearClassifier(
feature_columns=feat_cols,
n_classes=3,
model_dir="/tmp/iris_model")
classifier.train(input_fn=input_fn, steps=1000)
这给了我一个错误,因为 x 和 y 值显然是错误的,但我无法弄清楚它们的含义,因为关于它们的文档很短,甚至不存在。一些帖子指出 x 代表特征列,y 代表标签。但这对我没有帮助,因为来自熊猫我知道标签是列的名称,但什么是特征列,对我来说意味着相同?!
请有人详细说明 x 和 y 的含义。
这是错误:
TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'feature1': <tf.Tensor 'random_shuffle_queue_DequeueUpTo:6' shape=(?,) dtype=float64>, 'feature2': <tf.Tensor 'random_shuffle_queue_DequeueUpTo:7' shape=(?,) dtype=float64>, 'feature3': <tf.Tensor 'random_shuffle_queue_DequeueUpTo:8' shape=(?,) dtype=float64>, 'feature4': <tf.Tensor 'random_shuffle_queue_DequeueUpTo:9' shape=(?,) dtype=float64>, 'target': <tf.Tensor 'random_shuffle_queue_DequeueUpTo:10' shape=(?,) dtype=int64>}. Consider casting elements to a supported type.
【问题讨论】:
-
“这给了我一个错误”对任何人都没有帮助; 究竟是什么错误?请发布完整的错误跟踪。
标签: python pandas tensorflow machine-learning