在数组内连接数组-值错误:无法连接零维数组

时间:2020-05-17 01:36:51

标签: python arrays pandas numpy

我正在尝试将3个新列追加到现有的数据框中,这些新列应该对虚拟变量进行编码。

为此,我正在创建一个函数,以查看要“消隐”的数组,并为每个“ hit”将对应的值分配给新行。

import numpy as np
import pandas as pd

iriss = np.concatenate((np.array(['setosa']*50), np.array(['versicolor']*50), np.array(['virginica']*50)), axis = 0)

在这种情况下,我展示了Iris数据集的“物种”列,其中包含150种均匀分布的物种(每个物种50个单位)。

def one_hot_coding():
    one_hot_column = np.array([], dtype = 'int8')

    for one_hot in iriss:
        #my idea here is to find the 'hit = species' and to then for each 'hit' to assign to these
        # three different np.arrays the value of one or zero
        if one_hot == 'setosa':
            one_hot_setosa = np.append(one_hot_column, 1)
            one_hot_versicolor = np.append(one_hot_column, 0)
            one_hot_virginica = np.append(one_hot_column, 0)            
        elif one_hot == 'versicolor':
            one_hot_setosa = np.append(one_hot_column, 0)
            one_hot_versicolor = np.append(one_hot_column, 1)
            one_hot_virginica = np.append(one_hot_column, 0)
        else:
            one_hot_setosa = np.append(one_hot_column, 0)
            one_hot_versicolor = np.append(one_hot_column, 0)
            one_hot_virginica = np.append(one_hot_column, 1)

        one_hot_setosa = np.concatenate((one_hot_setosa), axis = 0)

        print(one_hot_setosa)
one_hot_coding()

结果讨论:

为了简化起见,我只会谈论one_hot_setosa: 当我在print上呼叫one_hot_setosa时,出现150行,其中前50行是[1],而后100行是[0]。

[1] [1] ...48 [0] [0] ...48 [0] [0] ... 48

从这里我看到的结果是,在名为one_hot_setosa的数组中有150个独立的数组。

当我尝试将它们全部连接起来以获得单个数组(即为容纳150个单位而创建的iriss数组)时,出现以下错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-107-6f70367ed6eb> in <module>
     24 
     25         print(one_hot_setosa)
---> 26 one_hot_coding()

<ipython-input-107-6f70367ed6eb> in one_hot_coding()
     21             one_hot_virginica = np.append(one_hot_column, 1)
     22 
---> 23         one_hot_setosa = np.concatenate((one_hot_setosa), axis = 0)
     24 
     25         print(one_hot_setosa)

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: zero-dimensional arrays cannot be concatenated

所以这个错误告诉我实际上我没有150个数组,或者更好的说一个数组.shape =(150,1)(这就是我想要的)。但是实际上我的数组什么都不包含吗?为什么会这样?

1 个答案:

答案 0 :(得分:0)

好的,我一直在努力,但是我设法解决了这个问题。

np.concatenate会将行添加到数组中。因此,我指定了要向one_hot_ flower 添加行。另外,正如@hpaulj所指出的,要考虑方括号也非常重要。由于我的数组是一维数组,因此我只需要向要连接的值添加一次方括号即可。

所有最终代码都看起来像这样:

import numpy as np
import pandas as pd

iriss = np.concatenate((np.array(['setosa']*50), np.array(['versicolor']*50), np.array(['virginica']*50)), axis = 0)

one_hot_setosa = np.array([])
one_hot_versicolor = np.array([])
one_hot_virginica = np.array([])

def one_hot_coding():
    global one_hot_setosa
    global one_hot_versicolor
    global one_hot_virginica

    for one_hot in iriss:
        if one_hot == 'setosa':
            one_hot_setosa = np.concatenate((one_hot_setosa, np.array([1])), axis = 0)
            one_hot_versicolor = np.concatenate((one_hot_versicolor, np.array([0])), axis = 0)
            one_hot_virginica = np.concatenate((one_hot_virginica, np.array([0])), axis = 0)            
        elif one_hot == 'versicolor':
            one_hot_setosa = np.concatenate((one_hot_setosa, np.array([0])), axis = 0)
            one_hot_versicolor = np.concatenate((one_hot_versicolor, np.array([1])), axis = 0)
            one_hot_virginica = np.concatenate((one_hot_virginica, np.array([0])), axis = 0)
        else:
            one_hot_setosa = np.concatenate((one_hot_setosa, np.array([0])), axis = 0)
            one_hot_versicolor = np.concatenate((one_hot_versicolor, np.array([0])), axis = 0)
            one_hot_virginica = np.concatenate((one_hot_virginica, np.array([1])), axis = 0)

        #one_hot_setosa = np.concatenate((one_hot_setosa), axis = 0)

    return one_hot_setosa, one_hot_versicolor, one_hot_virginica
one_hot_coding()
[Out]:
(array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
 array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
 array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]))
相关问题