numpy之one-hot
2017-10-10 11:11
351 查看
a = np.array([1, 0, 3])#数据列,one-hot编码之后非零的列 n_class = a.max() + 1 n_sample = a.shape[0] b = np.zeros((n_sample, n_class))#3个样本,4个类别 b[:, a] = 1#非零列赋值为1 b array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.]]) >>>
使用np.eye生成M*N矩阵,后面跟的数组说明1偏移的位置
values = np.array([1, 0, 3]) n_values = np.max(values) + 1 np.eye(n_values)[values] array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.]])
例如
np.eye(4,3)[np.array([1, 1, 1, 1])] array([[ 0., 1., 0.], [ 0., 1., 0.], [ 0., 1., 0.], [ 0., 1., 0.]])
values = np.array([1, 0, 3,1,2,1,2]) n_values = np.max(values) + 1 np.eye(n_values)[values] array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.]])
相关文章推荐
- 使用numpy生成one_hot
- 利用numpy 生成one_hot 数组
- 机器学习:数据预处理之独热编码(One-Hot)
- PyTorch——Tensor_把索引标签转换成one-hot标签表示
- Spark MLlib特征处理:OneHotEncoder OneHot编码 ---原理及实战
- Python—numpy模块下函数介绍(一)numpy.ones、empty等
- 多分类标签转换为one-hot向量
- 【自用】 Pandas 预处理 —— 补全和编码(one-hot)
- Tensorflow 的tf.one_hot()功能:dense to one hot
- labelencoding onehot
- one_hot的源码实现及其理解
- one artical on pci hotplug framework
- one-hot转为distributed representation表示
- one-hot使用体会
- one hot encoding
- one hot 编码及数据归一化
- pytorch: 如何优雅的将 int list 转成 one-hot形式
- tensorflow--tf.one_hot()函数示例
- Verilog中状态机编码方式的选择:二进制编码(Binary)、格雷码(Gray-code)编码、独热码(One-hot)编码
- One Hot Code---独热码