one_hot 类别变量中n个不同类别转换为n个变量
dummy variable 在某一设定的参考准则下,对n个不同的类别,转换为n-1个变量
pandas 将标签转化为独热编码
pd.get_dummies(df_NMF[‘cluster‘]).head(20)
tensorflow 将标签转化为独热编码
from keras.utils import to_categorical
encoded=to_categorical(df_NMF[‘cluster‘])
机器学习包的独热编码使用
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
data = [‘cold‘, ‘cold‘, ‘warm‘, ‘cold‘, ‘hot‘, ‘hot‘, ‘warm‘, ‘cold‘, ‘warm‘, ‘hot‘]
values = np.array(data)
print(values)
# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
print(integer_encoded)
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print(onehot_encoded)
# invert first example
inverted = label_encoder.inverse_transform([np.argmax(onehot_encoded[0, :])])
print(inverted)
原文:https://www.cnblogs.com/raisok/p/12659617.html