赞
踩
import numpy as np from keras.utils import to_categorical data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 7] data = array(data) print(data) # [1 2 3 4 5 6 7 8 9 7] #有普通np数组转换为one-hot one_hots = to_categorical(data) print(one_hots) # [[ 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] # [ 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] # [ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] # [ 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] # [ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] # [ 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] # [ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] # [ 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] # [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] # [ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]] # 由one-hot转换为普通np数组 data = [argmax(one_hot)for one_hot in one_hots] print(data) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 7]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。