week9

作者: 猪蹄炖粥 | 来源:发表于2018-08-08 23:18 被阅读4次

chp14 随机游走与数据可视化

模拟模型:1、模仿实际系统的活动2、预测实验系统的未来状态

布朗运动是随机游走的一种

随机游走

  • 醉汉游走
  • class Location
  • class Field
  • class usualDrunk
  • class Drunk
    完整代码如下:
import random
class Location(object):
    def __init__(self,x,y):
        self.x = x
        self.y = y
    
    def move(self,deltaX,deltaY):
        """
        deltaX,deltaY是数值型
        """
        return Location(self.x + deltaX , self.y+ deltaY)
    def getX(self):
        return self.x
    def getY(self):
        return self.y
    
    def distFrom(self,other):
        ox, oy = other.x ,other.y
        xDist, yDist = self.x - ox, self.y- oy
        return (xDist**2 + yDist**2)**0.5
    
    def __str__(self):
        return '<' + str(self.x) + ',' + str(self.y) + '>'

class Field(object):
    def __init__(self):
        self.drunks = {}
    
    def addDrunk(self,drunk,loc):
        if drunk in self.drunks:
            raise ValueError('Duplicate drunk')
        else:
            self.drunks[drunk] = loc
    
    def moveDrunk(self,drunk):
        if drunk not in self.drunks:   
            raise ValueError('Drunk not in field')
        xDist , yDist = drunk.takeStep()
        currentLocation = self.drunks[drunk]
        self.drunks[drunk] = currentLocation.move(xDist, yDist)
    def getLoc(self,drunk):
        if drunk not in self.drunks:
            raise ValueError('Drunk not in field')
        return self.drunks[drunk]

class Drunk(object):
    def __init__(self,name=None):
        """假设name是字符"""
        self.name = name
    def __str__(self):
        if self != None:
            return self.name    
        return 'Anonymous'
    
class UsualDrunk(Drunk):
    def takeStep(self):
        stepChoices = [(0,1),(0,-1),(1,0),(-1,0)]
        return random.choice(stepChoices)

def walk(f,d,numSteps):
    '''
        f:Field
        d:Drunk
        numSteps: step counts
    '''
    start = f.getLoc(d)
    for s in range(numSteps):
        f.moveDrunk(d)
    return start.distFrom(f.getLoc(d))

def simWalks(numSteps,numTrials,dClass):
    '''
    numSteps 
    '''
    Homer = dClass()
    origin = Location(0,0)
    distances = []
    for t in range(numTrials):
        f = Field()
        f.addDrunk(Homer,origin)
        distances.append(round(walk(f,Homer,numSteps),1))
    return distances

def drunkTest(walkLengths,numTrials,dClass):
    for numSteps in walkLengths:
        distances = simWalks(numSteps,numTrials,dClass)
        print(dClass.__name__,'random walk of',numSteps,'steps')
        print('Mean = ',round(sum(distances)/len(distances),4))
        print('Max = ',max(distances),'Min = ',min(distances))


drunkTest((10, 100, 1000, 10000), 100, UsualDrunk)

有机随机游走

即行为的倾向性

相关文章

  • Coursera.MachineLearning.Week9

    Machine Learning Week9 : Anomaly Detection & Recommender ...

  • 2020年week 9,10,11,12,13 迟到的复盘

    Week9 2020.2.24~2020.3.1Week10 2020.3.2 ~2020.3.8Week11 2...

  • CS229 Week9 异常检查&推荐系统

    title: CS229 Week9 异常检查&推荐系统date: 2017-03-26 18:45:19cate...

  • week9

    chp14 随机游走与数据可视化 模拟模型:1、模仿实际系统的活动2、预测实验系统的未来状态 布朗运动是随机游走的...

  • week9

    每年二月做工资奖金计划,三月宣布,四月生效。工资是保密的,每个人都只能知道自己的数。我想,各个公司的薪酬体系应该是...

  • Week9

    本周目标 完成同时使用生成的配置文件以及读取原来DB中的配置文件,并且根据同一个domain object的改动生...

  • week9

    工作:这个星期组织大家改之前的自动化测试脚本,有一些脚本写的好糟糕。但是我们会把它们改好。从好的角度讲,有一点值得...

  • 快手的一些重设计与思考

    Weekly redesign challenge Week9 这周选择去重设计的App是快手。因为一直没用过快手...

  • 练字 week9

  • Week9复盘,2月收获满满,3月期盼更满

    Week9充实的一个礼拜,下面详细的说说我完成了哪些事件。 阅读 《被讨厌的勇气》️ 《人生》 《只管去做》 《断...

网友评论

      本文标题:week9

      本文链接:https://www.haomeiwen.com/subject/qilovftx.html