1

I get a weird error with following codes:

import numpy as np
w=np.random.rand(100,10)
x=np.random.rand(200,100)
y= np.random.rand(200,10)

def compute( x, y):
    numpy_x = np.array(x) 
    numpy_y = np.array(y)

    zeross = np.zeros(np.shape(x)[0])
    oness = np.ones(np.shape(x)[0])

    result= np.zeros((np.shape(x)[0],np.shape(y)[1]))
    for i in range(np.shape(x)[1]):
        for j in range(np.shape(y)[1]):
            result[i,j] = w[i,j]+np.max(zeross, np.subtract(oness,w[:,j].dot(np.transpose(numpy_x)))).dot(numpy_x[:,i]*(numpy_y[:,j]*-1))

    return result

print(compute(x,y))

Here x is a 100 by 100 matrix, y is a 100 by 5 matrix w is a 100 by 5 matrix The error that I'm getting is :

Test Failed: only integer scalar arrays can be converted to a scalar index

The compiler seems to indicate error is in library?

     37     return result
     38 
---> 39 print(compute(x,y))

2 frames
<ipython-input-36-8b5bccb531cc> in compute(x, y)
     33     for i in range(np.shape(x)[1]):
     34         for j in range(np.shape(y)[1]):
---> 35             result[i,j] = w[i,j]+np.max(zeross, np.subtract(oness,w[:,j].dot(np.transpose(numpy_x)))).dot(numpy_x[:,i]*(numpy_y[:,j]*-1))
     36 
     37     return result

/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial)
   2503     """
   2504     return _wrapreduction(a, np.maximum, 'max', axis, None, out, keepdims=keepdims,
-> 2505                           initial=initial)
   2506 
   2507 

/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     84                 return reduction(axis=axis, out=out, **passkwargs)
     85 
---> 86     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     87 
     88 

Please help

user42493
  • 813
  • 4
  • 14
  • 34
  • 1
    Possible duplicate of [TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array](https://stackoverflow.com/questions/50997928/typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-with-1d) – Guy Oct 23 '19 at 06:38
  • in the future, please don't include images of exceptions. paste the whole exception - also there's a middle part of the exception not shown in the image. – Ruzihm Oct 23 '19 at 06:57
  • Ok, I don't see where I put an array for an index of another array? – user42493 Oct 23 '19 at 07:03
  • 1
    @user42493 I'm not sure if this gives you the calculations you expect, but if you're trying to do an element-wise maximum between two arrays, then you should use `np.maximum` instead of `np.max`. The second argument of `np.max` is an `axis` parameter, so it is trying to convert the array you are giving it into indices. – Ruzihm Oct 23 '19 at 18:21

0 Answers0