keras仅支持对已经进行整数编码的数据进行一次热编码。您可以像这样手动对字符串进行整数编码:手动编码
使用scikit-learn# this integer encoding is purely based on position, you can do this in other waysinteger_mapping = {x: i for i,x in enumerate(pre)}vec = [integer_mapping[word] for word in pre]# vec is# [0, 1, 2, 3, 16, 5, 6, 22, 8, 22, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
from sklearn.preprocessing import LabelEnprerimport numpy as nppre = np.array(pre)label_enprer = LabelEnprer()vec = label_enprer.fit_transform(pre)# array([ 2, 6, 7, 9, 19, 1, 16, 0, 17, 0, 3, 10, 5, 21, 11, 18, 19,# 4, 22, 14, 13, 12, 0, 20, 8, 15])
您现在可以将其输入
keras.utils.to_categorical:
from keras.utils import to_categoricalto_categorical(vec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)