【数据处理】OneHotEncoder编码

原创博文,转载请注明出处!

# OneHotEncoder编码

     OneHotEncoder编码称为“哑编码”或“独热编码”,是将表示分类的数据扩维度,由列向量扩展为稀疏矩阵


# OneHotEncoder例子

  1 # -*- coding: utf-8 -*-
  2 from sklearn.preprocessing import OneHotEncoder
  3 ohe = OneHotEncoder()
  4 ohe.fit([[1],[2],[3],[4],[7],[9]])
  5 ohe_transform = ohe.transform([[1],[2],[3],[4],[7],[9]]).toarray()
  6 print(ohe_transform)
  7 """
  8 [[ 1.  0.  0.  0.  0.  0.]
  9  [ 0.  1.  0.  0.  0.  0.]
 10  [ 0.  0.  1.  0.  0.  0.]
 11  [ 0.  0.  0.  1.  0.  0.]
 12  [ 0.  0.  0.  0.  1.  0.]
 13  [ 0.  0.  0.  0.  0.  1.]]
 14 """
原文地址:https://www.cnblogs.com/wanglei5205/p/8746914.html