【问题标题】:Using Pandas Dataframe in TensorFlow - X and Y values在 TensorFlow 中使用 Pandas Dataframe - X 和 Y 值
【发布时间】: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


【解决方案1】:

您需要使用数据框吗? 尝试预先将它们转换为数组:

array = data.values
x = array[:,0:3]
y = array[:,4]

但您还应该查看 keras 以获取 this kind of work

【讨论】:

  • 当我尝试使用数组时,我得到:'numpy.ndarray' 对象没有属性'index'
  • 您收到该错误是因为“索引”是列表的属性,而不是 numpy 数组的属性。您可能希望使用 np.where() 来实现您的方法
  • 然后我得到:'tuple' object has no attribute 'copy'
【解决方案2】:

我无法重现您提到的错误。我正在使用 TF 1.13.1。这是我运行您的代码时遇到的错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [Labels must <= n_classes - 1]

所以,我只是将 1,2,3 更改为 0,1,2。然后程序运行没有错误。

data['target'] = data['target'].str.replace('Iris-setosa','0')
data['target'] = data['target'].str.replace('Iris-virginica','1')
data['target'] = data['target'].str.replace('Iris-versicolor','2')

您可以从这里获取教程中使用的 csv 文件:

http://download.tensorflow.org/data/iris_training.csv

http://download.tensorflow.org/data/iris_test.csv

我正在使用 Tensorflow '1.13.1'。该示例运行良好,尽管警告使用 tf.data。

https://nbviewer.jupyter.org/gist/yufengg/a6dff912ab48f7a273f5704ad9ab1311

使用 tf.data 的教程。此外,还解释了如何从 iris csv 数据创建特征列。

https://www.tensorflow.org/guide/premade_estimators

https://github.com/tensorflow/models/blob/master/samples/core/get_started/iris_data.py

【讨论】:

  • 感谢您提供所有这些信息,但这并不能完全帮助我解决我的问题:在 tf 中使用 pandas df。
  • 我在尝试了您的代码 sn-p 后更新了答案。请检查。
猜你喜欢
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 2021-10-04
  • 2021-10-05
相关资源
最近更新 更多