Реши. Выбрать нужно вариант чисел от 1 до 15

Erstellt am: 24. Oktober 2025

Beantwortet mit GPT-5 Thinking von Chat01

Frage

IMG_20251024_133027.jpgIMG_20251024_133027.jpg
IMG_20251024_133046.jpgIMG_20251024_133046.jpg

Реши. Выбрать нужно вариант чисел от 1 до 15

Python

python
from PIL import Image, ImageOps, ImageFilter, ImageEnhance, ImageStat, ImageDraw, ImageFont, Image img2=Image.open('/mnt/data/IMG_20251024_133046.jpg') img2.size (4624, 3472)

Python

python
img2_cropped=img2.crop((1500,1100,4400,3200)).resize((900,800)) img2_cropped <PIL.Image.Image image mode=RGB size=900x800>

Python

python
display(img2_cropped.crop((410,380,880,720)).resize((600,400)))

Python

python
display(img2_cropped.crop((0,200,450,650)).resize((600,600)))

Python

python
display(img2_cropped.crop((150,0,500,250)).resize((600,400)))

Python

python
display(img2_cropped.crop((0,40,300,280)).resize((600,400)))

Python

python
display(img2_cropped.crop((530,40,880,400)).resize((600,400)))

Python

python
display(img2_cropped.crop((350,450,640,720)).resize((500,400)))

Python

python
display(img2_cropped.crop((330,330,700,580)).resize((500,400)))

Python

python
display(img2_cropped.crop((410,180,540,350)).resize((250,300)))

Python

python
display(img2_cropped.crop((300,260,800,650)).resize((700,500)))

Python

python
import math, itertools, collections, numpy as np, pandas as pd # Define training points as (x,y,class) train = [] # Orange class 3 coords: for p in [(1,2),(2,1),(2,0),(4,2),(4,1),(6,1)]: train.append((*p,3)) # Blue class 1 (except test (3,4)) for p in [(2,3),(4,3),(5,3)]: train.append((*p,1)) # Red class 0 except test (8,2) for p in [(8,3),(9,2)]: train.append((*p,0)) # Green class 2 except test (5,0) for p in [(6,0),(7,0),(7,-1),(8,-1)]: train.append((*p,2)) len(train), train[:5] (15, [(1, 2, 3), (2, 1, 3), (2, 0, 3), (4, 2, 3), (4, 1, 3)])

Python

python
tests = [((8,2),0), ((3,4),1), ((5,0),2)] def knn_predict(k, point): x,y = point # compute squared Euclidean distances dist = [((x-xi)**2 + (y-yi)**2, cls) for xi,yi,cls in train] # sort by distance then class number (tie rule for nearest ordering) dist.sort(key=lambda t: (t[0], t[1])) # get first k neighbors k_neighbors = dist[:k] # count frequencies freq = collections.Counter(cls for d,cls in k_neighbors) # find max frequency max_freq = max(freq.values()) candidate_classes = [cls for cls,count in freq.items() if count==max_freq] if len(candidate_classes)==1: return candidate_classes[0] # tie: compute sum of distances among neighbors for each class sums = {cls: sum(d for d, c in k_neighbors if c==cls) for cls in candidate_classes} min_sum = min(sums.values()) candidates = [cls for cls, s in sums.items() if s==min_sum] if len(candidates)==1: return candidates[0] # still tie -> choose smaller class number return min(candidates) # For k=1..15 predictions for each test: results = {} for k in range(1,16): preds=[(point,knn_predict(k,point)) for point,_ in tests] correct = [pred==true for (point,true),(_,pred) in zip(tests, preds)] results[k]= (preds, correct) results[1] ([((8, 2), 0), ((3, 4), 1), ((5, 0), 2)], [True, True, True])

Python

python
good_k=[] details={} for k,(preds,correct) in results.items(): if all(correct): good_k.append(k) details[k]=preds,correct good_k, details[2] ([1, 2, 5], ([((8, 2), 0), ((3, 4), 1), ((5, 0), 2)], [True, True, True]))

