为了账号安全,请及时绑定邮箱和手机立即绑定

Cython - 在 nogil 函数中索引 numpy 数组

Cython - 在 nogil 函数中索引 numpy 数组

慕的地8271018 2022-12-20 11:06:19
我正在尝试扩展 sklearn 中的Splitter类,它与 sklearn 的决策树类一起使用。更具体地说,我想feature_weights在新类中添加一个变量,这将通过根据特征权重按比例改变纯度计算来影响最佳分割点的确定。新类几乎是 sklearnBestSplitter类 ( https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_splitter.pyx ) 的精确副本,只有微小的变化。这是我到目前为止所拥有的:cdef class WeightedBestSplitter(WeightedBaseDenseSplitter):    cdef object feature_weights # new variable - 1D array of feature weights    def __reduce__(self):        # same as sklearn BestSplitter (basically)    # NEW METHOD    def set_weights(self, object feature_weights):         feature_weights = np.asfortranarray(feature_weights, dtype=DTYPE)        self.feature_weights = feature_weights      cdef int node_split(self, double impurity, SplitRecord* split,                        SIZE_t* n_constant_features) nogil except -1:        # .... same as sklearn BestSplitter ....        current_proxy_improvement = self.criterion.proxy_impurity_improvement()        current_proxy_improvement *= self.feature_weights[<int>(current.feature)]  # new line        # .... same as sklearn BestSplitter ....关于上面的一些注意事项:我正在使用object变量类型,np.asfortranarray因为这是变量X在其他地方定义和设置的方式,并且X像我试图索引一样被索引feature_weights。此外,每个文件custom.feature都有一个变量类型( https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_splitter.pxd)。SIZE_t_splitter.pxd该问题似乎是由self.feature_weights. 上面的代码抛出多个错误,但即使尝试引用类似的东西self.feature_weights[0]并将其设置为另一个变量也会抛出错误:Indexing Python object not allowed without gil我想知道我需要做什么才能索引self.feature_weights标量值并将其用作乘数。
查看完整描述

1 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

如果没有 GIL,你绝对不能索引一个通用的 Python 对象(正如你正在尝试做的那样)。您可以在没有 GIL 的情况下索引类型化的内存视图。

定义feature_weights

cdef double[:] feature_weights


查看完整回答
反对 回复 2022-12-20
  • 1 回答
  • 0 关注
  • 223 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号