简体   繁体   中英

'numpy.ndarray' object has no attribute 'concatenate' error

I have written some simple code to iterate through a group of lists I am analyzing (from b1 to b20). To these lists, I want to check first which of them are empty. To those that are empty, I want to add the value 0. I want to add 0 to the empty lists, because I will later sum the values from different lists altogether and, as far as I understand, I cannot add together lists that are empty.

At the moment, I have the following code:

for z in np.arange(1,21):
    r=np.array([0])
    rate = eval('b' + str(z))
    print (z)
    if len(rate)==0:
        rate.concatenate(r)
        print (rate)
    else:
        print (rate)


order_x20=b16+c16+d16+h16+i16
order_x2020=b17+c17+d17+h17+i17
order_x2050=b15+c15+d15+h15+i15
order_x20100=b2+c2+d2+h2+i2
order_x20300=b20+c20+d20+h20+i20

Every time I run the code, I get the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-37-876cc7bddcdf> in <module>
   2200     print (z)
   2201     if len(rate)==0:
-> 2202         rate.concatenate(r)
   2203         print (rate)
   2204     else:

AttributeError: 'numpy.ndarray' object has no attribute 'concatenate'

Could someone please help me solve the issue? I don't really understand why I am getting this error, but I assume is due to the fact that I cannot use np.append() or np.concatenate() with the eval() function?

Docstring:
concatenate((a1, a2, ...), axis=0, out=None)
a1, a2, ... : sequence of array_like
    The arrays must have the same shape, except in the dimension
    corresponding to `axis` (the first, by default).

This is a function, not a method. It is called with np.concatenate .

The first argument is a tuple (or more generally sequence) of arrays (or array like). If called with np.concatenate(a1, a2) , the a2 will be interpreted as the axis parameter, which must be a simple number!

Don't use np.concatenate (or np.append ) as though it were a clone of list append . alist.append(r) is a method call, and acts in-place. The numpy functions are functions and don't act in-place. They return a new array. When used repeatedly in a loop they are much less efficient.

From your description, this sounds like a simple list comprehension problem:

In [14]: alist = [[1,2],[],[2,3],[],[],[4]]                                     
In [15]: newlist = [i if len(i) else [0] for i in alist]                        
In [16]: newlist                                                                
Out[16]: [[1, 2], [0], [2, 3], [0], [0], [4]]

Or written as a for loop:

In [20]: newlist = [] 
    ...: for i in alist: 
    ...:     if len(i)==0: 
    ...:         i = [0] 
    ...:     newlist.append(i) 

This list could be turned into an array with one (correct) np.concatenate call after:

In [22]: np.concatenate(newlist)                                                
Out[22]: array([1, 2, 0, 2, 3, 0, 0, 4])

To concatenate two numpy arrays, you have to write rate = np.concatenate((rate,r),axis=0/1) , depending upon how you want to concatenate the two arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM