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表格行作为前几行的标签


猜你喜欢
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01