小编eth*_*oks的帖子

为给定的 2D 概率数组沿轴向量化 `numpy.random.choice`

Numpy 具有该random.choice功能,可让您从分类分布中进行采样。你会如何在一个轴上重复这个?为了说明我的意思,这是我当前的代码:

categorical_distributions = np.array([
    [.1, .3, .6],
    [.2, .4, .4],
])
_, n = categorical_distributions.shape
np.array([np.random.choice(n, p=row)
          for row in categorical_distributions])
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想消除 for 循环。

python random numpy vectorization

8
推荐指数
1
解决办法
3717
查看次数

为什么互斥锁不解锁?

我正在尝试为大型Obj类型实现全局对象池。这是代码POOL

static mut POOL: Option<Mutex<Vec<Obj>>> = None;
static INIT: Once = ONCE_INIT;

pub struct Obj;
Run Code Online (Sandbox Code Playgroud)

这是我访问和锁定的方式POOL

fn get_pool<'a>() -> MutexGuard<'a, Vec<Obj>> {
    unsafe {
        match POOL {
            Some(ref mutex) => mutex.lock().unwrap(),
            None            => {
                INIT.call_once(|| {
                    POOL = Some(Mutex::new(vec![]));
                });
                get_pool()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是导致问题的代码:

impl Drop for Obj {
    fn drop(&mut self) {
        println!("dropping.");
        println!("hangs here...");
        get_pool().push(Obj {});
    }
}

impl Obj {
    pub fn new() -> Obj {
        println!("initializing");
        get_pool().pop().unwrap_or(Obj {}) …
Run Code Online (Sandbox Code Playgroud)

concurrency mutex pool rust

5
推荐指数
1
解决办法
1875
查看次数

如何在Cython中指定`.pxd`文件的路径

我的项目具有以下目录结构:

.
??? Makefile
??? pxd
??? pyx
?   ??? Landscaping.pyx
?   ??? Shrubbing.pxd
?   ??? Shrubbing.pyx
??? setup.py
Run Code Online (Sandbox Code Playgroud)

但是,如果我移到Shrubbing.pxd其他任何地方,例如pxd/,我将收到以下错误:

Error compiling Cython file:
------------------------------------------------------------
...
import pyx.Shrubbing
cimport Shrubbing
       ^
------------------------------------------------------------

pyx/Landscaping.pyx:2:8: 'Shrubbing.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
import pyx.Shrubbing
cimport Shrubbing

cdef Shrubbing.Shrubbery sh
    ^
------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为setup.py我有:

from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(ext_modules=cythonize([
    Extension(
        'pyx.Landscaping',
        sources=["pyx/Landscaping.pyx"],
        include_dirs=['pxd']), # <-- HERE
    Extension('pyx.Shrubbing', sources=["pyx/Shrubbing.pyx"])
]))
Run Code Online (Sandbox Code Playgroud)

明确指定的新目录Shrubbing.pxd …

cython include-path

5
推荐指数
1
解决办法
2719
查看次数

分发 python 包时处理 dylib

python setup.py bdist_wheel在使用或等效命令构建 Cython 模块后,我需要运行某些命令。这可能吗?

\n\n

具体情况如下:\n我正在为 osx 开发 Cython 程序。它链接到.dylib我的主目录中的某些文件,并且要构建程序,您必须运行

\n\n
export DYLD_LIBRARY_PATH=/path/to/parent/dir\n
Run Code Online (Sandbox Code Playgroud)\n\n

(这很麻烦)或包含一个指令来setup.py运行

\n\n
install_name_tool -change @executable_path/my.dylib /path/to/my.dylib\n
Run Code Online (Sandbox Code Playgroud)\n\n

这基本上的作用是告诉二进制文件在哪里寻找dylib. 这两种选择都不是理想的,但后者似乎更便携。现在我刚刚将这些说明添加到 的底部setup.py,但这仍然不可移植:假设我按如下方式打包并上传我的包:

\n\n
python setup.py sdist  # I think this collects source files into a .tar\npython setup.py bdist_wheel  # I think this builds a binary. This is where I assume the post-compilation stuff should happen.\ntwine upload dist/*   # This puts stuff up on PyPi.\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我已经阅读了文档和一些教程,但我承认我仍然不能 100% 确定这些命令的作用。但我知道如果随后在另一个项目中我运行 …

portability distutils dylib cython

5
推荐指数
1
解决办法
1880
查看次数