Home javascript android java php c# ubuntu python c++ c ios TAGS

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are vali


Date : 2019-08-04 11:45:04 , By : Emre Turk

I am getting this error How can I solve this problem

def fprop(self, input_data, target_data):
    tmp = [(t, i) for i, t in enumerate(target_data)]
    z = zip(*tmp)  # unzipping trick !
    cost = np.sum(np.log(input_data[z]))
    if self.size_average:
        cost /= input_data.shape[1]

Error

cost = np.sum(np.log(input_data[z]))
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
TAGS : python
Answers :
By : hpaulj

You should tell us something about the code and data. Is it something you wrote or is someone else's? Is it new, or did it work before, may be in some other context? What are:

input_data, target_data

But let's make a reasonable guess, and explore the code step by step (this is something you should have done yourself):

In [485]: alist = [1,3,0,2]                                                                                  
In [486]: tmp = [(t,i) for i,t in enumerate(alist)]                                                          
In [487]: tmp                                                                                                
Out[487]: [(1, 0), (3, 1), (0, 2), (2, 3)]
In [488]: z = zip(*tmp)                                                                                      
In [489]: z                                                                                                  
Out[489]: <zip at 0x7ff381d50188>

In Python 3, zip produces a zip object, a generator. That clearly can't be an index for anything.

We can turn it into a list, which is what Python2 would have done:

In [490]: list(z)                                                                                            
Out[490]: [(1, 3, 0, 2), (0, 1, 2, 3)]

But that list of tuples is marginally good:

In [491]: x = np.arange(16).reshape(4,4)                                                                     
In [492]: x[_490]                                                                                            
/usr/local/bin/ipython3:1: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  #!/usr/bin/python3
Out[492]: array([ 4, 13,  2, 11])

If we turn that into a tuple of tuples, numpy works without the warning:

In [493]: x[tuple(_490)]                                                                                     
Out[493]: array([ 4, 13,  2, 11])

This is the same as using alist to pick one value per column:

In [494]: x[alist, np.arange(4)]                                                                             
Out[494]: array([ 4, 13,  2, 11])

In [495]: x                                                                                                  
Out[495]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

I should have expanded the zip with tuple initially:

In [502]: x[tuple(zip(*tmp))]                                                                                
Out[502]: array([ 4, 13,  2, 11])

Having worked through this, I now suspect this code originally worked for Python2.

- Technology - Yellow pages
» java
» php
» c#
» ubuntu
» python
» c++
» c
» ios
» jquery
» html
» sql
» r
» mysql
» linux
» access
» iphone
» css
» excel
» vb.net
» .net
» ruby
Privacy Policy - Copyrights Notice - Terms - Contact Us 2019 © tinytut
Licensed under cc by-sa 3.0 with attribution required.