- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想编写一个对 ndarray(任意维度)进行操作的函数。
我发现的 cython 示例如下:
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def c_fill_na_inplace_f64(np.ndarray[np.int64_t, cast=False, mode="c"] X, np.int64_t fillval):
cdef size_t N = X.size
cdef np.int64_t* = &X[0]
但是,我怀疑对于不同维度的数组我需要不同的索引:
cdef np.int64_t* = &X[0]
cdef np.int64_t* = &X[0,0]
cdef np.int64_t* = &X[0,0,0]
是否有更好的方法来获取数据。
就此而言,如何编写一个接受任意维度的 ndarray 的函数? np.ndarray[ndim=1...
假设未提供 ndim
。
(注意。由于 const bug,我无法使用 MemoryView)。
最佳答案
也许最困难的部分是获得正确的签名。我将使用通用的 python-object +reshape(-1)
,它将数组重新解释为一维数组而不进行复制。
这是一个有点草率的例子。它至少适用于连续数组,对于其他数组则更复杂,也是因为 C 代码必须意识到它,并且仅传递指针 &X[0]
是不够的:
%%cython
cimport numpy as np
def print_nd(object X):
cdef np.ndarray[np.int64_t] arr=X.reshape(-1)
cdef np.int64_t *ptr=&arr[0]
print("addr", <np.uint64_t>(ptr))
print("ptr1:", ptr[0])
这里是一个测试(针对 2 维数组):
%%cython
cimport numpy as np
def print_2d(np.ndarray[np.int64_t, ndim=2] X):
cdef np.int64_t *ptr=&X[0,0]
print("addr", <np.uint64_t>(ptr))
print("ptr1:", ptr[0])
>>> print_nd(a)
addr 94635612809360
ptr1: 1
>>> print_2d(a)
addr 94635612809360
ptr1: 1
如您所见,这两种情况下都是相同的地址。
关于python - 如何在 cython 中获取指向 ndarray 数据(任意维度)开头的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48232319/
我想使用批处理从文件夹中读取图像。但是解码后,当我使用tf.train.batch时可能会出现一些问题。这是代码。 def get_batch(image, label, batch_size, ca
我正在使用 tf.unsorted_segment_sum TensorFlow 的方法,当我作为数据给出的张量只有一行时,它工作正常。例如: tf.unsorted_segment_sum(tf.c
我想创建一个正则表达式来检查有效维度JavaScript 中的长度 x 宽度 x 高度。 例如90.49 x 34.93 x 40.64 我打算使用的示例代码: var dimensionRegex
ViewPager 是否必须是 Activity 布局中唯一存在的对象?我正在尝试实现这样的东西: 我应该在什么地方有一个大的寻呼机在顶部滚动(我有)和一个较小的画廊在它下面滚动。这只向我显示寻
据我所知,(维度、维度属性和事实)差异的最佳示例如下所示: 维度 - 产品、帐户、客户 维度属性 - ProductName、ProductNumber、CustomerName、CustomerNu
我是 Numpy 的新手,正在尝试理解什么是维度的基本问题, 我尝试了以下命令并试图理解为什么最后两个数组的 ndim 相同? >>> a= array([1,2,3]) >>> a.ndim 1 >
我对 MDX 比较陌生,正在尝试完成我认为应该很容易的事情,但我还没有找到任何解决方案。 我有一个销售立方体,其中一个衡量标准是利润,它可以是负数也可以是正数。我想得到一个有效的正利润总和的度量,即只
在大多数情况下,维度内层次结构的每个级别代表不同的概念(即国家->地区->城市、年->月->日),这很简单,可以在多维数据集中使用。 我感兴趣的是可变深度层次结构,它往往由相同概念的实例组成,即计算机
我正在尝试创建一个方法来总结潜在的项目并从数组返回该总和。以下是一些预期的示例输入: arraySum(new int[10]); // 10 arraySum(new int[2][5]); //
我正在尝试初始化一个二维数组(我创建的类对象),但我仍然遇到相同的运行时错误: Exception in thread "main" java.lang.NullPoointerException
(我是一名学生,这是我第一次发帖,所以请放轻松。)我想创建一个将二维数组作为参数的函数,并且在该数组中,我想要一个变量,稍后我想在代码中对其进行修改。这是最接近我想要的例子的东西: int size;
我想我可能会问一个虚拟问题,但我对 Android 编程还是个新手,而且我无法(尽管我付出了所有努力)在 Google 上找到我的答案。 问题是,我正在尝试使用 2D 图形开发一个小游戏。我希望我的“
如何使用 Crossfilter 过滤一系列日期?当我知道该时间段之间存在事实记录时,以下内容不起作用。 Var myDimension = CrossFilterObj.dimension(func
我正在启动另一个应用程序并设置其主要 HWND 位置和大小。我正在使用 STARTUPINFO指定窗口尺寸的标志,但看起来只有在新进程使用 CW_USEDEFAULT 时才会遵循这些尺寸在其 Crea
我正在尝试使用 Keras 构建我的第一个神经网络。我的经验为零,我似乎无法弄清楚为什么我的维度不对。我无法从他们的文档中弄清楚这个错误在提示什么,甚至是什么层导致了它。 我的模型接受一个 32 字节
我有一个水平导航栏,我的 a 元素没有扩展到父 li 元素的宽度和高度。 如何修改我的 CSS,使 a 元素与外部/父级 li 元素一样宽和高?
如何只更改需要 Dimension 对象的组件的宽度或高度?目前我是这样做的: jbutton.setPreferredSize(new Dimension(button.getPreferredSi
哪些 OLAP 工具支持动态、动态地创建维度或层次结构? 例如,层次结构将成员定义为:“前 5 名”、“前 6-10 名”、“其他”... 计算成员是通常的答案,我正在寻找不同的东西。计算器的问题。成
我使用 1 个 div 元素为我的网站制作 .background 。它的高度将是 100%。为了实现这一点,我使用 jQuery 尺寸实用程序。 用这个脚本来获取高度 $('.background
MultiArray与使用 std::vector 创建多数组相比,在 boost 中有很多优势。但是,我对 BOOST 中的 MultiArray 感到不舒服的一件事是创建一个可以轻松更改其大小的多
我是一名优秀的程序员,十分优秀!