是否可以在非数组中获取非零元素的长度而不迭代数组或屏蔽数组.速度是计算长度的主要目标.
基本上,像len(array).where(array != 0).
如果它改变了答案,则每行将以零开头.数组用零填充在对角线上.
我试图围绕PEP3118的numpy实现.缓冲区访问究竟是如何在numpy中工作的.
>>> p = numpy.getbuffer(numpy.arange(10))
>>> p
<read-write buffer for 0x1003e5b10, size -1, offset 0 at 0x1016ab4b0>
>>> numpy.frombuffer(p)
array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,
1.48219694e-323, 1.97626258e-323, 2.47032823e-323,
2.96439388e-323, 3.45845952e-323, 3.95252517e-323,
4.44659081e-323])
Run Code Online (Sandbox Code Playgroud)
所以我得到了意想不到的回报.我希望看到一个包含0-9的10个元素的数组.我可以进入数组并进行读/写.
>>> j = numpy.frombuffer(p)
>>> j
array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,
1.48219694e-323, 1.97626258e-323, 2.47032823e-323,
2.96439388e-323, 3.45845952e-323, 3.95252517e-323,
4.44659081e-323])
>>> j += 1
>>> j
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
Run Code Online (Sandbox Code Playgroud)
所以看起来缓冲区正在初始化为全零,然后我可以写入.我期望的功能是能够使用getbuffer将数组(使用arange或asarray)直接构建到缓冲区.这不可能吗?
我正在使用多处理模块操作numpy数组,并且遇到了一些问题,尝试了我在这里运行的一些代码.具体来说,我从一个numpy数组创建一个ctypes数组,然后尝试将ctypes数组返回到一个numpy数组.这是代码:
shared_arr = multiprocessing.RawArray(_numpy_to_ctypes[array.dtype.type],array.size)
Run Code Online (Sandbox Code Playgroud)
我不需要任何类型的同步锁,所以我使用的是RawArray.基于输入数组的dtype从字典中提取ctypes数据类型.这非常有效.
shared_arr = numpy.ctypeslib.as_array(shared_arr.get_obj())
Run Code Online (Sandbox Code Playgroud)
在这里,我得到一个堆栈跟踪说明:
AttributeError: 'c_double_Array_16154769' object has no attribute 'get_obj'
Run Code Online (Sandbox Code Playgroud)
我也从这篇文章中尝试了以下内容,但得到了相同的错误.
def tonumpyarray(shared_arr):
return numpy.frombuffer(shared_arr.get_obj())
Run Code Online (Sandbox Code Playgroud)
我被困在运行python 2.6并且不知道这是否是问题,如果这是共享变量名称的问题(我试图尽可能低地保持内存使用并且我试图不复制numpy数组和ctypes因为我刚学习python的这个组件,所以在内存中的数组).
建议?
我正在分割大型ctype数组并并行处理它们。我收到下面的错误,并相信它是因为数组的一个片段要先完成另一个片段的处理。我尝试使用process.join()来等待第一组进程,但是那没有用。有想法吗?
Exception RuntimeError: RuntimeError('cannot join current thread',) in <Finalize object, dead> ignored
Run Code Online (Sandbox Code Playgroud)
使用方法:
....
with closing(multiprocessing.Pool(initializer=init(array))) as p:
del array #Since the array is now stored in a shared array destroy the array ref for memory reasons
step = y // cores
if step != 0:
jobs =[]
for i in range (0, y, step):
process = p.Process(target=stretch, args= (shared_arr,slice(i, i+step)),kwargs=options)
jobs.append(process)
process.start()
for j in jobs:
j.join()
del jobs
del process
Run Code Online (Sandbox Code Playgroud)
更新:
#Create an ctypes array
array = ArrayConvert.SharedMemArray(array) …Run Code Online (Sandbox Code Playgroud) 目前,我使用argparse来解析参数并将标志存储为布尔选项.然后我检查看哪个标志设置为true并执行该功能.Argparse解析一个输入文件,该文件被打开并作为参数传递给被调用函数.
所以:
parser.add_argument('input_data', action='store', help='some help')
parser.add_argument('outputname', action='store',default=None, help='some help')
parser.add_argument('--flag','-f', action='store_true', dest='flag', default=False, help='help!')
Run Code Online (Sandbox Code Playgroud)
我必须打开input_data,在调用flag函数之前从中读取一些信息.目前实现如下:
if args.flag == True:
array_out = flag(array_read_from_input)
if args.outputname == None:
name = 'Flag.tif'
Run Code Online (Sandbox Code Playgroud)
可以将argparse子类化以使action关键字调用函数.
是否可以解析input_data选项,执行一些处理,然后调用flag函数,而不必为每个参数嵌套if循环,例如,通过继承argparse的action参数?
我在交互式Matplotlib图中使用了一个光标小部件,如下所示:
cursor = Cursor(ax1, useblit=True, color='red', linewidth=1)
cid = fig.canvas.mpl_connect('button_press_event', on_click)
Run Code Online (Sandbox Code Playgroud)
效果很好.该on_click函数采用x,y点击位置并进行一些补充绘图.基本的东西.
当我激活缩放工具时,我也捕获了点击.是否有必要将激活和取消激活关键笔划绑定到RectangleSelector示例的窗口小部件,或者是否存在知道工具栏项状态的方法?
RectangleSelector示例中选择器打开/关闭的示例:
def toggle_selector(event):
if event.key in ['Q','q'] and toggle_selector.RS.active:
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
toggle_selector.RS.set_active(True)
Run Code Online (Sandbox Code Playgroud) 是否可以为github问题执行部分单词匹配查询:
例如,如果问题标题是"缺少多边形",是否可以使用类似于以下语法的查询进行查询:
poly* in:title
Run Code Online (Sandbox Code Playgroud) 请看以下示例:
from foo import Bar
b = Bar
print(b.__slots__)
#Stack trace, no attribute __slots__
Run Code Online (Sandbox Code Playgroud)
然后假设我们要添加一个__slots__属性,因为Bar将创建很多(和很多)实例.
from foo import Bar
CBar = Bar
CBar.__slots__ = ["one", "two", "three"]
b = CBar
print(b.__slots__)
#Success
b.four = 0
print(b.four)
#Prints 0....hmmm....
Run Code Online (Sandbox Code Playgroud)
是否可以从模块导入类并__slots__动态添加属性?
我一直在研究 Hartley 和 Zisserman 多视图几何文本,并实现了用于计算基本矩阵的黄金标准算法。这需要使用 Levenberg-Marquardt 解决非线性最小化问题。
我用 实现了这个scipy.optimize.least_squares,但性能比使用lsqnonlin. 在这两种情况下,我都没有提供雅可比矩阵或雅可比矩阵稀疏性的掩码。
关于计算时间,这适用于可用的 scipy 求解器范围。我想知道是否存在与 matlab 具有相似性能(数值和速度)的替代方案,或者是否需要转移到包装的、编译的求解器?
编辑代码请求注释。我试图限制插入的代码总量。
MATLAB:
P2GS = lsqnonlin(@(h)ReprojErrGS(corres1,PF1,corres2,h),PF2);
function REGS = ReprojErrGS(corres1,PF1,corres2,PF2)
%Find estimated 3D point by Triangulation method
XwEst = TriangulationGS(corres1,PF1,corres2,PF2);
%Reprojection Back to the image
x1hat = PF1*XwEst;
x1hat = x1hat ./ repmat(x1hat(3,:),3,1);
x2hat = PF2*XwEst;
x2hat = x2hat ./ repmat(x2hat(3,:),3,1);
%Find root mean squared distance error
dist = ((corres1 - x1hat).*(corres1 - x1hat)) + ((corres2 - x2hat).* (corres2 - x2hat));
REGS …Run Code Online (Sandbox Code Playgroud) 在多处理调用的函数中,numpy ndarray的作用域是否有不同的作用?这是一个例子:
使用python的多处理模块我正在调用一个函数:
for core in range(cores):
#target could be f() or g()
proc = mp.Process(target=f, args=(core))
jobs.append(proc)
for job in jobs:
job.start()
for job in jobs:
job.join()
def f(core):
x = 0
x += random.randint(0,10)
print x
def g(core):
#Assume an array with 4 columns and n rows
local = np.copy(globalshared_array[:,core])
shuffled = np.random.permutation(local)
Run Code Online (Sandbox Code Playgroud)
调用时f(core),x变量是进程的本地变量,即.它按预期打印一个不同的随机整数.这些从不超过10,表明x=0在每个过程中.那是对的吗?
调用g(core)和置换数组的副本将返回4个相同的'shuffled'数组.这似乎表明工作副本不是本地子进程.那是对的吗?如果是这样,除了使用sharedmemory空间之外,当需要从共享内存空间填充时,是否可以让ndarray成为子进程的本地进程?
编辑:
改变g(core)添加随机整数似乎具有所需的效果.数组显示不同的值.必须发生的permutation事情是随机排序列(每个子进程的本地)相同的......想法?
def g(core):
#Assume an array with 4 columns and …Run Code Online (Sandbox Code Playgroud) python ×9
numpy ×4
python-2.7 ×2
argparse ×1
buffer ×1
ctypes ×1
github ×1
interactive ×1
matlab ×1
matplotlib ×1
regex ×1
scipy ×1
search ×1