我有这样一个数组:
A = array([1,2,3,4,5,6,7,8,9,10])
Run Code Online (Sandbox Code Playgroud)
我试图得到这样的数组:
B = array([[1,2,3],
[2,3,4],
[3,4,5],
[4,5,6]])
Run Code Online (Sandbox Code Playgroud)
每行(固定的任意宽度)移动一个.A的数组是10k记录长,我试图在Numpy中找到一种有效的方法.目前我正在使用vstack和一个缓慢的for循环.有更快的方法吗?
编辑:
width = 3 # fixed arbitrary width
length = 10000 # length of A which I wish to use
B = A[0:length + 1]
for i in range (1, length):
B = np.vstack((B, A[i, i + width + 1]))
Run Code Online (Sandbox Code Playgroud) 我有一个numpy形状(6,2)
[[00,01],
[10,11],
[20,21],
[30,31],
[40,41],
[50,51]]
Run Code Online (Sandbox Code Playgroud)
我需要一个步长为1的滑动窗口,窗口大小为3喜欢这个:
[[00,01,10,11,20,21],
[10,11,20,21,30,31],
[20,21,30,31,40,41],
[30,31,40,41,50,51]]
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个numpy解决方案.如果您的解决方案可以参数化原始数组的形状以及窗口大小和步长,那就太好了.
我发现这个相关的答案使用步幅有效的移动平均滤波器,但我没有看到如何指定那里的步长以及如何将窗口从3d折叠到连续的2d数组.这个滚动或滑动窗口迭代器在Python中,但是在Python中,我不确定它的效率如何.此外,它支持元素,但如果每个元素具有多个特征,则最终不会将它们连接在一起.
我正在尝试改进功能,该功能为图像的每个像素计算位于像素附近的像素的标准偏差.我的函数使用两个嵌入式循环来运行矩阵,这是我的程序的瓶颈.我想有可能通过numpy摆脱循环来改善它,但我不知道如何继续.欢迎任何建议!
问候
def sliding_std_dev(image_original,radius=5) :
height, width = image_original.shape
result = np.zeros_like(image_original) # initialize the output matrix
hgt = range(radius,height-radius)
wdt = range(radius,width-radius)
for i in hgt:
for j in wdt:
result[i,j] = np.std(image_original[i-radius:i+radius,j-radius:j+radius])
return result
Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要获得图像方差的项目.目前我正采取两种方法(两种方法都有效,但速度很慢):
这是使用numpy的代码,varianceMatrix是输出
varianceMatrix = np.zeros(im.shape,np.uint8)
w = 1 # the radius of pixels neighbors
ny = len(im)
nx = len(im[0])
for i in range(w,nx-w):
for j in range(w,ny-w):
sampleframe = im[j-w:j+w, i-w:i+w]
variance = np.var(sampleframe)
varianceMatrix[j][i] = int(variance)
return varianceMatrix
Run Code Online (Sandbox Code Playgroud)
这是scipy功能:
from scipy import ndimage
varianceMatrix = ndimage.generic_filter(im, np.var, size = 3)
Run Code Online (Sandbox Code Playgroud)
scipy功能更快,但不是那么多.我正在寻找一种更好的替代方案来计算方差.
有任何想法吗???
当我使用python实现一个滑动窗口来检测静止图像中的对象时,我开始了解这个很好的函数:
numpy.lib.stride_tricks.as_strided
Run Code Online (Sandbox Code Playgroud)
因此,我尝试实现一般规则,以避免在改变我需要的滑动窗口大小时可能失败的错误.最后我得到了这个代表:
all_windows = as_strided(x,((x.shape[0] - xsize)/xstep ,(x.shape[1] - ysize)/ystep ,xsize,ysize), (x.strides[0]*xstep,x.strides[1]*ystep,x.strides[0],x.strides[1])
Run Code Online (Sandbox Code Playgroud)
这导致4 dim矩阵.前两个表示图像的x和y轴上的窗口数.其他代表窗口的大小(xsize,ysize)
并且step表示两个连续窗口之间的位移.
如果我选择方形滑动窗口,这种表示可以正常工作.但我仍然有一个问题,让这个工作为ex(128,64)的窗口,我通常得到图像不相关的数据.
我的代码出了什么问题.有任何想法吗?如果有一个更好的方法来获得一个漂亮和整洁的python图像处理滑动窗口?
谢谢
我有4个2D numpy数组,称为a, b, c, d每个数组由n行和m列组成.我需要做的是给每个元素b和d一个如下计算的值(伪代码):
min_coords = min_of_neighbors_coords(x, y)
b[x,y] = a[x,y] * a[min_coords];
d[x,y] = c[min_coords];
Run Code Online (Sandbox Code Playgroud)
min_of_neighbors_coords给定数组元素坐标的函数在哪里返回具有较低值的'neighbor'元素的坐标.即,考虑到阵列:
1, 2, 5
3, 7, 2
2, 3, 6
Run Code Online (Sandbox Code Playgroud)
min_of_neighbors_coords(1, 1)将引用具有值的中心元素7,并将返回元组(0, 0):数字的坐标1.
我设法使用for循环(每个元素的元素),但算法非常慢,我正在寻找一种方法来改进它,避免循环,并要求计算numpy.
可能吗?
我试图在theano中实现一个扫描循环,给定张量将使用输入的"移动切片".它实际上不必是一个移动切片,它可以是另一个张量的预处理张量,代表移动切片.
实质上:
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
|-------| (first iteration)
|-------| (second iteration)
|-------| (third iteration)
...
...
...
|-------| (last iteration)
Run Code Online (Sandbox Code Playgroud)
|-------|每次迭代的输入在哪里.
我试图找出最有效的方法,也许使用某种形式的引用或操纵步幅,但我还没有设法让一些东西工作,即使是纯粹的numpy.
我找到的一个可能的解决方案可以在这里找到,但我无法弄清楚如何使用步幅,我没有看到与theano一起使用它的方法.
我正在尝试使用此节拍检测算法在 python 中进行音频处理。我已经实现了上述文章中的第一个(非优化版本)。虽然它打印了一些结果,但我无法检测它是否具有一定的准确性,因为我不知道如何用它播放声音。
目前,我习惯Popen在进入计算循环之前用歌曲异步启动媒体播放器,但我不确定此策略是否有效并给出同步结果。
#!/usr/bin/python
import scipy.io.wavfile, numpy, sys, subprocess
# Some abstractions for computation
def sumsquared(arr):
sum = 0
for i in arr:
sum = sum + (i[0] * i[0]) + (i[1] * i[1])
return sum
if sys.argv.__len__() < 2:
print 'USAGE: wavdsp <wavfile>'
sys.exit(1)
numpy.set_printoptions(threshold='nan')
rate, data = scipy.io.wavfile.read(sys.argv[1])
# Beat detection algorithm begin
# the algorithm has been implemented as per GameDev Article
# Initialisation
data_len = data.__len__()
idx = 0
hist_last = …Run Code Online (Sandbox Code Playgroud) 我想一次将函数应用于单维数组3个元素,并为每个元素输出单个元素.
例如,我有一个包含13个元素的数组:
a = np.arange(13)**2
Run Code Online (Sandbox Code Playgroud)
我想应用一个函数,让我们以np.std为例.
这是等效的列表理解:
[np.std(a[i:i+3]) for i in range(0, len(a),3)]
[1.6996731711975948,
6.5489609014628334,
11.440668201153674,
16.336734339790461,
0.0]
Run Code Online (Sandbox Code Playgroud)
有没有人知道使用numpy函数更有效的方法?
numpy ×8
python ×8
scipy ×2
image ×1
optimization ×1
python-2.7 ×1
theano ×1
time-series ×1
variance ×1