嗨,我正在尝试在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