如何从带有向量列的DataFrame创建Tensorflow数据集? [英] How to create a tensorflow dataset from a DataFrame with vector columns?

查看:103
本文介绍了如何从带有向量列的DataFrame创建Tensorflow数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在csv文件train.csv中有一些火车数据,格式如下:

So I have some train data in a csv file train.csv with the following format:

x;y;type
[1,2,3];[2,3,4];A
[2,7,9];[0,1,2];B

此文件使用以下内容解析为pd.DataFrame:

This file is parsed as a pd.DataFrame with the following:

CSV_COLUMN_NAMES = ['x', 'y', 'type']
train = pd.read_csv("train.csv", names=CSV_COLUMN_NAMES, header=0, delimiter=";")
train['x'] = train['x'].apply(literal_eval)
train['y'] = train['y'].apply(literal_eval)

到目前为止,一切都很好.应用了literal_eval函数,因此将xy视为数组.下一步是创建具有以下内容的DataSet:

So far so good. The literal_eval function is applied so x and y are treated as array. The next step is to create a DataSet with the following:

features, labels = train, train.pop('type')
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

这是它的坏处:(它溢出了以下错误:

And here is where it breaks :( It spills the following errors:

TypeError: Expected binary or unicode string, got [1, 2, 3]

为什么要使用二进制或unicode字符串?不允许使用向量要素列吗?还是我做错了什么?请给我一些启示

Why is binary or unicode string expected? Are vector feature columns not allowed? Or am I doing something wrong? Please shed me some light

推荐答案

TF可以从数据帧自动创建张量,只要它只有一种数据类型,在这种情况下,它似乎具有不同的数据类型.

TF can automatically create a tensor from a data frame as long as it has only one data type, in this case it seems to have different data types.

没有literal_eval的代码似乎可以正常工作,因为每个功能都是字符串,而不是混合类型:

Without literal_eval the code seems to work, as each of the features are string and not of mixed type:

train = pd.read_csv("train.csv", names=CSV_COLUMN_NAMES, header=0, delimiter=",")

Features,labels = train,train.pop('type')

dataset = tf.data.Dataset.from_tensor_slices((dict(Features), labels))
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()

with tf.Session() as sess:
  sess.run(iterator.initializer)
  print(sess.run(next_element))
  print(sess.run(next_element))

输出:

({'y': b'[2, 3, 4]', 'x': b'[1, 2, 3]'}, b'A')
({'y': b'[0, 1, 2]', 'x': b'[2, 7, 9]'}, b'B')

基于此解决方案:(

Based on this solution: (How to convert a Numpy 2D array with object dtype to a regular 2D array of floats ) if we convert the mixed object type to same (with np.vstack), it works.

train['x'] = train['x'].apply(literal_eval)
train['y'] = train['y'].apply(literal_eval)

Features,labels = train,train.pop('type')
dataset = tf.data.Dataset.from_tensor_slices(((np.vstack(Features['x']),    np.vstack(Features['y'])), labels))

iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()

with tf.Session() as sess:
   sess.run(iterator.initializer)
   print(sess.run(next_element))
   print(sess.run(next_element))

输出:

((array([1, 2, 3]), array([2, 3, 4])), b'A')
((array([2, 7, 9]), array([0, 1, 2])), b'B')

这篇关于如何从带有向量列的DataFrame创建Tensorflow数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