这是仅使用numpy数组的随机游走问题的一个版本。为了找到重新定位超过500步的位置的时间,我们必须将位置的排序数组与其偏移量进行比较,记录它接近0的时间,然后增加偏移量。
到目前为止,这是我的代码,问题出在'while'循环中,我试图将位置重新访问的最终次数存储为'zeroArray'中的元素
当我运行它时,我得到索引错误,没有记录结果,并且计数器已经迭代了太多次,即使循环停止的布尔表达式已经改变。
编辑:如何使用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
感谢您的帮助,
答案 0 :(得分:2)
您可以直接将您的位置向量与自身进行比较:
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