看起来像单个列对numpy结构化和记录数组进行排序比在类似的独立数组上进行排序要慢得多:
In [111]: a = np.random.rand(1e4)
In [112]: b = np.random.rand(1e4)
In [113]: rec = np.rec.fromarrays([a,b])
In [114]: timeit rec.argsort(order='f0')
100 loops, best of 3: 18.8 ms per loop
In [115]: timeit a.argsort()
1000 loops, best of 3: 891 µs per loop
Run Code Online (Sandbox Code Playgroud)
使用结构化数组有一个微小的改进,但它不是戏剧性的:
In [120]: struct = np.empty(len(a),dtype=[('a','f8'),('b','f8')])
In [121]: struct['a'] = a
In [122]: struct['b'] = b
In [124]: timeit struct.argsort(order='a')
100 loops, best of 3: 15.8 ms per loop
Run Code Online (Sandbox Code Playgroud)
这表明从argsort创建索引数组然后使用它来重新排序各个数组可能会更快.这是可以的,除了我希望处理非常大的数组,并希望尽可能避免复制数据.有没有一种更有效的方法来做到这一点,我错过了?
我正在尝试使用OpenMP加速用Cython编写的一段简单代码.这是一个双循环,对于输入数组中的每个位置,在每个参考点添加一个数量.这是代码的主要部分:
cimport cython
import numpy as np
cimport numpy as np
cimport openmp
DTYPE = np.double
ctypedef np.double_t DTYPE_t
cdef extern from "math.h" nogil :
DTYPE_t sqrt(DTYPE_t)
@cython.cdivision(True)
@cython.boundscheck(False)
def summation(np.ndarray[DTYPE_t,ndim=2] pos, np.ndarray[DTYPE_t,ndim=1] weights,
np.ndarray[DTYPE_t, ndim=2] points, int num_threads = 0):
from cython.parallel cimport prange, parallel, threadid
if num_threads <= 0 :
num_threads = openmp.omp_get_num_procs()
if num_threads > openmp.omp_get_num_procs() :
num_threads = openmp.omp_get_num_procs()
openmp.omp_set_num_threads(num_threads)
cdef unsigned int nips = len(points)
cdef np.ndarray[DTYPE_t, ndim=1] sum_array = np.zeros(nips, dtype = np.float64)
cdef …Run Code Online (Sandbox Code Playgroud) 我正在使用scipy.sparse.csr_matrix如下构造稀疏向量:
csr_matrix((values, (np.zeros(len(indices)), indices)), shape = (1, max_index))
Run Code Online (Sandbox Code Playgroud)
这适用于我的大部分数据,但偶尔我会得到一个ValueError: could not convert integer scalar.
这再现了问题:
In [145]: inds
Out[145]:
array([ 827969148, 996833913, 1968345558, 898183169, 1811744124,
2101454109, 133039182, 898183170, 919293479, 133039089])
In [146]: vals
Out[146]:
array([ 1., 1., 1., 1., 1., 2., 1., 1., 1., 1.])
In [147]: max_index
Out[147]:
2337713000
In [143]: csr_matrix((vals, (np.zeros(10), inds)), shape = (1, max_index+1))
...
996 fn = _sparsetools.csr_sum_duplicates
997 M,N = self._swap(self.shape)
--> 998 fn(M, N, self.indptr, self.indices, self.data)
999 …Run Code Online (Sandbox Code Playgroud) 我正在使用reveal.js:我想将全屏图像显示为背景,一旦我移动到另一张幻灯片,我就想模糊或调暗它.看看在Reveal.js中更改背景图像样式,我在我的css中尝试了这个:
.html.blur .backgrounds {
-webkit-filter: blur(5px);
-moz-filter: blur(10px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
}
Run Code Online (Sandbox Code Playgroud)
然后在我的降价文件中我做了一个html评论
.slide: data-background="figs/background.svg" data-background-size="contain" data-state="blur"
Run Code Online (Sandbox Code Playgroud)
这会在里面创建一个<section>标签data-state="blur".然而,背景并没有变得模糊 - 我错过了什么?