이것은 numpy 배열만을 사용하는 임의 walk 문제의 버전입니다. 위치가 500 단계 이상 재검색 된 시간을 찾으려면 정렬 된 정렬 된 배열을 오프셋과 비교해야하며 0에 가까운 시간을 기록한 다음 오프셋을 증가시켜야합니다. 여기 Numpy 배열은 2 개의 배열 요소를 지표의 증가하는 오프셋과 비교하여 비교합니다.
문제가에 지금까지 내 코드 년대 나는 시간의 최종 금액 위치가 요소로 재 방문한다를 저장하기 위해 노력하고있어 어디 루프 '동안'은 'zeroArray'나는 그것을 실행 루프에 대한 부울 표현식이 변경 되었더라도 인덱스 오류가 발생하고 결과가 기록되지 않고 반복 횟수가 너무 많은 카운터가 너무 많습니다.
편집 : numpy 배열로 반복 위치를 찾는 방법 : 1) 최종 위치 배열을 증가 순서로 정렬하십시오. 2) 이웃 오프셋 (offset 1)과 위치를 비교하는 해당 오프셋 인 에서 0.001m 이내의 위치를 찾으면 슬라이스와 오프셋을 비교하십시오. 당신은 이웃 사람들이 두 공간에서 당신이 단지 2 가지 사례를 찾을 수 있다고 계산 한 18 가지 사례를 발견 할 수 있습니다. 그리고 세 공간에서 당신은 어느 지점에서 멈추는 지 알게 될 것입니다. 어떤 도움
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
감사합니다,
정확히 당신이하려고하는 것 : 예를 들어
을 같이 당신은 당신의 안타를 얻을 수 있습니다 이루다? 이 카운터들은 모두 매우 혼란 스럽습니다. 랜덤 워크 중 언제 다른 위치와 비슷한 위치인지 알기 위해 노력하고있는 것 같습니다. – Jaime
예. 그리고 내가 사용하기로되어있는 방법은 다음과 같습니다 : – user2193007
본문에서 편집을 참조하십시오. – user2193007