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 循环。
我正在尝试为大型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) 我的项目具有以下目录结构:
.
??? 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 …
python setup.py bdist_wheel
在使用或等效命令构建 Cython 模块后,我需要运行某些命令。这可能吗?
具体情况如下:\n我正在为 osx 开发 Cython 程序。它链接到.dylib
我的主目录中的某些文件,并且要构建程序,您必须运行
export DYLD_LIBRARY_PATH=/path/to/parent/dir\n
Run Code Online (Sandbox Code Playgroud)\n\n(这很麻烦)或包含一个指令来setup.py
运行
install_name_tool -change @executable_path/my.dylib /path/to/my.dylib\n
Run Code Online (Sandbox Code Playgroud)\n\n这基本上的作用是告诉二进制文件在哪里寻找dylib
. 这两种选择都不是理想的,但后者似乎更便携。现在我刚刚将这些说明添加到 的底部setup.py
,但这仍然不可移植:假设我按如下方式打包并上传我的包:
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% 确定这些命令的作用。但我知道如果随后在另一个项目中我运行 …
cython ×2
concurrency ×1
distutils ×1
dylib ×1
include-path ×1
mutex ×1
numpy ×1
pool ×1
portability ×1
python ×1
random ×1
rust ×1