Click here to Skip to main content
15,896,154 members

Beginner trouble Numpy 2D arrays

Sidney Secter asked:

Open original thread
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:

#............................................................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:
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
Tags: Python

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900