• CS50_AI_2_Uncertainty 概率 (Python实现 - 英文注释)


    主要内容

    1. 概率论基础, 贝叶斯公式, 贝叶斯网络
    2. 似然估计
    3. 马尔可夫模型, 马尔可夫链, 隐马尔可夫模型

    课程笔记地址 : https://cs50.harvard.edu/ai/2020/notes/2/#joint-probability
    官网:https://cs50.harvard.edu/ai/2020/weeks/2/
    上课源码: http://cdn.cs50.net/ai/2020/spring/lectures/2/src2.zip

    代码讲解

    本人概率论学不好, 所以只讲贝叶斯网络的部分 (

    likelihood.py

    from model import model
    
    # Calculate probability for a given observation
    # none rain, no maintenance, train on time, appointment attend 的 概率
    probability = model.probability([["heavy", "yes", "delayed", "miss"]])
    
    print(probability)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    model.py

    from pomegranate import *
    
    # Rain node has no parents: root Node
    # rain's probability
    rain = Node(DiscreteDistribution({
        "none": 0.7,
        "light": 0.2,
        "heavy": 0.1
    }), name="rain")
    
    # Track maintenance node is conditional on rain
    # rain -> maintenance
    maintenance = Node(ConditionalProbabilityTable([
        ["none", "yes", 0.4],
        ["none", "no", 0.6],
    
        ["light", "yes", 0.2],
        ["light", "no", 0.8],
    
        ["heavy", "yes", 0.1],
        ["heavy", "no", 0.9]
    ], [rain.distribution]), name="maintenance")
    
    # Train node is conditional on rain and maintenance
    # rain -> Train,  maintenance -> Train
    train = Node(ConditionalProbabilityTable([
        ["none", "yes", "on time", 0.8],
        ["none", "yes", "delayed", 0.2],
    
        ["none", "no", "on time", 0.9],
        ["none", "no", "delayed", 0.1],
    
        ["light", "yes", "on time", 0.6],
        ["light", "yes", "delayed", 0.4],
    
        ["light", "no", "on time", 0.7],
        ["light", "no", "delayed", 0.3],
    
        ["heavy", "yes", "on time", 0.4],
        ["heavy", "yes", "delayed", 0.6],
    
        ["heavy", "no", "on time", 0.5],
        ["heavy", "no", "delayed", 0.5],
    ], [rain.distribution, maintenance.distribution]), name="train")
    
    # Appointment node is conditional on train
    # train -> appointment 
    appointment = Node(ConditionalProbabilityTable([
        ["on time", "attend", 0.9],
        ["on time", "miss", 0.1],
    
        ["delayed", "attend", 0.6],
        ["delayed", "miss", 0.4]
    ], [train.distribution]), name="appointment")
    
    # Create a Bayesian Network and add states
    model = BayesianNetwork()
    model.add_states(rain, maintenance, train, appointment) # Bayesian's Node
    
    # Add edges connecting nodes
    model.add_edge(rain, maintenance)
    model.add_edge(rain, train)
    model.add_edge(maintenance, train)
    model.add_edge(train, appointment)
    
    # Finalize model
    model.bake()
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    讲解

    关系图

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6CrdEPg9-1668324150628)(https://cdn.acwing.com/media/article/image/2022/11/13/157227_0034576763-QQ图片20221113151541.png)]
    就是一棵树, 其中父节点的情况影响子节点的情况, 然后根据给出的各个条件的情况, 来计算最后约会准时的概率

    1. 其中Node 为一个类型转换, 转换为了一个树上的节点Node
    2. 然后add_edge 这些应该配合注释都能看懂, 就是 加边 add 函数, 不由感慨 Python的方便
    3. 如果你发现有些库名未定义, 在vscode中的终端下输入: pip install 库名
  • 相关阅读:
    数据分析——埋点
    Segment Routing IPv6简介
    企业简化客户服务的5种方法
    springboot集成swagger3.0
    实验1机器学习之线性回归实验
    Sentinel 规则
    本地环境下启动openFaas创建的Java的云函数
    小程序的数据驱动和vue的双向绑定有何异同?
    Airbnb的动态kubernetes集群扩缩容
    蚁群优化算法解决TSP问题(Matlab代码实现)
  • 原文地址:https://blog.csdn.net/lihua777/article/details/127832652