【问题标题】:I´m trying to implement: np.maximum.outer in Python 3x but I´m getting this error: NotImplementedError我正在尝试实现:Python 3x 中的 np.maximum.outer 但我收到此错误:NotImplementedError
【发布时间】:2021-11-04 23:21:26
【问题描述】:

我有一个这样的矩阵:

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'])

我正在计算:

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

我收到了这个错误:

---------------------------------------------------------------------------
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: 

这在 Python 2.7 中完美运行,但我需要在 Python 3.x 中运行它

我需要找到解决此问题的方法。非常感谢。

【问题讨论】:

    标签: python python-3.x numpy matrix


    【解决方案1】:
    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
    

    有时将数据帧(或系列)传递给 numpy 函数可以正常工作,但显然在这里我们需要显式使用数组值:

    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]])
    

    实际上看回溯,显然pandas 使ufunc 适应它自己的用途。 np.maximum(norms,norms) 有效,但显然 pandas 没有采用 outer 方法。 [186] 是纯numpy,返回一个数组。

    普通的np.maximum 返回一个系列:

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

    outer 返回一个二维数组,在pandas 术语中将是一个数据帧,而不是一个系列。这可以解释为什么 pandas 没有实现 outer

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多