有没有办法......
>>> x = np.array([0, 8, 10, 15, 50]).reshape((-1, 1)); ncols = 5
Run Code Online (Sandbox Code Playgroud)
......把它变成......
array([[ 0, 1, 2, 3, 4],
[ 8, 9, 10, 11, 12],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[50, 51, 52, 53, 54]])
Run Code Online (Sandbox Code Playgroud)
我能够用np.apply_along_axis... 做到这一点
>>> def myFunc(a, ncols):
return np.arange(a, (a+ncols))
>>> np.apply_along_axis(myFunc, axis=1, arr=x)
Run Code Online (Sandbox Code Playgroud)
和for循环......
>>> X = np.zeros((x.size,ncols))
>>> for a,b in izip(xrange(x.size),x):
X[a] = myFunc(b, ncols)
Run Code Online (Sandbox Code Playgroud)
但它们太慢了.有更快的方法吗?
提前致谢.
我一直在试验和尝试学习Numexpr包。关于如何使用它的示例充其量是稀疏的。有人可以给我一个关于如何使用“local_dict”和“global_dict”参数的快速示例吗?
>>> x = np.array([['a0', 'a1'],['b0','b1']])
>>> y = np.array([['x0', 'x1'],['y0','y1']])
>>> iterable = [np.outer(x[i],y[i]) for i in xrange(x.shape[0])]
>>> elbareti = np.asarray(iterable)
>>> elbareti
array([[[ 'a0'*'x0', 'a0'*'x1' ],
[ 'a1'*'x0', 'a1'*'x1' ]],
[[ 'b0'*'y0', 'b0'*'y1' ],
[ 'b1'*'y0', 'b1'*'y1' ]]])
Run Code Online (Sandbox Code Playgroud)
由于我打算使用大型阵列,是否有更多类似于numpy的版本?我觉得答案就在我的鼻子底下,我觉得它与之有关reduce,但是numpy的版本只适用于ufuncs,而不是函数.即使是暗示也会受到高度赞赏.
提前致谢.
我试图获得给定布尔值的向量的最大值.
随着Numpy:
>>> this = np.arange(10)
>>> this[~(this>=5)].max()
4
Run Code Online (Sandbox Code Playgroud)
但是与Theano:
>>> that = T.arange(10, dtype='int32')
>>> that[~(that>=5)].max().eval()
9
>>> that[~(that>=5).nonzero()].max().eval()
Traceback (most recent call last):
File "<pyshell#146>", line 1, in <module>
that[~(that>=5).nonzero()].max().eval()
AttributeError: 'TensorVariable' object has no attribute 'nonzero'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这是一个我错过的微妙的细微差别吗?
我正在尝试使用python的dis库来试验和理解性能.以下是我尝试过的实验,结果如下.
import dis
def myfunc1(dictionary):
t = tuple(dictionary.items())
return t
def myfunc2(dictionary, func=tuple):
t = func(dictionary.items())
return t
Run Code Online (Sandbox Code Playgroud)
4 0 LOAD_GLOBAL 0 (tuple)
3 LOAD_FAST 0 (dictionary)
6 LOAD_ATTR 1 (items)
9 CALL_FUNCTION 0
12 CALL_FUNCTION 1
15 STORE_FAST 1 (t)
5 18 LOAD_FAST 1 (t)
21 RETURN_VALUE
Run Code Online (Sandbox Code Playgroud)
4 0 LOAD_FAST 1 (func)
3 LOAD_FAST 0 (dictionary)
6 LOAD_ATTR 0 (items)
9 CALL_FUNCTION 0
12 CALL_FUNCTION 1
15 STORE_FAST 2 (t)
5 18 LOAD_FAST …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译一个在install(.pyd)上预编译的模块的快速扩展.下面是我正在尝试做的一个简单的例子.鉴于foo.pyd:
from foo.bar cimport Bar
cdef class Baz(Bar):
pass
Run Code Online (Sandbox Code Playgroud)
cdef class Baz(Bar):
def __init__(self, *a, **k):
...
Run Code Online (Sandbox Code Playgroud)
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
extensions = [Extension('baz', ['baz.pyx',], libraries=['foo.pyd',])]
setup(name='baz', ext_modules=cythonize(extensions))
Run Code Online (Sandbox Code Playgroud)
我已尝试过上述的许多变化,但无济于事.
我正在考虑使用Kivy开发我的第一个移动应用程序(主要是因为我不想学习任何新语言).此应用程序需要访问用户的音乐库文件和元数据.我正在浏览Kivy的API参考资料,找不到适合作为解决方案的任何内容.该音频模块给我的背景音乐或SFX功能的印象.
我的问题是,Kivy是否严格用于为应用程序创建GUI?如果没有,有人可以给我一个例子,说明如何使用Kivy/python访问移动设备上的用户音乐库吗?
提前致谢.
我一直在寻找通过一个C代码片段,当我遇到这行的汇编代码来:
char *buf = alloca(0x2000);
asm volatile("" :: "m" (buf));
Run Code Online (Sandbox Code Playgroud)
我不知道这意味着什么.在我的调查中,我了解到有许多不同类型的汇编语言(例如,MASM,NASM,GAS等),而在我(非常有限)的经验中,作者很少指定他们正在使用哪一种.
这条线意味着什么; 更重要的是,C开发人员(可能不精通大会)如何研究他们以这种方式遇到的汇编代码?
class C:
def __init__(self, **kwargs):
self.w = 'foo'
self.z = kwargs['z']
self.my_function(self.z)
def my_function(self, inp):
inp += '!!!'
input_args = {}
input_args['z'] = 'bar'
c = C(**input_args)
print c.z
Run Code Online (Sandbox Code Playgroud)
bar!!!
Run Code Online (Sandbox Code Playgroud)
bar
Run Code Online (Sandbox Code Playgroud)
你如何在init中调用类的方法?
BoxLayout(orientation='vertical')对比GridLayout(cols=1):
他们都做同样的事,不是吗?是否有理由选择其中一个?
python ×9
numpy ×4
kivy ×2
assembly ×1
c ×1
compilation ×1
constructor ×1
cython ×1
numexpr ×1
performance ×1
python-2.7 ×1
theano ×1