2013-03-20 41 views
2

這是一個版本的隨機遊走問題,只使用numpy數組。要查找500次以上的位置重新訪問的時間,我們必須將排序的位置數組與其偏移量進行比較,記錄它接近0的時間,然後增加偏移量。Numpy數組比較兩個數組明智與增加指示的偏移

這裏是我的代碼,到目前爲止,這個問題是在「同時」的循環,我想保存的位置重新爲元素的次最終量「zeroArray」

當我運行我得到一個索引錯誤,沒有記錄結果,並且一個計數器迭代了很多次,即使循環停止的布爾表達式已經改變了。

編輯:如何找到numpy數組的重複位置:1)按遞增順序對最終位置數組進行排序。 2)比較切片與增加的偏移量,只要你找到位置在0.001米以內的位置 那是你比較位置與相鄰位置(偏移量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

究竟是你想什麼實現?所有這些計數器都很混亂。在隨機遊走期間,你似乎試圖找出何時與步行中另一個位置相似的位置? – Jaime 2013-03-20 23:46:20

+0

是的。我應該使用的方法如下: – user2193007 2013-03-21 00:06:34

+0

請參閱我在主文本中的編輯 – user2193007 2013-03-21 00:15:00

回答

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 
+0

當我嘗試從命中中量化True的數量時,如果僅爲500個位置的散步返回500次以上的值,該怎麼辦? 我嘗試過np.sum(點擊)和np.count_nonzero(點擊) – user2193007 2013-03-21 01:26:33

+0

@ user2193007'hits'在對角線上有501個'真',並且你每次點擊計數兩次,都是'(i,j)'和'(j,i)'。從總數中減去501併除以2得到實際的命中數。 – Jaime 2013-03-21 02:02:02

+0

謝謝!對不起,麻煩了,但如果你也可以給一個資源的鏈接,可以教我一些關於切片numpy陣列,這將是驚人的! – user2193007 2013-03-21 02:11:36