gpt4 book ai didi

python - tflearn to_categorical : Processing data from pandas. df.values:数组数组

转载 作者:行者123 更新时间:2023-12-04 03:10:24 25 4
gpt4 key购买 nike

labels = np.array([['positive'],['negative'],['negative'],['positive']])
# output from pandas is similar to the above
values = (labels=='positive').astype(np.int_)
to_categorical(values,2)

输出:

array([[ 1.,  1.],
[ 1., 1.],
[ 1., 1.],
[ 1., 1.]])

如果我删除包含每个元素的内部列表,它似乎工作得很好

labels = np.array([['positive'],['negative'],['negative'],['positive']])
values = (labels=='positive').astype(np.int_)
to_categorical(values.T[0],2)

输出:

array([[ 0.,  1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.]])

为什么会这样?我正在学习一些教程,但即使对于数组数组,它​​们似乎也获得了正确的输出。最近是否升级为这种行为?

我在 py362 上使用 tflearn (0.3.2)

最佳答案

查看to_categorical 的源代码:

def to_categorical(y, nb_classes):
""" to_categorical.

Convert class vector (integers from 0 to nb_classes)
to binary class matrix, for use with categorical_crossentropy.

Arguments:
y: `array`. Class vector to convert.
nb_classes: `int`. Total number of classes.

"""
y = np.asarray(y, dtype='int32')
if not nb_classes:
nb_classes = np.max(y)+1
Y = np.zeros((len(y), nb_classes))
Y[np.arange(len(y)),y] = 1.
return Y

核心部分是高级索引 Y[np.arange(len(y)),y] = 1,它处理输入向量y 作为结果数组中的列索引;所以 y 需要是一维数组才能正常工作,您通常会收到任意二维数组的广播错误:

例如:

to_categorical([[1,2,3],[2,3,4]], 2)

--------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () ----> 1 to_categorical([[1,2,3],[2,3,4]], 2)

c:\anaconda3\envs\tensorflow\lib\site-packages\tflearn\data_utils.py in to_categorical(y, nb_classes) 40 nb_classes = np.max(y)+1 41 Y = np.zeros((len(y), nb_classes)) ---> 42 Y[np.arange(len(y)),y] = 1. 43 return Y 44

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (2,3)

这两种方法都可以正常工作:

to_categorical(values.ravel(), 2)
array([[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.]])

to_categorical(values.squeeze(), 2)
array([[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.]])

to_categorical(values[:,0], 2)
array([[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.]])

关于python - tflearn to_categorical : Processing data from pandas. df.values:数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45838313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com