想用自己的数据进行独热编码,一开始没用官方的程序。
from sklearn import preprocessing
enc = preprocessing.OneHotEncoder()
想用这个函数的可以参考博主https://www.cnblogs.com/webRobot/p/8831069.html
我找的是GitHub里的一个程序https://github.com/lahneh/LSTM-Human-Activity-Recognition
可以参考下。
代码贴上:
def one_hot(y_, n_classes): #对输出结果进行独热编码
y_ =y_.reshape(len(y_))
return np.eye(n_classes)[np.array(y_, dtype=np.int32)]
那么问题来了:一直报错
AttributeError: 'DataFrame' object has no attribute 'reshape'
这是因为我的数据是DataFrame格式的,要将他转化成nparray格式才好用。
于是,我又找DataFrame转nparray的操作
df.values
参考:https://blog.csdn.net/qq_30163461/article/details/80080529
不知道为什么在我这里又不行。。。
然后我直接转了
label= np.array(label)
运行之后可行。
最后要注意,python是从0开始的,所以在给数据做标签时最好也从0开始,不然会报错说size不同,不能转化。