如何解决对分类数据进行热编码时,如何处理丢失的数据?
我正在尝试对包含缺失值'N'的2D numpy数组应用一种热编码。我有以下代码,可以在不丢失任何值的情况下很好地工作。
import numpy as np
X = [['A','G'],['N','C'],['T','A']]
X = np.array(X)
print('Shape of data before one hot encoding: ',X.shape)
classes = np.array(['A','C','G','T'])
X = np.searchsorted(classes,X)
eye = np.eye(classes.shape[0])
onehotlabels = np.concatenate([eye[i] for i in X.T],axis=1)
print('Shape of data after pre-processing: ',onehotlabels.shape)
print(onehotlabels)
Output:
Shape of data before one hot encoding: (3,2)
Shape of data after pre-processing: (3,8)
[[1. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 1. 0. 1. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]]
是否可以通过这种方法将“ N”替换为0 0 0 0?输出将如下所示:
[[1. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。