gpt4 book ai didi

python - 扩展 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:10 31 4
gpt4 key购买 nike

我是 Python/numpy 的新手。

我正在尝试扩展 numpy.array 以赋予它一些功能,使其能够更好地表示图像(例如转换为灰度等)。

import numpy as np
import cv2
from support import *
import matplotlib.pyplot as plt

class Frame(np.array):
def __init__(self):
print "new frame"


f = Frame()

目前这给了我:

  File "o.py", line 6, in <module>
class Frame(np.array):
TypeError: Error when calling the metaclass bases
cannot create 'builtin_function_or_method' instances

我不明白为什么这是 Python 的问题?

最佳答案

您想子类化 np.ndarray,而不是 np.array,但这比在您的示例中换成另一个要复杂一些。可能值得看一下文档: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

关于python - 扩展 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23115233/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com