- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在通过像 __rsub__
这样的右手运算符传递 numpy 数组后,如何访问它的属性?
我用 python 写了一个非常简单的类,它定义了两个函数:
class test(object):
def __sub__(self, other):
return other
def __rsub__(self, other):
return other
基本上他们应该做同样的事情。左侧运算符 __sub__
按预期工作,但 numpy 数组似乎在右侧运算符上剥离了其属性
from skimage import data
from skimage.color import rgb2gray
lena = data.lena()
grayLena = rgb2gray(lena)
t = test()
## overloaded - operator
left_hand = t - grayLena
print left_hand
# Output:
#array([[ 0.60802863, 0.60802863, 0.60779059, ..., 0.64137412,
# 0.57998235, 0.46985725],
# [ 0.60802863, 0.60802863, 0.60779059, ..., 0.64137412,
# 0.57998235, 0.46985725],
# [ 0.60802863, 0.60802863, 0.60779059, ..., 0.64137412,
# 0.57998235, 0.46985725],
# ...,
# [ 0.13746353, 0.13746353, 0.16881412, ..., 0.37271804,
# 0.35559529, 0.34377725],
# [ 0.14617059, 0.14617059, 0.18730588, ..., 0.36788784,
# 0.37292549, 0.38467529],
# [ 0.14617059, 0.14617059, 0.18730588, ..., 0.36788784,
# 0.37292549, 0.38467529]])
right_hand = grayLena - t
print right_hand
# Output:
# array([[0.6080286274509803, 0.6080286274509803, 0.6077905882352941, ...,
# 0.6413741176470589, 0.5799823529411765, 0.4698572549019608],
# [0.6080286274509803, 0.6080286274509803, 0.6077905882352941, ...,
# 0.6413741176470589, 0.5799823529411765, 0.4698572549019608],
# [0.6080286274509803, 0.6080286274509803, 0.6077905882352941, ...,
# 0.6413741176470589, 0.5799823529411765, 0.4698572549019608],
# ...,
# [0.1374635294117647, 0.1374635294117647, 0.1688141176470588, ...,
# 0.3727180392156863, 0.35559529411764706, 0.34377725490196076],
# [0.1461705882352941, 0.1461705882352941, 0.18730588235294118, ...,
# 0.3678878431372549, 0.37292549019607846, 0.3846752941176471],
# [0.1461705882352941, 0.1461705882352941, 0.18730588235294118, ...,
# 0.3678878431372549, 0.37292549019607846, 0.3846752941176471]], dtype=object)
所以这两种操作的区别在于__rsub__
接收的是一个dtype=object的数组。如果我只设置这个数组的数据类型,一切都会正常进行。
但是,它仅适用于 __rsub__
之外的返回值。在我的 __rsub__
中,我得到的只是垃圾,我无法转换回来,也就是说,如果我这样做了
npArray = np.array(other, dtype=type(other))
我得到一个类型的一维数组(在我的例子中是 float )。但是由于某种原因,形状信息丢失了。有没有人这样做过或知道如何访问数组的原始属性(形状和类型)?
最佳答案
我不确定 ndarray
机制中的确切控制流是什么,但您的情况或多或少是清楚的:
ndarray
委托(delegate)给对象的 __rsub__
方法的不是整体减法操作,而是从数组中的每个项目中减去对象。显然,当它必须将操作委托(delegate)给对象的方法时,无论返回什么,返回类型都设置为 object
。您可以通过对代码稍作修改来检查它:
class test(object):
def __sub__(self, other):
return other
def __rsub__(self, other):
return other if other != 1 else 666
In [11]: t = test()
In [12]: t - np.arange(4)
Out[12]: array([0, 1, 2, 3])
In [13]: np.arange(4) - t
Out[13]: array([0, 666, 2, 3], dtype=object)
我不认为有一种简单的方法可以覆盖这种行为。你可以试试 making test
a subclass of ndarray
具有高 __array_priority__
并滥用一点 __array_wrap__
方法:
class test(np.ndarray):
__array_priority__ = 100
def __new__(cls):
obj = np.int32([1]).view(cls)
return obj
def __array_wrap__(self, arr, context) :
if context is not None :
ufunc = context[0]
args = context[1]
if ufunc == np.subtract :
if self is args[0] :
return args[1]
elif self is args[1] :
return args[0]
return arr
现在:
>>> t = test()
>>> np.arange(4) - t
array([0, 1, 2, 3])
>>> t - np.arange(4)
array([0, 1, 2, 3])
但是:
>>> np.arange(4) + t
test([1, 2, 3, 4])
>>> t + np.arange(4)
test([1, 2, 3, 4])
有点浪费,因为我们是在做把t
里面的1
加到数组中的每一个值,然后默默丢弃的操作,但是我想不出任何方法来覆盖它。
关于python - 参数类型在正常和反射运算符重载 (__sub__/__rsub__) 方面不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15340650/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!