Python

python
[(k, all(res[1])) for k,res in details.items()] [(1, True), (2, True), (3, False), (4, False), (5, True), (6, False), (7, False), (8, False), (9, False), (10, False), (11, False), (12, False), (13, False), (14, False), (15, False)]

Python

python
for k in range(1,16): preds,_=details[k] print(k, [(point,pred) for point,pred in preds], 'allcorrect', all(_))

Python

python
import math def knn_predict(k, point): x,y = point dist = [ (math.dist((x,y),(xi,yi)), cls) for xi,yi,cls in train] dist.sort(key=lambda t: (t[0], t[1])) k_neighbors = dist[:k] # counts freq = collections.Counter(cls for d,cls in k_neighbors) max_freq = max(freq.values()) candidate_classes = [cls for cls,count in freq.items() if count==max_freq] if len(candidate_classes)==1: return candidate_classes[0] sums = {cls: sum(d for d,c in k_neighbors if c==cls) for cls in candidate_classes} min_sum = min(sums.values()) candidates = [cls for cls,s in sums.items() if s == min_sum [(1, True, [((8, 2), 0), ((3, 4), 1), ((5, 0), 2)]), (2, True, [((8, 2), 0), ((3, 4), 1), ((5, 0), 2)]), (3, False, [((8, 2), 0), ((3, 4), 1), ((5, 0), 3)]), (4, False, [((8, 2), 0), ((3, 4), 1), ((5, 0), 3)]), (5, True, [((8, 2), 0), ((3, 4), 1), ((5, 0), 2)]), (6, False, [((8, 2), 2), ((3, 4), 1), ((5, 0), 3)]), (7, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (8, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (9, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (10, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (11, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (12, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (13, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (14, False, [((8, 2), 3), ((3, 4), 3), ((5, 0), 3)]), (15, False, [((8, 2), 3), ((3, 4), 3), ((5, 0), 3)])]

Gedanke

import math def knn_predict(k, point): x,y = point dist = [ (math.dist((x,y),(xi,yi)), cls) for xi,yi,cls in train] dist.sort(key=lambda t: (t[0], t[1])) k_neighbors = dist[:k] # counts freq = collections.Counter(cls for d,cls in k_neighbors) max_freq = max(freq.values()) candidate_classes = [cls for cls,count in freq.items() if count==max_freq] if len(candidate_classes)==1: return candidate_classes[0] sums = {cls: sum(d for d,c in k_neighbors if c==cls) for cls in candidate_classes} min_sum = min(sums.values()) candidates = [cls for cls,s in sums.items() if s == min_sum

[(1, True, [((8, 2), 0), ((3, 4), 1), ((5, 0), 2)]), (2, True, [((8, 2), 0), ((3, 4), 1), ((5, 0), 2)]), (3, False, [((8, 2), 0), ((3, 4), 1), ((5, 0), 3)]), (4, False, [((8, 2), 0), ((3, 4), 1), ((5, 0), 3)]), (5, True, [((8, 2), 0), ((3, 4), 1), ((5, 0), 2)]), (6, False, [((8, 2), 2), ((3, 4), 1), ((5, 0), 3)]), (7, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (8, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (9, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (10, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (11, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (12, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (13, False, [((8, 2), 2), ((3, 4), 3), ((5, 0), 3)]), (14, False, [((8, 2), 3), ((3, 4), 3), ((5, 0), 3)]), (15, False, [((8, 2), 3), ((3, 4), 3), ((5, 0), 3)])]

Thought for 5m 13s

Antwort

Ответ: 1, 2 и 5.

(После исключения трёх выделенных точек из обучающей выборки метод k-NN верно классифицирует одновременно все три точки (8;2), (3;4) и (5;0) только при k = 1, 2 и 5. При k = 3–4 для (5;0) побеждает класс 3, а при k ≥ 6 хотя бы одна из точек классифицируется неверно.)

Diesen Q&A teilen