【发布时间】:2018-10-30 01:35:44
【问题描述】:
我想对机器学习方法的字符串输入字母进行编码。假设我的火车数据是这样的:
score text
1 show photos
1 show my photos
2 who are you?
目前我正在做这样的事情:
for index, row in train_set.iterrows():
list2 = []
list2 = list(row.text.lower())
for n, key in enumerate(list2):
if key in dictionary:
list2[n] = dictionary[key]
else:
dictionary[key] = i
list2[n] = i
i += 1
train_set.set_value(index,'text', list2)
作为这个示例数据的结果,我得到:
score text
1 [0, 1, 2, 3, 4, 5, 1, 2, 6, 2, 0]
1 [0, 1, 2, 3, 4, 7, 8, 4, 5, 1, 2, 6, 2, 0]
2 [3, 1, 2, 4, 10, 13, 9, 4, 8, 2, 19, 21]
正如您所知道的,例如对于神经网络,使用此值不是一种正确的方法,因此在我看来,在这种情况下,一种热编码将是最佳解决方案。我想知道在text 数据帧的text 列和train_set 数据帧的text 列中转换这些值的最有效方法是什么,看起来像test_set 但显然没有预期的第一列价值观。我认为在这两种情况下,在使用一种热编码后我应该具有相同大小的列,并且相同的索引和行应该对应于 test_set 和 train_set 数据帧中的相同字符。我希望你明白我的意思。如果没有,请告诉我。我将尝试以更清晰的方式解释它。有什么想法我该怎么做?
【问题讨论】:
-
好吧,如果您正在使用一种热编码,那么这可能会有所帮助stackoverflow.com/questions/43618245/…
标签: python python-3.x machine-learning scikit-learn one-hot-encoding