简体   繁体   English

我正在尝试实现:np.maximum.outer in Python 3x 但我收到此错误:NotImplementedError

[英]I´m trying to implement: np.maximum.outer in Python 3x but I´m getting this error: NotImplementedError

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这在 Python 2.7 中完美运行,但我需要在 Python 3.x 中运行它

I need to find a way around this issue.我需要找到解决这个问题的方法。 Thanks a lot.非常感谢。

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:有时将数据帧(或系列)传递给 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]])

Actually looking at the traceback, apparently pandas adapts ufunc to its own uses.实际上查看回溯,显然pandas ufunc适应其自身的用途。 np.maximum(norms,norms) works, but apparently pandas has not adapted the outer method. np.maximum(norms,norms)有效,但显然熊猫还没有适应outer方法。 [186] is pure numpy , returning an array. [186] 是纯numpy ,返回一个数组。

Plain np.maximum returns a Series:普通np.maximum返回一个系列:

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. outer返回一个二维数组,用pandas术语来说,它是一个数据框,而不是一个系列。 That could explain why pandas does not implement outer .这可以解释为什么 pandas 没有实现outer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用python实现strassen的算法,但出现错误 - I'm trying to implement strassen's algorithm using python and I'm getting a error 我正在尝试在 python 中制作一个石头剪刀布游戏,但我得到一个 unindent 与任何外部缩进杆错误不匹配 - I'm trying to make a rock paper scissors game in python, but am getting an unindent does not match any outer indent lever error 在 python 3.x 中使用 chatterbot 时出现错误 - I'm getting error when using the chatterbot in python 3.x 尝试使用Selenium 2与Python绑定,但我收到导入错误 - Trying to use Selenium 2 with Python bindings, but I'm getting an import error 我正在尝试使用此 python 代码创建 wordcloud,但不断出现错误 - I'm trying to create a wordcloud with this python code, but keeps getting error 我正在尝试在 python 中使用 HTML img 标签发送邮件,但出现以下错误 - I'm trying to use HTML img tag in python to send a mail, but I'm getting the following error 我正在尝试标准化我的属性,但由于 Python 找不到它而出现标准缩放器错误 - I'm trying to standardize my attribute but I'm getting standard scaler error because Python can not find it 我正在尝试实现一个极小极大算法来创建一个井字游戏机器人,但我遇到了递归错误 - I'm trying to implement a minimax algorithm to create a tic-tac-toe bot, but i'm getting a recursion error 我在 python 中遇到关键错误 - I'm getting Key error in python 我正在尝试用 python 制作游戏,首先是让角色移动,但我一直遇到同样的错误 - I'm trying to make a game in python, starting by getting the character moving, but i keep getting the same error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM