#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Beginner Troubles PYTHON NUMPY 2D ARRAYS


    Hi, my issues is with trying to work with arrays that for each elements is a tuple of 2 values.

    specifically the problem is to generate a random 2-dimensional walk of 200 (but for testing say 2) steps with a max distance, then 100 (try just 2) of those walks per stage, and commencing each stage at the largest distance from the origin of the previous stage.

    I can successfully generate the array of random steps and get them to return the final position (x,y) value and as well calculate the distance of them from the origin of each walk:

    That's defined in these functions:


    Code:
    #............................................................getPositionInteger
        def getPosInt(description) :
            """Asks the user to enter a positive integer"""
            askAgain = False
            while askAgain == False:
                try:
                    posInt = eval(raw_input("\n %s :  " %description))
                    assert 0 < posInt , "Not a positive integer"
                    assert type(posInt) == int , "Not a positive integer"
                    askAgain = True
                except :
                    print "Input failed, not a positive integer"
            return posInt
        #...............................................................initialPosition
        def initialPosition() :
            """Initial position of walker at the start of a random walk"""
            return (0.0, 0.0)
        
        #......................................................................distance
        def distance(posA, posB) :
            """Distance between two positions"""
            xi = posA[0] ; yi = posA[1]
            xf = posB[0] ; yf = posB[1]
            return np.sqrt((xf-xi)**2+(yf-yi)**2)
        
            #..................................................................getPositions
        def getPositions(start, nSteps, maxStep):
            xArray = maxStep * np.random.random(nSteps+1)* np.cos(2.0 * np.pi * random.random())
            yArray = maxStep * np.random.random(nSteps+1)* np.sin(2.0 * np.pi * random.random())
            xArray[0] = start[0]
            yArray[0] = start[-1]
            xArray = np.cumsum(xArray)
            yArray = np.cumsum(yArray)                                                    
            return (xArray[-1], yArray[-1])
    But I can't get the array of the final position of each walk per stage in (x,y) form per stage

    Here's the main script and where I'm having trouble:

    Code:
        import numpy as np
        import matplotlib.pylab as plt
        import random
        import time
        
        MAX_STEP_SIZE = 0.90    # maximum size of a single step [m]
        random.seed(12345)
        
        #..........................................................................main
        def main ():
            ''''''
            print "RANDOM WALK IN STAGES IN TWO DIMENSIONS"
            userdata = getDetails()
            print "\nPlease wait while random walks are generated and analyzed..."
            NUM_STAGES = userdata[0]
            NUM_WALKS = userdata[1]
            NUM_STEPS = userdata[2]
            stageStart = initialPosition()
            for stage in np.arange(NUM_STAGES):
                walks = np.zeros((NUM_WALKS, NUM_WALKS), dtype=np.ndarray)
                for walk in walks:
                    walk = getPositions(stageStart, NUM_STEPS, MAX_STEP_SIZE)
                    print walk
                print walks
    You will see I'm having trouble making a an (x,y) style array, where the [0 0] should be [0.0 , 0.0] and its printed twice and additionally, its not changing to the final position.

    I really appreciate and help, advice or references you can provide.
    Thanks
    -Sid
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,930
    Rep Power
    481

    What do you expect as output?


    What output did you expect? I changed userdata=getDetails() to
    userdata = 10,1,10
    ran the program and got as output
    Code:
    RANDOM WALK IN STAGES IN TWO DIMENSIONS
    
    Please wait while random walks are generated and analyzed...
    (-4.6461161910377999, 0.32103417289982134)
    [[0]]
    (2.1955440711016769, 3.7807754156706594)
    [[0]]
    (-3.0304407306964394, 5.2429299862224612)
    [[0]]
    (-3.3162134005778117, 4.9166761309986038)
    [[0]]
    (3.830483661939506, 2.0576834780914264)
    [[0]]
    (-3.6234706789166853, 4.0016698869877896)
    [[0]]
    (-4.6367834098200982, 4.2339818579432684)
    [[0]]
    (4.4380056915390904, 2.6445702182361286)
    [[0]]
    (6.5452387932382976, 2.8836993165671081)
    [[0]]
    (-4.5986136756689291, 3.8931832057005007)
    [[0]]
    What do you expect as output?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,930
    Rep Power
    481
    I had hoped you'd respond so I could admonish you for your bad algorithm. I'll tell you anyway.

    Let's look at one step.
    Code:
    Correct: sample a radius and sample an angle
    x = radius * cos(angle)
    y = radius * sin(angle)
    
    stride length = radius * sqrt(sin**2 + cos**2)
    stride length = radius
    You, however, sampled radius and angle for x, and radius and angle for y. your x and y are uncorrelated.
    Wrong:
    Code:
    maximum x = cos(0)*(maximum radius)
    maximum y = sin(pi/2)*(maximum radius)
    maximum stride = sqrt((max radius) ** 2 + (max radius)**2)
    maximum stride = radius * sqrt(2)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo