小编dig*_*ngo的帖子

结合itertools和多处理?

我有一个256x256x256Numpy数组,其中每个元素都是一个矩阵.我需要对这些矩阵中的每一个进行一些计算,并且我想使用该multiprocessing模块来加快速度.

这些计算的结果必须存储在256x256x256与原始数组相同的数组中,以便[i,j,k]原始数组中元素的矩阵结果必须放在[i,j,k]新数组的元素中.

为此,我想制作一个可以伪方式写入的列表,[array[i,j,k], (i, j, k)]并将其传递给一个"多处理"的函数.假设这matrices是从原始数组中提取的所有矩阵的列表,并且myfunc是执行计算的函数,代码看起来有点像这样:

import multiprocessing
import numpy as np
from itertools import izip

def myfunc(finput):
    # Do some calculations...
    ...

    # ... and return the result and the index:
    return (result, finput[1])

# Make indices:
inds = np.rollaxis(np.indices((256, 256, 256)), 0, 4).reshape(-1, 3)

# Make function input from the matrices and the indices:
finput = izip(matrices, inds)

pool = multiprocessing.Pool()
async_results …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing python-itertools

12
推荐指数
1
解决办法
5385
查看次数

对3D阵列进行高效迭代?

我正在使用Python和Numpy进行一些数据分析.

我有一个大的3D矩阵(NxNxN),其中每个单元格也是一个矩阵,这次是3x3矩阵.调用矩阵data,它看起来像这样:

data[N,N,N,3,3]  
Run Code Online (Sandbox Code Playgroud)

我需要找到所有3x3矩阵的特征值,为此我使用Numpy的eigvals例程,但需要很长时间才能完成.现在我几乎这样做:

for i in range(N):
    for j in range(N):
        for k in range(N):
            a = np.linalg.eigvals(data[i,j,k,:,:])
Run Code Online (Sandbox Code Playgroud)

对于N = 256,这需要大约一个小时.关于如何提高效率的任何想法?

非常感谢任何建议!

python numpy

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

peewee:反向引用外键时,获取单个模型而不是SelectQuery

我在向后引用外键方面有些挣扎peewee。考虑以下代码:

import peewee as pw

db = pw.SqliteDatabase(':memory:')

class Parent(pw.Model):
    name = pw.CharField()

    class Meta:
        database = db


class Child(pw.Model):
    name = pw.CharField()
    parent = pw.ForeignKeyField(Parent, related_name="kid")

    class Meta:
        database = db

db.create_tables([Parent, Child])

bob = Parent.create(name="Bob")
alice = Child.create(name="Alice", parent=bob)
Run Code Online (Sandbox Code Playgroud)

可以与Bob的孩子接触bob.kid,这可以给我一个机会SelectQuery。但是,根据设计,我知道任何一个Parent都只能有一个Child。然后可以使用访问该孩子bob.kid[0]

我希望能够通过简单地调用而不是Child从a 访问。无需进一步修改类就可以实现吗?Parentbob.kidbob.kid[0]Parent

python foreign-keys peewee

3
推荐指数
1
解决办法
864
查看次数

Peewee、SQLite 和线程

我正在开发一个线程应用程序,其中一个线程将提供Queue要修改的对象,然后许多其他线程将从队列中读取,进行修改并保存更改。

该应用程序不需要很多并发,所以我想坚持使用 SQLite 数据库。这是一个说明该应用程序的小示例:

import queue
import threading
import peewee as pw

db = pw.SqliteDatabase('test.db', threadlocals=True)

class Container(pw.Model):
    contents = pw.CharField(default="spam")

    class Meta:
        database = db


class FeederThread(threading.Thread):

    def __init__(self, input_queue):
        super().__init__()

        self.q = input_queue

    def run(self):
        containers = Container.select()

        for container in containers:
            self.q.put(container)


class ReaderThread(threading.Thread):

    def __init__(self, input_queue):
        super().__init__()

        self.q = input_queue

    def run(self):
        while True:
            item = self.q.get()

            with db.execution_context() as ctx:
                # Get a new connection to the container object:
                container = Container.get(id=item.id)
                container.contents …
Run Code Online (Sandbox Code Playgroud)

python sqlite multithreading peewee

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