问题描述:
当代码编辑如下时,运行中总会报出module‘keras.utils' has no attribute 'to_categorical'的错误
import keras labels= keras.utils.to_categorical(labels, classs) #classs:标签中元素种类
解决方案一:
from tensorflow.keras.utils import to_categorical
labels= to_categorical(labels,classs)
解决方案二:
from keras.utils import np_utils
labels= np_utils.to_categorical(labels,classs)
解决方案三:
from sklearn.preprocessing import OneHotEncoder #sklearn包中的独热编码
labels=OneHotEncoder().fit_transform(np.array(labels).reshape(-1,1)).toarray()