相关疑难解决方法(0)

numpy:数组中唯一值的最有效频率计数

numpy/中scipy,是否有一种有效的方法来获取数组中唯一值的频率计数?

这些方面的东西:

x = array( [1,1,1,2,2,2,5,25,1,1] )
y = freq_count( x )
print y

>> [[1, 5], [2,3], [5,1], [25,1]]
Run Code Online (Sandbox Code Playgroud)

(对你来说,R用户在那里,我基本上都在寻找这个table()功能)

python arrays performance numpy

210
推荐指数
10
解决办法
24万
查看次数

Python相当于MATLAB的"ismember"函数

在尝试优化代码的许多尝试之后,似乎最后一个资源是尝试使用多个核运行下面的代码.我不知道如何转换/重新构建我的代码,以便使用多个内核可以更快地运行.如果我能获得指导以实现最终目标,我将不胜感激.最终目标是能够尽快为阵列A和B运行此代码,其中每个阵列包含大约700,000个元素.这是使用小数组的代码.700k元素数组被注释掉了.

import numpy as np

def ismember(a,b):
    for i in a:
        index = np.where(b==i)[0]
        if index.size == 0:
            yield 0
        else:
            yield index


def f(A, gen_obj):
    my_array = np.arange(len(A))
    for i in my_array:
        my_array[i] = gen_obj.next()
    return my_array


#A = np.arange(700000)
#B = np.arange(700000)
A = np.array([3,4,4,3,6])
B = np.array([2,5,2,6,3])

gen_obj = ismember(A,B)

f(A, gen_obj)

print 'done'
# if we print f(A, gen_obj) the output will be: [4 0 0 4 3]
# notice that the output array needs to …
Run Code Online (Sandbox Code Playgroud)

python optimization matlab numpy

20
推荐指数
2
解决办法
2万
查看次数

标签 统计

numpy ×2

python ×2

arrays ×1

matlab ×1

optimization ×1

performance ×1