小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. Python
  5. 正文

随机漫步模型之python实现

2017年10月22日 1249点热度 0人点赞 0条评论

random_walk.py

# -*- coding: utf-8 -*-
"""
Created on Sun Oct 22 15:22:51 2017

@author: dqhpl
"""
from random import choice

class RandomWalk():
    """一个生成随机漫步数据的类"""
    
    def __init__(self, num_points = 5000):
        """初始化随机漫步的属性"""
        self.num_points = num_points
        """所有随机漫步都始于(0,0)"""
        self.x_values = [0]
        self.y_values = [0]
    def fill_walk(self):
        """计算机随机漫步包含的所有点"""
        #不断漫步,直到列表到达指定长度
        while len(self.x_values)< self.num_points:
            #决定前进方向以及沿这个方向前进的距离
            x_direction = choice([1, -1])
            x_distance = choice([0,1,2,3,4])
            x_step = x_direction * x_distance
            
            y_direction =choice([1,-1])
            y_distance = choice([0,1,2,3,4])
            y_step = y_direction * y_distance
            
            #拒绝原地踏步
            if x_step == 0 and y_step == 0:
                continue
            
            #计算下一个点的x和y值
            next_x = self.x_values[-1] + x_step
            next_y = self.y_values[-1] + y_step
            
            self.x_values.append(next_x)
            self.y_values.append(next_y)

rw_visual.py

# -*- coding: utf-8 -*-
"""
Created on Sun Oct 22 15:42:34 2017
@author: dqhpl
"""
import matplotlib.pyplot as plt
from random_walk import RandomWalk
#创建一个randomwalk实例,并将其包含的点都绘制出来
while True:
    rw = RandomWalk()
    rw.fill_walk()
    point_numbers = list(range(rw.num_points))
    #plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Reds,
                #edgecolor = 'none',s=3)
    plt.plot(rw.x_values, rw.y_values)
    #突出起点和终点
    
   #plt.scatter(0,0, c='green', edgecolors=None, s=10)
   #plt.scatter(rw.x_values[-1],rw.y_values[-1], c='red', edgecolor=None,s=10)
    #隐藏坐标轴
    #plt.show()
    plt.savefig('randomwalk.png', bbox_inches = 'tight')
    keep_running = input("Make another walk?(y/n)\n")
    if keep_running == 'n':
        break

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Python python学习
最后更新:2017年10月22日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
C++ Primer Plus(第五版)第5章编程题答案 《超决战!奥特英雄传说》官方网站12月25日晚新版即将上线! [leetcode]题目解析(190612) Python chapter 11 learning notes WebRTC Noise Suppression逻辑关系图 志愿高校团体掠影
标签聚合
鸟哥的linux私房菜 生活 python学习 算法 Java leetcode Python linux 高中 学习
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 9 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号