我试图加快答案在这里使用用Cython.我尝试编译代码(在执行此处cygwinccompiler.py解释的hack 之后),但是出错了.任何人都可以告诉我,如果这是我的代码的问题,或Cython的一些深奥的微妙?fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated
以下是我的代码.提前致谢:
import numpy as np
import scipy as sp
cimport numpy as np
cimport cython
cdef inline np.ndarray[np.int, ndim=1] fbincount(np.ndarray[np.int_t, ndim=1] x):
cdef int m = np.amax(x)+1
cdef int n = x.size
cdef unsigned int i
cdef np.ndarray[np.int_t, ndim=1] c = np.zeros(m, dtype=np.int)
for i in xrange(n):
c[<unsigned int>x[i]] += 1
return c
cdef packed struct Point:
np.float64_t f0, f1
@cython.boundscheck(False)
def sparsemaker(np.ndarray[np.float_t, ndim=2] …Run Code Online (Sandbox Code Playgroud) 我正在阅读Python内存管理,并希望减少我的应用程序的内存占用.有人建议,子程序在减轻问题方面会有很长的路要走; 但我无法概念化需要做什么.有人可以提供一个简单的例子来说明如何...
def my_function():
x = range(1000000)
y = copy.deepcopy(x)
del x
return y
@subprocess_witchcraft
def my_function_dispatcher(*args):
return my_function()
Run Code Online (Sandbox Code Playgroud)
...进入一个真正的子处理函数,不存储额外的"自由列表"?
这个"自由列表"概念是否也适用于python c-extensions?
编译器如何强制堆栈内存是连续的,是否会导致在程序运行时每次都移动内存,或者在运行之前是否在程序所需的堆栈上保留内存?
有时您必须在一个或多个大型Numpy阵列上执行许多中间操作.这很快就会导致MemoryErrors.在我迄今为止的研究中,你发现Pickling(Pickle,CPickle,Pytables等)并且gc.collect()是减轻这种情况的方法.我想知道在处理大量数据时是否还有其他有经验的程序员使用的技术(当然,除了删除策略/代码中的冗余).
另外,如果有一点我确定没有什么是免费的.使用其中一些技术,有什么权衡(即速度,稳健性等)?
我刚刚学习python OOP.在某些框架的源代码中,我遇到return super(...并想知道两者之间是否存在差异.
class a(object):
def foo(self):
print 'a'
class b(object):
def foo(self):
print 'b'
class A(a):
def foo(self):
super(A, self).foo()
class B(b):
def foo(self):
return super(B, self).foo()
>>> aie = A(); bee = B()
>>> aie.foo(); bee.foo()
a
b
Run Code Online (Sandbox Code Playgroud)
看起来和我一样.我知道如果你愿意,OOP会变得非常复杂,但是在我的学习中,我没有足够的资金来提出一个更复杂的例子.是否存在返回super与呼叫不同的情况super?
我正在努力学习nditer,以便加速我的应用程序.在这里,我尝试制作一个小型的重塑程序,它将采用20号阵列并将其重塑为5x4阵列:
myArray = np.arange(20)
def fi_by_fo_100(array):
offset = np.array([0, 4, 8, 12, 16])
it = np.nditer([offset, None],
flags=['reduce_ok'],
op_flags=[['readonly'],
['readwrite','allocate']],
op_axes=[None, [0,1,-1]],
itershape=(-1, 4, offset.size))
while not it.finished:
indices = np.arange(it[0],(it[0]+4), dtype=int)
info = array.take(indices)
'''Just for fun, we'll perform an operation on data.\
Let's shift it to 100'''
info = info + 81
it.operands[1][...]=info
it.iternext()
return it.operands[1]
test = fi_by_fo_100(myArray)
>>> test
array([[ 97, 98, 99, 100]])
Run Code Online (Sandbox Code Playgroud)
显然,该程序将每个结果重写为一行.所以我尝试使用nditer的索引功能,但仍然没有骰子.
flags=['reduce_ok','c_iter']- > it.operands[1][it.index][...]=info=
IndexError: index out of …
没问题:
>>> t = np.array([[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4],[5,5,5,5,5]])
>>> x = np.arange(5).reshape((-1,1)); y = np.arange(5)
>>> print (t[[x]],t[[y]])
Run Code Online (Sandbox Code Playgroud)
大问题:
>>> s = scipy.sparse.csr_matrix(t)
>>> print (s[[x]].toarray(),s[[y]].toarray())
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
: :
: :
ValueError: data, indices, and indptr should be rank 1
Run Code Online (Sandbox Code Playgroud)
s.toarray()[[x]]效果很好,但由于我的数组太大,使用稀疏矩阵打败了我的整个目的.我已经检查了与某些稀疏矩阵相关的属性和方法,以获取引用高级索引的任何内容,但没有骰子.有任何想法吗?
我在通过神经网络进行机器学习的一些概念方面遇到了麻烦.其中一个是反向传播.在权重更新方程中,
delta_w = a*(t - y)*g'(h)*x
Run Code Online (Sandbox Code Playgroud)
t是"目标输出",在监督学习的情况下,它将是您的类别标签或其他东西.但是,"目标输出"对于无监督学习会是什么?
有人可以提供一个例子,说明如何在无监督学习中使用BP,特别是对于分类的聚类?
提前致谢.
有没有办法......
>>> 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”参数的快速示例吗?
python ×8
numpy ×6
arrays ×1
c++ ×1
callstack ×1
cython ×1
iteration ×1
numexpr ×1
oop ×1
optimization ×1
python-2.7 ×1
scipy ×1
subprocess ×1
windows-7 ×1