Home > front end >  I´m trying to implement: np.maximum.outer in Python 3x but I´m getting this error: NotImplementedErr
I´m trying to implement: np.maximum.outer in Python 3x but I´m getting this error: NotImplementedErr

Time:11-05

I have a matrix like this:

RCA = pd.DataFrame(
  data=[
    (1,0,0,0),
    (1,1,1,0),
    (0,0,1,0),
    (0,1,0,1),
    (1,0,1,0)],
    columns=['ct1','ct2','ct3','ct4'],
    index=['ind_1','ind_2','ind_3','ind_4','ind_5'])

I´m trying to calculate:

norms = RCA.sum()
norm = np.maximum.outer(norms, norms)

And I´m getting this error:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-9-4fd04a55ad8c> in <module>
      4 
      5 norms = RCA.sum()
----> 6 norm = np.maximum.outer(norms, norms)
      7 proximity = RCA.T.dot(RCA).div(norm)
      8 

~/opt/anaconda3/envs/py37/lib/python3.7/site-packages/pandas/core/series.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
    746             return None
    747         else:
--> 748             return construct_return(result)
    749 
    750     def __array__(self, dtype=None) -> np.ndarray:

~/opt/anaconda3/envs/py37/lib/python3.7/site-packages/pandas/core/series.py in construct_return(result)
    735                 if method == "outer":
    736                     # GH#27198
--> 737                     raise NotImplementedError
    738                 return result
    739             return self._constructor(result, index=index, name=name, copy=False)

NotImplementedError: 

This works perfect in Python 2.7, but I need to run it in Python 3.x

I need to find a way around this issue. Thanks a lot.

CodePudding user response:

In [181]: RCA
Out[181]: 
       ct1  ct2  ct3  ct4
ind_1    1    0    0    0
ind_2    1    1    1    0
ind_3    0    0    1    0
ind_4    0    1    0    1
ind_5    1    0    1    0
In [182]: norms = RCA.sum()
In [183]: norms
Out[183]: 
ct1    3
ct2    2
ct3    3
ct4    1
dtype: int64
In [184]: np.maximum.outer(norms,norms)
Traceback (most recent call last):
  File "<ipython-input-184-d24a173874f6>", line 1, in <module>
    np.maximum.outer(norms,norms)
  File "/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py", line 2032, in __array_ufunc__
    return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/pandas/core/arraylike.py", line 381, in array_ufunc
    result = reconstruct(result)
  File "/usr/local/lib/python3.8/dist-packages/pandas/core/arraylike.py", line 334, in reconstruct
    raise NotImplementedError
NotImplementedError

Sometimes passing a dataframe (or Series) to a numpy function works ok, but apparently here we need to explicitly use the array values:

In [185]: norms.values
Out[185]: array([3, 2, 3, 1])
In [186]: np.maximum.outer(norms.values,norms.values)
Out[186]: 
array([[3, 3, 3, 3],
       [3, 2, 3, 2],
       [3, 3, 3, 3],
       [3, 2, 3, 1]])

Actually looking at the traceback, apparently pandas adapts ufunc to its own uses. np.maximum(norms,norms) works, but apparently pandas has not adapted the outer method. [186] is pure numpy, returning an array.

Plain np.maximum returns a Series:

In [192]: np.maximum(norms,norms)
Out[192]: 
ct1    3
ct2    2
ct3    3
ct4    1
dtype: int64

outer returns a 2d array, which in pandas terms would be a dataframe, not a Series. That could explain why pandas does not implement outer.

  • Related