Python中的随机游走图

时间:2018-06-26 17:07:14

标签: python

我一直在研究由Jesse M. Kinder和Philip Nelson撰写的“ Python物理建模学生指南”这本书,并指导我进行一个练习,以构建一个Brownian运动模拟器/随机行走模拟器和绘图它。我不知道为什么我的代码不起作用,我希望可以从您那里得到一些帮助:

end[i]

它不会抛出错误,但我无法绘制

编辑:

这类似于我期望看到的:

http://people.sc.fsu.edu/~jburkardt/m_src/random_walk_2d_simulation/walks_1_steps_1000_plot.png

4 个答案:

答案 0 :(得分:1)

使用numpy,您可以创建更高效​​的布尔切片。请注意,这不适用于Python Lists / Tuples。

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random as rng

def Brownian_motion(steps):
    """
    this is a random walk function
    define the number of steps to be taken as a integer value
    """
    #these are the random numbers
    steps_x = rng(steps)
    steps_y = rng(steps)

    pace_x = np.ones_like(steps_x)
    idx = steps_x < 0.5
    pace_x[idx] = -1

    idy = steps_y < 0.5
    pace_y = np.ones_like(steps_y)
    pace_y[idy] = -1

    plt.plot(np.cumsum(pace_x), np.cumsum(pace_y))
    # plt.axis("equal") 
    # I would also add this. This way your plot won't be
    # distorted.
    plt.show()

a = Brownian_motion(500)

Random Walk

答案 1 :(得分:0)

在循环结束时,您有不必要的return语句,因此您的代码永远不会到达绘图中。删除那些,Brownian_motion函数应该有机会完成执行。

答案 2 :(得分:0)

我不知道什么是布朗运动模拟器/随机行走模拟器,但是代码中的问题在于,在函数中,您有一个return语句(实际上为2),该语句使函数停止而不执行情节。

评论它似乎可以工作并且可以绘制一些内容(我不知道这是您所期望的)。

代码:

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random as rng

def Brownian_motion(steps):
    """
    this is a random walk function
    define the number of steps to be taken as a integer value
    """
    #these are the random numbers
    steps_x = rng(steps)
    steps_y = rng(steps)
    #Here I change the random numbers to 1 or -1
    pace_x = (steps_x < 0.5)
    #print(pace_x)

    x_list = list()
    y_list = list()
    for i in pace_x:
        if i == False:
            #pace_x[i] = -1
            x_list.append(-1)
        else:
            #pace_x[i] = 1
            x_list.append(1)
        #return pace_x
    print("Hello there")
    pace_y = (steps_y < 0.5)
    for i in pace_y:
        if i == False:
            #pace_y[i] = -1
            y_list.append(-1)

        else:
            #pace_x[i] = 1
            y_list.append(1)
        #return pace_y
    plt.plot(x_list, y_list)
    plt.show()

Brownian_motion(500)

建议:当Python中出现问题时,请尝试在代码中放置print函数调用,以检查期望的内容是否正确。例如,我将print("Hello there")行放在返回之后,并发现它从未执行过(现在已被注释)。

答案 3 :(得分:0)

尝试从函数中删除return,然后将布尔值转换为整数

%matplotlib notebook

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random as rng

def Brownian_motion(steps):
    """
    this is a random walk function
    define the number of steps to be taken as a integer value
    """
    #these are the random numbers
    steps_x = rng(steps)
    steps_y = rng(steps)

    #Here I change the random numbers to 1 or -1
    pace_x = (steps_x < 0.5)*1
    for i in pace_x:
        if i == False:
            pace_x[i] = -1
        else:
            pace_x[i] = 1
        #return pace_x

    pace_y = (steps_y < 0.5)*1
    for i in pace_y:
        if i == False:
            pace_y[i] = -1
        else:
            pace_x[i] = 1
        #return pace_y

    plt.plot(np.cumsum(pace_x), np.cumsum(pace_y))
    plt.show()

Brownian_motion(500)