Iti0210w9tut: erinevus redaktsioonide vahel

Allikas: Lambda
Mine navigeerimisribale Mine otsikasti
PriitJ (arutelu | kaastöö)
Uus lehekülg: '<pre> import csv from collections import defaultdict counts = defaultdict(int) with open("somerville.txt") as f: rd = csv.reader(f, delimiter=",") print(next(rd)) fo...'
 
PriitJ (arutelu | kaastöö)
Resümee puudub
 
39. rida: 39. rida:
P = P_qe / P_e
P = P_qe / P_e
</pre>
</pre>
[[Category:Iti0210]]

Viimane redaktsioon: 24. märts 2021, kell 10:28

import csv
from collections import defaultdict

counts = defaultdict(int)
with open("somerville.txt") as f:
    rd = csv.reader(f, delimiter=",")
    print(next(rd))
    for r in rd:
        counts[tuple(r)] += 1

# ['happiness', 'house_price', 'school_quality', 'road_quality']

N = sum(counts.values())
jpd = {}
for happiness in ["happy" , "unhappy"]:
    for house_price in ["low", "ok", "high"]:
        for school_quality in ["low", "ok", "high"]:
            for road_quality in ["low", "ok", "high"]:
                p = counts[(happiness,house_price,school_quality,road_quality)]/N
                jpd[(happiness,house_price,school_quality,road_quality)] = p

# e: road_quality = low
# q: happiness = happy
# hidden variables h: [house_price, school_quality]

P_e = 0
for happiness in ["happy" , "unhappy"]:
    for house_price in ["low", "ok", "high"]:
        for school_quality in ["low", "ok", "high"]:
            P_e += jpd[(happiness,house_price,school_quality,"low")]

P_qe = 0
for house_price in ["low", "ok", "high"]:
    for school_quality in ["low", "ok", "high"]:
        P_qe += jpd[("happy",house_price,school_quality,"low")]

# P(happy|road_quality=low)
P = P_qe / P_e