gpt4 book ai didi

python - 在矩阵的一维中进行洗牌(有效)?

转载 作者:行者123 更新时间:2023-11-30 23:32:03 30 4
gpt4 key购买 nike

我试图编写一个函数,获取 2D 点矩阵和概率 p 并以概率 p 更改或交换每个点坐标

所以我问了一个question我试图使用二进制序列作为特定矩阵 swap_matrix=[[0,1],[1,0]] 的幂的数组来随机交换(以特定比例)给定的一组 2D 点的坐标。但是我意识到幂函数只接受整数值而不接受数组。洗牌是我对整个矩阵的理解,你不能指定特定的维度。

拥有这两个功能中的任何一个都可以。

例如:

swap(a=[[1,2],[2,3],[3,4],[3,5],[5,6]],b=[0,0,0,1,1])

应返回[[1,2],[2,3],[3,4],[5,3],[6,5]]

刚刚出现的想法,现在我正在编辑的是:

def swap(mat,K,N):
#where K/N is the proportion and K and N are natural numbers
#mat is a N*2 matrix that I am planning to randomly changes
#it coordinates of each row or keep it as it is
a=[[[0,1],[1,0]]]
b=[[[1,0],[0,1]]]
a=np.repeat(a,K,axis=0)
b=np.repeat(b,N-K,axis=0)
out=np.append(a,b,axis=0)
np.random.shuffle(out)
return np.multiply(mat,out.T)

我收到错误的地方是因为我不能只展平一次以使矩阵可乘!

我再次寻找一种有效的方法(在 Matlab 上下文中向量化)。

附注在我的特殊情况下,矩阵的形状为 (N,2) ,并且第二列全部为 1(如果有帮助的话)。

最佳答案

也许这足以满足您的目的。在快速测试中,它似乎比生硬的 for 循环方法快大约 13 倍(@Naji,发布“低效”代码有助于进行比较)。

根据 Jaime 的评论编辑了我的代码

def swap(a, b):
a = np.copy(a)
b = np.asarray(b, dtype=np.bool)
a[b] = a[b, ::-1] # equivalent to: a[b] = np.fliplr(a[b])
return a

# the following is faster, but modifies the original array
def swap_inplace(a, b):
b = np.asarray(b, dtype=np.bool)
a[b] = a[b, ::-1]


print swap(a=[[1,2],[2,3],[3,4],[3,5],[5,6]],b=[0,0,0,1,1])

输出:

[[1 2]
[2 3]
[3 4]
[5 3]
[6 5]]
<小时/>

编辑以包含更详细的计时

我想知道我是否可以使用 Cython 来加快速度,所以我进一步研究了效率:-) 我认为结果值得一提(因为效率是实际问题的一部分),但我确实表示歉意预付附加代码量。

首先是结果......“cython”函数显然是所有函数中最快的,比上面建议的 Numpy 解决方案还要快 10 倍。我提到的“钝循环方法”是由名为“loop”的函数给出的,但事实证明,还有更快的方法。我的纯 Python 解决方案仅比上面的矢量化 Numpy 代码慢 3 倍!另一件需要注意的事情是“swap_inplace”大多数时候只比“swap”快一点。此外,不同的随机矩阵 ab 的时间也会有所不同......所以现在您知道了:-)

function     | milisec | normalized
-------------+---------+-----------
loop | 184 | 10.
double_loop | 84 | 4.7
pure_python | 51 | 2.8
swap | 18 | 1
swap_inplace | 17 | 0.95
cython | 1.9 | 0.11

我使用的其余代码(看来我很认真地对待这种方式:P):

def loop(a, b):
a_c = np.copy(a)
for i in xrange(a.shape[0]):
if b[i]:
a_c[i,:] = a[i, ::-1]

def double_loop(a, b):
a_c = np.copy(a)
n, m = a_c.shape
for i in xrange(n):
if b[i]:
for j in xrange(m):
a_c[i, j] = a[i, m-j-1]
return a_c

from copy import copy
def pure_python(a, b):
a_c = copy(a)
n, m = len(a), len(a[0])
for i in xrange(n):
if b[i]:
for j in xrange(m):
a_c[i][j] = a[i][m-j-1]
return a_c

import pyximport; pyximport.install()
import testcy
def cython(a, b):
return testcy.swap(a, np.asarray(b, dtype=np.uint8))

def rand_bin_array(K, N):
arr = np.zeros(N, dtype=np.bool)
arr[:K] = 1
np.random.shuffle(arr)
return arr

N = 100000
a = np.random.randint(0, N, (N, 2))
b = rand_bin_array(0.33*N, N)

# before timing the pure python solution I first did:
a = a.tolist()
b = b.tolist()


######### In the file testcy.pyx #########

#cython: boundscheck=False
#cython: wraparound=False

import numpy as np
cimport numpy as np

def swap(np.ndarray[np.int_t, ndim=2] a, np.ndarray[np.uint8_t, ndim=1] b):
cdef np.ndarray[np.int_t, ndim=2] a_c
cdef int n, m, i, j
a_c = a.copy()
n = a_c.shape[0]
m = a_c.shape[1]
for i in range(n):
if b[i]:
for j in range(m):
a_c[i, j] = a[i, m-j-1]
return a_c

关于python - 在矩阵的一维中进行洗牌(有效)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19597910/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com