tensorflow - 使用 tf.data 将字符串转换为 csv 中的浮点数组
问题描述
我有一个这样的csv:
kw_text,kw_text_weight
amazon google,0.5 0.5
google facebook microsoft,0.5 0.3 0.2
kw_text | kw_text_weight |
---|---|
亚马逊谷歌 | 0.5 0.5 |
谷歌 脸书 微软 | 0.5 0.3 0.2 |
我想将列转换text_weight
为tf.data
. 但我在 tensorflow 文档网站上一无所获。
解决方案
我相信这就是你想要的:
import pandas as pd
import tensorflow as tf
d = {"kw_text": [['amazon', 'google'], ['google', 'facebook', 'microsoft']],
"kw_text_weight": [['0.5', '0.5'], ['0.5', '0.3', '0.2']]}
df = pd.DataFrame(d)
# Convert string to float
for i in range(len(df.index)):
df['kw_text_weight'][i] = [float(s) for s in df['kw_text_weight'][i]]
# Build dataset
rt=tf.ragged.constant(df['kw_text_weight'].tolist())
kw_text_weight_data = tf.data.Dataset.from_tensor_slices(rt)
for feature_batch in kw_text_weight_data:
print(feature_batch)
输出:
tf.Tensor([0.5 0.5], shape=(2,), dtype=float32)
tf.Tensor([0.5 0.3 0.2], shape=(3,), dtype=float32)
推荐阅读
- c# - NET 核心 webapi:为什么我会通过这个特定的 API 请求得到 404?
- javascript - 如何在 javascript 中全局捕获 net::ERR_NAME_NOT_RESOLVED
- python - 函数调用堆栈:模型拟合的 train_function 错误
- sharepoint-2013 - 列表项附件的 SharePoint 事件接收器已更新
- python - Matplotlib - 从数据框中的列在条形图上添加值标签
- flutter - Dart Analyzer 已停止工作,但我的代码中没有看到任何错误
- stored-procedures - 从 java oracle rdb 7.4 版本调用存储过程时出现问题
- python - 如何在 PySimpleGui 中设置列
- huawei-mobile-services - 测试时App Bundle格式的App意外退出导致App被拒绝的解决方法
- python - 在 pyspark 中绘制一个简单的 df