一个经典小问题
随机漫步问题是数学上的经典问题,有着广泛的应用,特别是对问题进行抽象时。本文假设一个醉汉,每一次可以向四个方向随机的行走一步,最终画出这个醉汉走了N步的脚印记录。
思路分析
首先要确定醉汉的每一个随机步伐,结合Python实现质数求和的思路,随机步伐可以通过for循环每一走一步计算一次,但是计算量较大。或者利用随机向量,一次生成所有的随机数。计算了随机步伐,还需要求出每走一次的对应坐标,可以利用循环计算,熟悉numpy的话,可以直接用np.cumsum计算累积值,大大提高计算效率。
有了基本的数据,剩下的就是利用Matplotlib作图展示了,先展示一幅图,然后考虑展示九幅图,并且最终把每一幅图的内容合成到一张大图上。作图时需要考虑图像的查看效果,比如增加辅助线、起始点等。
探索过程
初步实现
首先需要通过Numpy实现随机步伐,通过步伐计算出走了N步的坐标,这里以10步为例。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#生成随机漫步的步长
x_arr = np.random.randint(-1,2,10)
y_arr = np.random.randint(-1,2,10)
step = np.column_stack((x_arr,y_arr))
step
coordinate = np.empty([10,2],dtype = int)
coordinate[0] = [0,0]
for i in range (0,9):
coordinate[i+1] = coordinate[i] + step[i]
coordinate
计算出结果后,针对coordinate作图1
2coordinate = coordinate.T
plt.plot(coordinate[0],coordinate[1])
上面计算坐标时的方法复杂了,可以利用np.cumsum一步实现,代码如下。
封装自定义函数
将10步变为n步,然后封装为自定义函数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29# 输出随机游走的坐标函数,方法一:
def random_coordinate(z):
x_arr = np.random.randint(-1,2,z)
y_arr = np.random.randint(-1,2,z)
steps = np.vstack((x_arr,y_arr))
steps = steps.T
random_coordinate = np.empty([z+1,2],dtype=int)
random_coordinate[0] = [0,0]
for i in range(0,z):
random_coordinate[i+1] = random_coordinate[i] + steps[i]
random_coordinate = random_coordinate.T
return random_coordinate
# 优化后的代码如下:
def random_coordinate(x):
step = np.random.randint(-1,2,(2,x))
random_position = np.hstack(([[0],[0]],np.cumsum(step,axis = 1)))
return random_position
random_coordinate(100)
# 定义作图函数
def random_walk_test(z):
x = random_coordinate(z)
ax = plt.subplot()
out = ax.plot(x[0],x[1])
return out
random_walk_test(100)
上面的函数对比可以发现代码简洁了不少,对基础包越熟悉越有可能写出简洁的代码。
功能增强
优化作图细节
增加了基于零点的辅助线,两条虚线坐标轴,起始点和终点的单独标注。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19z = 100
x_arr = np.random.randint(-1,2,z)
y_arr = np.random.randint(-1,2,z)
steps = np.vstack((x_arr,y_arr))
steps = steps.T
random_coordinate = np.empty([z+1,2],dtype=int)
random_coordinate[0] = [0,0]
for i in range(0,z):
random_coordinate[i+1] = random_coordinate[i] + steps[i]
random_coordinate = random_coordinate.T
plt.plot(random_coordinate[0],random_coordinate[1]);
plt.plot([0],[0],marker = "o",color = 'green')
plt.plot(random_coordinate[0,z],random_coordinate[1,z],marker = 'o',color = 'red')
plt.plot([0,0],[random_coordinate[1].min()-1,random_coordinate[1].max()+1],color = 'black',lw = 0.5,ls = '--')
plt.plot([random_coordinate[0].min()-1,random_coordinate[0].max()+1],np.zeros_like([random_coordinate[0].min()-1,random_coordinate[0].max()+1]),color = 'black',lw = 0.5,ls = '--')
plt.xlim(random_coordinate[0].min()-1,random_coordinate[0].max()+1)
plt.ylim(random_coordinate[1].min()-1,random_coordinate[1].max()+1)
把优化的代码封装为自定义函数1
2
3
4
5
6
7
8
9
10
11
12
13def random_walk_final(z):
x = random_coordinate(z)
ax = plt.subplot()
out = ax.plot(x[0],x[1])
out = ax.plot([0],[0],marker = "o",color = 'green')
out = ax.plot(x[0,z],x[1,z],marker = 'o',color = 'red')
out = ax.plot([0,0],[x[1].min()-1,x[1].max()+1],color = 'black',lw = 0.5,ls = '--')
out = ax.plot([x[0].min()-1,x[0].max()+1],np.zeros_like([x[0].min()-1,x[0].max()+1]),color = 'black',lw = 0.5,ls = '--')
out = plt.xlim(x[0].min()-1,x[0].max()+1)
out = plt.ylim(x[1].min()-1,x[1].max()+1)
out = ax.get_xaxis().set_visible(False)
out = ax.get_yaxis().set_visible(False)
return out
九宫格函数
1 | def rand_walk_9_test2(y): |
增加“九合一”内容
上面的代码已经很完善了,但是如何把几幅图的内容合到一起呢?因为Matplotlib是面向对象的方式进行作图,所以在循环中不仅仅输出每一次的图像,把内容同时存储到右侧的大图中,每一个循环做一次,就能把内容汇总起来了。
1 | def random_walk_final(times): |
坐标轴固定
(16年12月6日添加)如果坐标轴在中间会是什么情况呢?
1 | def random_walk_change_spine(times): |
醉汉的脚步还真是“随机”啊~
彩蛋
到这里可以结束,但是并没有结束。还做了其他的尝试,但是还没有得到结果:
- 在九宫格自定义函数中调用已经完成的单独的画出随机漫步图像的函数,失败了,得到了其他的图像,如果有成功的小伙伴欢迎留言沟通。
- 把九个格子的内容整合到一张图看一下效果,还没有进行尝试,期待能看出什么不一样的地方。
- 关于代码中用到的函数,涉及到比较特殊的会在其他文章中讲解。
以上的代码在我的github均有源码可以下载:
完
声明: 本文转载需标明出处,禁止用于商业目的。
ChangeLog
161205 新建
161206 新增优化算法,和九宫格合一的内容