Tensorflow 如何将CSV文件中的单词(字符串)转换为适当的向量

嗨,我正在尝试在Tensorflow中制作一个小型分类器。我想从CSV文件中读取数据并用于我的训练阶段,问题是我的文件内容看起来像这样:

object,categorie
the blue balon,toy
a white plastic ship,toy
a big book,other
the wild cat,animal
a wet dolphin,animal…

所以我想读取这些句子,然后将它们转换为向量,以便在Tensorflow模型中使用。我阅读的所有信息都是关于数值数据的,但我不知道如何使用这样的数据。

官方网站的教程使用的是数值数据,到目前为止最好的选择是使用字典,但我认为应该有更好的选择。

另一个选择是制作我自己的方法,但可能会不够精确。

有人有任何想法吗?我可以怎么做?有没有替代我的方法,或者我如何在Tensorflow中处理单词?

对不起,如果我的英语不好。

编辑

尝试将句子转换为多维数组,但结果不理想,我估计糟糕的结果是由于一些语句短而另一些长,这影响了每个数组的最终可用空间,而这种可用空间影响了概率模型的结果。有什么建议吗?


回答:

幸运的是,使用pandas模块解决方案非常简单!

首先,让我们创建一个快速的.csv文件:

example.csv:

"object","category""the blue balloon","toy""a white plastic ship","toy""a big book","other""the wild cat","animal""a wet dolphin","animal"

现在我们可以编写我们的简单Python文件:

convert.py

import pandas as pddata = pd.read_csv("example.csv")print(data)data = data.join(pd.get_dummies(data["category"]))data = data.drop("category", axis=1)print(data)

最后,我们可以运行我们的文件并查看结果!

$ python convert.py                 object category0      the blue balloon      toy1  a white plastic ship      toy2            a big book    other3          the wild cat   animal4         a wet dolphin   animal                 object  animal  other  toy0      the blue balloon     0.0    0.0  1.01  a white plastic ship     0.0    0.0  1.02            a big book     0.0    1.0  0.03          the wild cat     1.0    0.0  0.04         a wet dolphin     1.0    0.0  0.0

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注