Jam*_*ood 191 python numpy machine-learning one-hot-encoding numpy-ndarray
假设我有一个ndy阵列
a = array([1,0,3])
Run Code Online (Sandbox Code Playgroud)
我想将其编码为2d 1-hot阵列
b = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]])
Run Code Online (Sandbox Code Playgroud)
有快速的方法吗?比仅仅循环a
设置元素更快b
.
YXD*_*YXD 342
您的数组a
定义输出数组中非零元素的列.您还需要定义行,然后使用花式索引:
>>> a = np.array([1, 0, 3])
>>> b = np.zeros((a.size, a.max()+1))
>>> b[np.arange(a.size),a] = 1
>>> b
array([[ 0., 1., 0., 0.],
[ 1., 0., 0., 0.],
[ 0., 0., 0., 1.]])
Run Code Online (Sandbox Code Playgroud)
K3-*_*rnc 149
>>> values = [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.]])
Run Code Online (Sandbox Code Playgroud)
小智 27
这是我觉得有用的东西:
def one_hot(a, num_classes):
return np.squeeze(np.eye(num_classes)[a.reshape(-1)])
Run Code Online (Sandbox Code Playgroud)
这里num_classes
代表你拥有的课程数量.因此,如果你有一个a
形状为(10000,)的向量,这个函数将它转换为(10000,C).注意,它a
是零索引的,one_hot(np.array([0, 1]), 2)
即将给出[[1, 0], [0, 1]]
.
我相信你究竟想拥有什么.
PS:来源是序列模型 - deeplearning.ai
Jod*_*odo 26
如果您使用keras,则有一个内置实用程序:
from keras.utils.np_utils import to_categorical
categorical_labels = to_categorical(int_labels, num_classes=3)
Run Code Online (Sandbox Code Playgroud)
Fra*_*urt 25
你可以使用 sklearn.preprocessing.LabelBinarizer
:
例:
import sklearn.preprocessing
a = [1,0,3]
label_binarizer = sklearn.preprocessing.LabelBinarizer()
label_binarizer.fit(range(max(a)+1))
b = label_binarizer.transform(a)
print('{0}'.format(b))
Run Code Online (Sandbox Code Playgroud)
输出:
[[0 1 0 0]
[1 0 0 0]
[0 0 0 1]]
Run Code Online (Sandbox Code Playgroud)
除此之外,您可以初始化sklearn.preprocessing.LabelBinarizer()
以使输出transform
稀疏.
Kar*_*rma 13
numpy.eye(类的大小)[要转换的向量]
小智 6
您可以使用以下代码转换为 one-hot 向量:
让 x 是具有单个列的普通类向量,其中类为 0 到某个数字:
import numpy as np
np.eye(x.max()+1)[x]
Run Code Online (Sandbox Code Playgroud)
如果 0 不是一个类;然后删除+1。
对于 1-hot-encoding
one_hot_encode=pandas.get_dummies(array)
Run Code Online (Sandbox Code Playgroud)
享受编码
这是将一维矢量转换为一维二维热阵列的函数。
#!/usr/bin/env python
import numpy as np
def convertToOneHot(vector, num_classes=None):
"""
Converts an input 1-D vector of integers into an output
2-D array of one-hot vectors, where an i'th input value
of j will set a '1' in the i'th row, j'th column of the
output array.
Example:
v = np.array((1, 0, 4))
one_hot_v = convertToOneHot(v)
print one_hot_v
[[0 1 0 0 0]
[1 0 0 0 0]
[0 0 0 0 1]]
"""
assert isinstance(vector, np.ndarray)
assert len(vector) > 0
if num_classes is None:
num_classes = np.max(vector)+1
else:
assert num_classes > 0
assert num_classes >= np.max(vector)
result = np.zeros(shape=(len(vector), num_classes))
result[np.arange(len(vector)), vector] = 1
return result.astype(int)
Run Code Online (Sandbox Code Playgroud)
以下是一些用法示例:
>>> a = np.array([1, 0, 3])
>>> convertToOneHot(a)
array([[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1]])
>>> convertToOneHot(a, num_classes=10)
array([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]])
Run Code Online (Sandbox Code Playgroud)