csv table row as label for previous several rows(CSV表格行作为前几行的标签)
本文介绍了CSV表格行作为前几行的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关于TensorFlow的问题。 我有CSV数据,如附加的图像,我想要映射它: 绿色行-是前5行的标签。 是否可以在map函数(Dataet.map())中执行此操作? 如何做到这一点?
推荐答案
尝试tf.data.Dataset.window:
import tensorflow as tf
import pandas as pd
d = {'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'B': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'C': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'D': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'E': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'F': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'G': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'H': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
df = pd.DataFrame(data=d)
def redefine_data(windowed_ds):
data, labels = [], []
for window in windowed_ds:
data.append(tf.convert_to_tensor([w for w in window.take(5)]))
labels.append(next(iter(window.skip(5).take(1))))
return tf.data.Dataset.from_tensor_slices((data, labels))
ds = tf.data.Dataset.from_tensor_slices((df.values)).window(6, shift=3, stride=1, drop_remainder=True)
ds = redefine_data(ds)
for data, label in ds:
print(data, label)
tf.Tensor(
[[1 1 1 1 1 1 1 1]
[2 2 2 2 2 2 2 2]
[3 3 3 3 3 3 3 3]
[4 4 4 4 4 4 4 4]
[5 5 5 5 5 5 5 5]], shape=(5, 8), dtype=int64) tf.Tensor([6 6 6 6 6 6 6 6], shape=(8,), dtype=int64)
tf.Tensor(
[[4 4 4 4 4 4 4 4]
[5 5 5 5 5 5 5 5]
[6 6 6 6 6 6 6 6]
[7 7 7 7 7 7 7 7]
[8 8 8 8 8 8 8 8]], shape=(5, 8), dtype=int64) tf.Tensor([9 9 9 9 9 9 9 9], shape=(8,), dtype=int64)
tf.Tensor(
[[ 7 7 7 7 7 7 7 7]
[ 8 8 8 8 8 8 8 8]
[ 9 9 9 9 9 9 9 9]
[10 10 10 10 10 10 10 10]
[11 11 11 11 11 11 11 11]], shape=(5, 8), dtype=int64) tf.Tensor([12 12 12 12 12 12 12 12], shape=(8,), dtype=int64)
这篇关于CSV表格行作为前几行的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:CSV表格行作为前几行的标签
基础教程推荐
猜你喜欢
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
