小编ram*_*ler的帖子

Python - SystemError:在PyObject调用中没有错误的NULL结果

故事:我正在尝试从C到Python的接口,以便为现有的Python代码使用更快的C计算速度.我已经取得了一些成功,也通过NumPy数组 - 但现在似乎有一个问题,我无法解决它.这是代码:

#define FORMAT_VALUE_T  "d"
char format_buffer[32];

typedef struct
    {
        PyObject_HEAD
        PyArrayObject *invmat;
        unsigned order;
        value_t weight, *buffer;
    } Det;

    typedef double value_t;

    typedef struct
    {
        PyObject_HEAD
        Det *det;
        value_t *row, *covs, ratio, star;
    } DetAppendMove;

    static int append_init(DetAppendMove *self, PyObject *args, PyObject *kwds)
    {
        value_t star, *temp;
        PyArrayObject *row, *col;
        PyObject *result = Py_BuildValue("(i)",1);
        Det *dete;

        snprintf(format_buffer, sizeof(format_buffer), "%s%s", "O!O!O!", FORMAT_VALUE_T);
        if (PyArg_ParseTuple(args, format_buffer, &DetType, &dete, &PyArray_Type, &row, &PyArray_Type, &col, &star))
        {   
            self->det = dete;
            temp = (value_t*)self->det->buffer; …
Run Code Online (Sandbox Code Playgroud)

c python arrays numpy extending

6
推荐指数
1
解决办法
4822
查看次数

遍历HDF5文件/树并返回后继续?

我有一个 HDF5 文件,其中包含某种树结构:

/a_1
/a_1/b_1
/a_1/b_1/data
/a_1
/a_1/b_2
/a_1/b_2/data
/a_2
/a_2/b_1
/a_2/b_1/data
/a_2
/a_2/b_2
/a_2/b_2/data
Run Code Online (Sandbox Code Playgroud)

这里/a_X/b_X有一些组,我们可以假设数据集data 包含某种数值数据。最好的提取方法是什么data?当然,我会尝试:

def extract(name, node):
    if isinstance(node, hdf.Dataset):
        return node[...]
    return None

with h5py.File(some_file) as f:
    f.visititems(extract)
Run Code Online (Sandbox Code Playgroud)

但这会在None第一次返回其他内容后停止。当然,可以构建一个全局对象并附加,但我想知道是否有某种“最佳实践”?

python tree numpy h5py

2
推荐指数
1
解决办法
2630
查看次数

在numpy中numpy.histogram /随机数的奇怪行为?

我在Python中偶然发现了随机数的一些特殊行为,特别是我使用模块numpy.random.

请考虑以下表达式:

n = 50
N = 1000
np.histogram(np.sum(np.random.randint(0, 2, size=(n, N)), axis=0), bins=n+1)[0]
Run Code Online (Sandbox Code Playgroud)

在大的限制中,N我期望二项分布(对于感兴趣的读者,这模拟Ehrenfest模型)和大n的正态分布.然而,典型的输出看起来像这样:

阵列([
1,0,0,1,0,2,0,1,0,15,0,
12,0,18,0,39,0,64,0,62,0,109,
0,97 ,0,107,0,114,0,102,0,92,0,
55,0,46,0,35,0,10,0,9,0,4,
0,0,0,3,0 ,1,1
]]

根据上面的陈述,我无法解释直方图中零点的出现 - 我在这里遗漏了一些明显的东西吗?

python random statistics numpy

0
推荐指数
1
解决办法
95
查看次数

标签 统计

numpy ×3

python ×3

arrays ×1

c ×1

extending ×1

h5py ×1

random ×1

statistics ×1

tree ×1