zad3 wip2

This commit is contained in:
kapitan committed 2022-01-23 10:39:54 +01:00
1 parent baab800220
commit d30f72f4c1
6 files changed
+87 -44

No files matched your search

+1
View File
@@ -0,0 +1 @@
[[0.26707464387488744, 0.874854952853165], [0.705437371776663, -0.34949767414070093], [-0.5123772864623964, 0.6642064994779734], [-0.6177949905888044, -0.08653100644282426], [0.2306447201597256, 0.15997733585871163], [-0.17981113007117286, -0.5662529061011463], [0.5210997923912827, -0.4328726372954992], [0.098444558908785, 0.9463812329618818], [-0.08349153183022817, -0.9536270256945143], [0.4733394651575273, -0.7495341015398066], [-0.6065455160048521, 0.1681939852588296], [0.33182453244095295, -0.5072685611099996], [0.9123852245993803, 0.20912199256357955], [0.5760493149872555, 0.5349177709112336], [-0.34401542908441646, -0.6829244249035414], [-0.5444059397567688, -0.8072194131021861], [-0.348044369392786, -0.7953176738397602], [0.3879884431435828, 0.6301030241070193], [0.3014344191306867, 0.1955985852142356], [0.2008763711882078, -0.6581882714512488], [-0.4298467671032608, 0.07063965484877796], [0.5714395567933921, -0.3411581119355833], [0.5130674745215648, 0.17082236936224793], [-0.31330386010949196, -0.7232347065738357], [0.3910415194714711, -0.8619910578621013], [0.37231010226526, -0.09949715555220831], [0.011172274885089867, 0.6957560233959222], [0.2042916905450732, 0.8933295265723123], [0.8772299632735555, -0.4599154183794484], [-0.6745371421227153, -0.6246224283985878], [-0.8548459622621303, 0.026203763525584668], [0.0003960555111902596, -0.2851993384779069], [0.13132744802058663, -0.8372460708371889], [0.6070920361038301, 0.7481629718139915], [-0.33085832366473544, 0.1265368675267927], [0.27903484181199883, 0.7651311160372127], [0.15800545539286157, -0.2511562767613446], [0.3743138554175838, 0.2591288724936859], [0.5814623997248104, 0.3735091839192642], [-0.43803851777676744, 0.02554483674462911], [-0.6332547100290917, -0.6126079756852014], [0.580874356779032, -0.5044583569611079], [-0.17533686527341164, -0.0041740262222879085], [-0.7944419041111201, 0.21566814287601763], [-0.8513159614224243, -0.45868850291904967], [0.6214933580430171, 0.3343114816776216], [-0.8543237265124555, 0.1106665871050883], [-0.5549208862251612, -0.6489338122175173], [0.45568997517605603, 0.6444683755231256], [-0.6048703175530287, -0.012780126690070353], [-0.16077571375999355, 0.5283910865401149], [0.10544050990938172, -0.5293124040022554], [-0.4167369042996385, 0.19455253960946214], [-0.1674194590747637, 0.571379676102006], [-0.675987752288574, 0.548631195266286], [0.23808787417822755, -0.5401261345428957], [0.875481281158248, 0.36696888311713516], [0.0032516528309900175, -0.1959173726852134], [-0.03264751267940699, -0.4423419510181568], [-0.2079660605331547, 0.4543529329466901], [0.19299466230881132, 0.28830685077653373], [0.5377729862858684, -0.7242490744221382], [0.217577389761842, 0.4612400302420327], [0.8207616971635188, -0.5242199691124612], [0.4150564189238725, 0.12168136108987514], [-0.45147811088354134, -0.3201352197111406], [-0.6375066490596355, -0.3906385783929273], [-0.21694262947054296, -0.4865687734231412], [-0.7536021068296546, -0.05953741704137909], [-0.2077938444479637, -0.820394986674174], [-0.413187840312592, 0.5419512753220819], [-0.0626200267476199, -0.4554300852485216], [-0.579173237508243, 0.38108877820925385], [-0.38639485556039166, 0.8236972812557243], [0.30285172576280256, 0.6973254196563166], [-0.18643077301152702, 0.8104664169281803], [-0.5056702408881234, -0.5645139129285524], [-0.19965409241022655, 0.020567382601630758], [0.7906613651062897, 0.4526155702182911], [0.22926769838935906, -0.09593168962690546], [0.2655874410918289, 0.775614912458297], [0.22446631035820688, 0.633307350304088], [-0.5252416870256676, -0.06105251694880269], [0.5350692099326566, 0.7171627237384379], [-0.07607819853592684, 0.5383928073863181], [-0.23214352774173766, -0.6687475643171613], [-0.945739293667666, 0.0278862483993709], [-0.9516580049184689, -0.20550528518977657], [0.432592032135796, -0.04357131293906445], [-0.15624644689389688, -0.9324537874524754], [0.9245085417241353, -0.37397169960687243], [-0.6264100577306938, -0.2698820209933208], [0.28159509543403355, -0.4085865726487092], [0.5111554547290363, 0.5996614930399004], [0.49207426523039743, 0.7772600457052795], [0.3979500438013701, -0.7575119633086485], [-0.11051877213392854, -0.7379223877397723], [0.25011225416657534, -0.2445013997226545], [0.535125138882435, -0.5058053749999026], [-0.05424425728455065, -0.09152221099526703], [-0.2076310905771738, 0.3906242865798983], [-0.20781512209852343, -0.8321550845773823], [0.804606639403553, -0.19200269746384477], [-0.3737101095721845, -0.4757043370633175], [0.09933587075907778, -0.9911738502964657], [-0.23469955934654554, 0.1187513870199458], [0.13712031648874362, 0.2582211898922505], [-0.3300882243125398, 0.9331843402218332], [0.08406339925746308, 0.31166138120285697], [-0.6025364808434707, -0.546624972487031], [0.030589506521855696, 0.20344277046948397], [-0.4889611552697711, -0.6726500006036894], [0.5259962114486337, 0.3335387807645858], [-0.3137622108076408, -0.22670442965431523], [-0.44041663701966516, -0.536200Line truncated
BIN
View File
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
[[3.094031536539631, 0.39478546662283415], [2.7296437622770164, -0.2613398001285549], [2.9699785275240655, 0.4463305680463877], [2.63945978783313, -0.2860601346977574], [2.918014288162325, -0.3568879841771503], [2.9757077054866814, 0.1976143367014519], [3.030438075231262, 0.4250929929744947], [3.389530100177308, -0.28379538762741097], [3.1280427502674355, -0.12422476057554413], [3.451993640418272, 0.11973477733048372], [2.825035233117628, 0.16844395811472174], [3.250405577067777, -0.3676711376419543], [2.9296598477439826, -0.26590506043713863], [2.6794456023856763, -0.12101255993467745], [3.237000886012415, 0.3819004399412394], [2.661815093143124, 0.22666139264932847], [2.6713788682959203, -0.34563361203295895], [3.240482686883239, 0.1677585999982612], [3.0973924359504146, 0.37249814406887993], [2.8380071737589554, 0.07353401757908563], [2.855735732068298, 0.4727569854083527], [2.8220073260916707, -0.0020349015143730337], [3.007589963913807, 0.357216588461496], [2.819100032462428, 0.0026571757221326713], [3.2508800545495435, -0.3498361050839355], [3.2549892755756202, 0.14644705092779428], [3.269429777042236, 0.3465606634114739], [3.0147066002832337, 0.13524061815175958], [2.8697470754307726, 0.1372217971061337], [3.153536760377613, -0.1735954819644789], [2.9196684366844967, 0.3928049158589175], [2.7741725972722118, 0.3174951846161572], [2.6246826297124923, -0.24390228004865894], [2.6465821469347914, 0.10099189983994017], [2.9894162122480594, -0.018957683361648596], [3.272372524749967, -0.3182278360178188], [2.7845186439092413, 0.2330590136221214], [3.3241642272982816, 0.19936259900087505], [2.637239228934715, -0.14945730545751948], [3.0569695904528102, -0.14168723128445732], [3.136663061100583, 0.06909264349911241], [2.8432459255435365, 0.23629856040733016], [2.9964783703067464, 0.36366748306186203], [3.0352668093513127, 0.45785855252148266], [2.7977989505847995, 0.40416634824352954], [2.8036737928213635, -0.22195768515214714], [2.8633099806245417, -0.04172977987118372], [2.8053201565064385, 0.4083142710359155], [3.2937899779903526, 0.02502122682780065], [3.3961471519685023, 0.11548942625563141], [2.8980582533006434, 0.16170524524275945], [3.052396561374095, -0.14751345927434364], [3.4808088724687383, 0.10776147953507777], [3.2320208834063897, 0.13902064532866526], [2.8723401627154423, -0.0618408361738482], [3.0110168800587758, -0.1975882364041726], [3.4435751747516044, 0.17336389672014899], [3.123770891508169, 0.218750700325495], [3.233468416647203, 0.39261047141457234], [3.3088259360819237, -0.02404211923635316], [3.2911137049215187, 0.19993310691994767], [2.9839675824264753, 0.34622554985083004], [2.7681799597580254, 0.37527818718611217], [2.659068813259608, -0.30485871211723553], [2.681049215993094, -0.05365454362877191], [3.3554716203575654, 0.04702582517075264], [2.968515135235644, 0.10167015360697616], [3.0281443502162673, 0.25745990983509415], [2.7522164367568043, -0.05010853276984887], [2.848107514587062, 0.28789197706160663], [2.689297538332607, 0.057032666174576904], [3.251593036340039, -0.006296478982475766], [2.8623357938003884, -0.33271560228443925], [2.9021321837186864, -0.034216129493711514], [2.8783397935406203, -0.20830490016032274], [3.37052245934321, -0.17867832853970655], [3.256738044521217, -0.14306882122389006], [3.051519849699072, -0.37063523405290044], [2.586013195778654, 0.13251703170846652], [2.8092383522372564, -0.3141947031835265], [3.070372515141633, 0.029622219888343418], [3.169710799172379, -0.19918120307397788], [2.6836256482768492, -0.2608653430820558], [2.985741891558439, -0.41565693178489244], [2.5737435060748637, -0.1474698365372057], [3.085649617057237, -0.47876543660040344], [3.4731902976084563, -0.069047382669738], [3.100161356611954, 0.23655154109851398], [3.0371434142328897, 0.27078625809072426], [3.449409419239475, 0.13355750906227024], [2.6647132454964297, 0.2932420879849981], [2.8122583414926825, 0.40098434255985227], [3.2520531486756967, 0.21151280977179407], [3.3970822388918718, -0.024129974958073622], [2.922083319441422, 0.1404386700531341], [3.2895817368399043, 0.3959636346306004], [3.073212126649572, -0.003508802617335682], [3.4527549459670466, -0.13271925061247078], [3.478178772683139, 0.09114978420919956], [2.893451017778235, -0.4373461986385599], [-2.984661213344486, 0.3875116986004827], [-2.611250198500886, -0.10896780970899189], [-2.83778554430435, -0.339613851311361], [-3.186936813845085, 0.1508338183285309], [-2.7775126048753775, 0.22246292314371108], [-2.8693125105494026, -0.34663681920880113], [-3.301292170394524, 0.2812956497152523], [-2.863555961893672, 0.005846716286817034], [-3.1400519769700734, 0.16912627936262317], [-3.3579728826709716, 0.18339066269473084], [-2.9794415309707456, 0.371048505060653], [-2.9619734917349487, -0.4162590320591322], [-3.1916434843484494, 0.3930596800257602], [-2.739676519020144, -0.2746237164651906], [-3.477287708280446, 0.08794583763286529], [-2.641179337577712, -0.24656055869765633], [-3.192958828685941, -0.405964Line truncated
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+76 -44
View File
@@ -1,8 +1,12 @@
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from random import sample
from random import sample, shuffle
from generate_points import get_random_point
import numpy as np
import json
METHODS = ['forgy', 'random_partition']
def get_color(i):
@@ -49,11 +53,12 @@ def plot_kmeans(all_data, k):
cluster_scatters = []
centroids, clusters = all_data[0]
for key in clusters:
lst_x, lst_y = zip(*clusters[key])
lst_x = list(lst_x)
lst_y = list(lst_y)
color = get_color(key/k)
cluster_scatters.append(ax.scatter(lst_x, lst_y, color=color))
if clusters[key]:
lst_x, lst_y = zip(*clusters[key])
lst_x = list(lst_x)
lst_y = list(lst_y)
cluster_scatters.append(ax.scatter(lst_x, lst_y, color=color))
centroid_scatters.append(ax.scatter([centroids[key][0]], [
centroids[key][1]], color=color, marker='X'))
@@ -77,10 +82,16 @@ def calc_length(a, b):
return (b[0]-a[0])**2+(b[1]-a[1])**2
def init_centroids(data, k, method='forgy'): #TODO: Add k-means++ and Random Partition
def init_centroids(data, k, method='forgy'): # TODO: Add k-means++ and Random Partition
match method:
case 'forgy':
return sample(data, k)
case 'random_partition':
shuffled = list(data)
shuffle(shuffled)
div = len(shuffled)/k
partition = [shuffled[int(round(div*i)):int(round(div*(i+1)))] for i in range(k)]
return [np.mean(prt, axis=0) for prt in partition]
case _:
raise NotImplementedError(
f'method {method} is not implemented yet')
@@ -91,9 +102,9 @@ def calc_error(centroids, clusters, k):
for i in range(k):
cluster = np.array(clusters[i])
centroid = np.array([centroids[i] for _ in range(len(cluster))])
errors = centroid - cluster
errors = cluster - centroid
squared_errors.append([e**2 for e in errors])
return sum([np.mean(err) for err in squared_errors])
return sum([np.mean(err) if err else 0 for err in squared_errors])
def plot_error_data(error_data):
@@ -112,28 +123,29 @@ def plot_error_data(error_data):
plt.show()
def main():
for get_data in [get_data1, get_data2]:
data = get_data()
def print_stats(k, data):
print('='*20)
print(f'k={k}')
errs = [x[1] for x in data]
m = np.mean(errs)
std = np.std(errs)
min_err = np.min(errs)
lst_empty = [sum([1 for cluster in centroids_with_clusters[1] if not cluster]) for centroids_with_clusters,_ in data]
print(lst_empty)
def main(datas):
# for get_data in [get_data1, get_data2]:
# data = get_data()
for data in datas:
plot_data(data)
kmeans_data = {}
for k in range(2, 21):
kmeans_with_err = []
for _ in range(100):
all_data = []
centroids = init_centroids(data, k)
clusters = {}
for i in range(k):
clusters[i] = []
for point in data:
lengths = [calc_length(c, point) for c in centroids]
index_min = np.argmin(lengths)
clusters[index_min].append(point)
all_data.append((list(centroids), clusters))
for method in METHODS:
kmeans_data = {}
for k in [20]: # range(2, 21):
kmeans_with_err = []
for _ in range(100):
for key in clusters:
if clusters[key]:
centroids[key] = np.mean(clusters[key], axis=0)
centroids_with_clusters = []
centroids = init_centroids(data, k, method=method)
clusters = {}
for i in range(k):
clusters[i] = []
@@ -141,22 +153,42 @@ def main():
lengths = [calc_length(c, point) for c in centroids]
index_min = np.argmin(lengths)
clusters[index_min].append(point)
all_data.append((list(centroids), clusters))
if all([all(np.isclose(all_data[-1][0][i], all_data[-2][0][i])) for i in range(k)]):
break
err = calc_error(centroids, clusters, k)
kmeans_with_err.append((all_data, err))
min_err = kmeans_with_err[0][1]
kmeans = kmeans_with_err[0][0]
for temp_kmeans, err in kmeans_with_err:
if err < min_err:
min_err = err
kmeans = temp_kmeans
kmeans_data[k] = (kmeans, min_err)
plot_kmeans(kmeans, k)
error_data = [[i, kmeans_data[i][1]] for i in range(2, 21, 2)]
plot_error_data(error_data)
centroids_with_clusters.append((list(centroids), clusters))
for _ in range(100):
for key in clusters:
if clusters[key]:
centroids[key] = np.mean(clusters[key], axis=0)
clusters = {}
for i in range(k):
clusters[i] = []
for point in data:
lengths = [calc_length(c, point)
for c in centroids]
index_min = np.argmin(lengths)
clusters[index_min].append(point)
centroids_with_clusters.append(
(list(centroids), clusters))
if all([all(np.isclose(centroids_with_clusters[-1][0][i], centroids_with_clusters[-2][0][i])) for i in range(k)]):
break
err = calc_error(centroids, clusters, k)
kmeans_with_err.append((centroids_with_clusters, err))
print_stats(k, [(iterations[-1],err) for iterations, err in kmeans_with_err])
min_err = kmeans_with_err[0][1]
kmeans = kmeans_with_err[0][0]
for temp_kmeans, err in kmeans_with_err:
if err < min_err:
min_err = err
kmeans = temp_kmeans
kmeans_data[k]=(kmeans, min_err)
plot_kmeans(kmeans, k)
#error_data = [[i, kmeans_data[i][1]] for i in range(2, 21, 2)]
#plot_error_data(error_data)
if __name__ == '__main__':
main()
datas = []
with open('data1.json', 'r') as d:
datas.append(json.loads(d.read()))
with open('data2.json', 'r') as d:
datas.append(json.loads(d.read()))
main(datas)