بتل کد Battle Codeدارد باز می‌شود…

Balance the Data چالش 6 از جلسه‌ی 35، سخت

مدل منصف‌تر: memory خالی است. با دو حلقه روی هر سه نفر و هر دو نور، دو عکس از هر برچسب اضافه کن: memory += cam.samples(LABELS, who=who, light=light, n=2) (این‌ها عکس‌های 0 و 1 هستند، پس با عکس‌های آزمون که از 4 شروع می‌شوند قاطی نمی‌شوند). خروجی:
photos in memory: 36
و بعد دقت هر گروه و all: 32 of 36. کدام گروه هنوز کامل نیست؟ چه عکسی باید اضافه کنی؟

def distance(a, b):
    total = 0
    for r in range(len(a)):
        for c in range(len(a[0])):
            total += abs(a[r][c] - b[r][c])
    return total

def knn(pic, examples, k):
    pairs = []
    for label, ex in examples:
        pairs.append((distance(pic, ex), label))
    pairs.sort()
    votes = {}
    for d, label in pairs[:k]:
        votes[label] = votes.get(label, 0) + 1
    best = pairs[0][1]
    for label in votes:
        if votes[label] > votes[best]:
            best = label
    return best

import cam

LABELS = ["up", "down", "none"]
memory = []

def test_group(who, light):
    right = 0
    for label, photo in cam.samples(LABELS, who=who, light=light, n=2, start=4):
        if knn(photo, memory, 1) == label:
            right += 1
    return right

print("photos in memory:", len(memory))
total = 0
for who in ["sara", "omid", "nika"]:
    for light in ["bright", "dim"]:
        r = test_group(who, light)
        total += r
        print(who, light, r, "of 6")
print("all:", total, "of 36")

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 35: سوگیری: داده‌ی یک‌طرفه

بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال