Numpy数组比较两个数组的元素,增加索引偏移量



这是只使用numpy数组的随机漫步问题的一个版本。为了找到在500步内重新访问位置的次数,我们必须将位置的排序数组与其偏移量进行比较,记录它接近0的时间,然后增加偏移量。

这是我的代码到目前为止,问题是在'while'循环中,我试图存储位置作为'zeroArray'中的元素重新访问的最终次数

当我运行它时,我得到一个索引错误,没有记录结果,计数器迭代了很多次,即使循环停止的布尔表达式已经改变。

edit:如何使用numpy数组查找重复位置:1)将最终位置数组按递增顺序排序。2)比较不断增加偏移量的切片,只要你在该偏移量的0.001m范围内找到位置也就是说,你将位置与相邻位置(偏移量1)进行比较,你可能会发现18种情况,而相邻位置在两个空间中可能只发现2种情况。在三个空格处,你会发现你停在0处。

import numpy as np
import random
MAX_STEP_SIZE = 0.90    # maximum size of a single step [m]
NUM_STEPS = 500         # number of steps in a random walk []
NUM_WALKS = 10        # number of random walks in a run []
TOLERANCE = 0.001    # separation of points considered the same [m]
STEP_TO_RECORD_1 = 100 # first step to record and analyze []
STEP_TO_RECORD_2 = 500 # 2nd step to record and analyze []
random.seed(12345)
#......................................................................distance
def distance(posA, posB) :
    """Distance between two positions"""
    return np.abs(posA - posB)

#...............................................................initialPosition
def initialPosition() :
    """Initial position of walker at the start of a random walk"""
    return 0.0
def genPositions(nSteps, maxStep) :
    """Return the new position after a random step between -maxStep and
    maxStep, given the previous position"""
    genArray1 = (maxStep - (-maxStep))*(np.random.random(nSteps+1)) + (-maxStep)
    genArray1[0]=initialPosition()    
    return np.cumsum(genArray1)
oneStep = np.zeros(NUM_WALKS)
fiveStep = np.zeros(NUM_WALKS)
zeroStep = np.zeros(NUM_WALKS)
walkArray = np.zeros(NUM_WALKS)
counter = 1
hitcounter = 0
zerocounter = 0
keepchecking = bool(1)
for ii in range(NUM_WALKS):
    position = (genPositions(NUM_STEPS, MAX_STEP_SIZE))   
    oneStep[ii] = position[100]
    fiveStep[ii] = position[-1]
    zeroArray = np.sort(position)
    while keepchecking == bool(1):
        zerocounter = 0
        for jj in range(len(zeroArray)):
            hitcounter = 0
            if distance(zeroArray[jj+counter], zeroArray[jj]) <= TOLERANCE:
               hitcounter +=1
            zerocounter += hitcounter
            counter +=1
            if hitcounter == 0:
                keepchecking = bool(0)
    zeroStep[ii] = zerocounter

谢谢你的帮助,

你可以直接比较你的位置向量和它自己:

deltas = np.abs(position[None, :] - position[:, None])

deltas[i, j]为步骤i和步骤j位置之间的距离。你可以得到点击:

hits = deltas <= TOLERANCE

要获得收盘位置对,您可以执行以下操作:

row, col = np.nonzero(hits)
idx = row < col # get rid of the diagonal and the upper triangular part
row = row[idx]
col = col[idx]

例如:

>>> position = genPositions(NUM_STEPS, MAX_STEP_SIZE)
>>> row, col = np.nonzero(np.abs(position[None, :] -
...                              position[:, None]) < TOLERANCE)
>>> idx = row < col
>>> row= row[idx]
>>> col = col[idx]
>>> row
array([ 35,  40, 112, 162, 165, 166, 180, 182, 200, 233, 234, 252, 253,
       320, 323, 325, 354, 355, 385, 432, 443, 451], dtype=int64)
>>> col
array([ 64,  78, 115, 240, 392, 246, 334, 430, 463, 366, 413, 401, 315,
       380, 365, 348, 438, 435, 401, 483, 473, 492], dtype=int64)
>>> for j in xrange(len(row)) :
...     print '{0}, {1} --> {2}, {3}'.format(row[j], col[j], position[row[j]],
...                                          position[col[j]]) 
... 
35, 64 --> 2.56179226445, 2.56275159205
40, 78 --> 2.97310455111, 2.97247314695
112, 115 --> 3.40413767436, 3.40420856824
...
432, 483 --> 10.2560778101, 10.2556475598
443, 473 --> 10.7463713139, 10.7460626764
451, 492 --> 12.3804383241, 12.3805940238

最新更新