Skip to content
Snippets Groups Projects
Commit feb5a12f authored by Franka Ludig's avatar Franka Ludig
Browse files

Notebook hinzugefügt

parents
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:chicken-minneapolis tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
```
%% Cell type:markdown id:modified-bookmark tags:
## My Dataset
%% Cell type:code id:chicken-marshall tags:
``` python
rng = np.random.default_rng(seed=1)
x = 7 + rng.random(20)
x = np.append(x, 5 + rng.random(20))
y = 5 + rng.random(20)
y = np.append(y, 4 + rng.random(20))
plt.plot(x, y, '.')
data = np.stack((x, y), axis=1)
data_1D = np.append(x, y)
# print(data)
```
%% Output
%% Cell type:markdown id:prepared-republic tags:
# Manual K-Means
%% Cell type:code id:generic-dating tags:
``` python
# Evaluate k
ks = np.arange(0, 15)
ks = [2]
for k in ks:
cs = data[0:k].copy()
terminate = False
last_cog = [np.array([0, 0]) for i in range(k)]
while not terminate:
dist = np.stack([[np.linalg.norm(c-d) for d in data] for c in cs], axis=1)
z = np.array([np.argmin(d) for d in dist])
df = [data[z == i] for i in range(k)]
current_cog = [np.sum(d, axis=0)/len(d) for d in df]
diff_cog = np.sum(np.abs([last - current for last, current in zip(last_cog, current_cog)]))
last_cog = current_cog
print(diff_cog)
cs = current_cog
if diff_cog < 0.3:
terminate = True
plt.scatter(df[0][:, 0], df[0][:, 1], color='green')
plt.scatter(df[1][:, 0], df[1][:, 1], color='pink')
# print(current_cog)
```
%% Output
25.16143318862738
2.0092105896679273
0.0
<matplotlib.collections.PathCollection at 0x12cd0ee80>
%% Cell type:markdown id:czech-romance tags:
# K-Means with Scikit-Learn
%% Cell type:code id:saved-scope tags:
``` python
km = KMeans(n_clusters=2)
data_predicted = km.fit_predict(data)
df1 = data[data_predicted == 0]
df2 = data[data_predicted == 1]
#print(df2)
plt.scatter(df1[:, 0], df1[:, 1], color='green')
plt.scatter(df2[:, 0], df2[:, 1], color='pink')
```
%% Output
[[5.75036467 4.27404839]
[5.28040876 4.00709183]
[5.48519097 4.6457209 ]
[5.9807372 4.71990938]
[5.96165719 4.83556922]
[5.72478994 4.28187783]
[5.54122686 4.21521817]
[5.2768912 4.63933138]
[5.16065201 4.80505483]
[5.96992541 4.96367087]
[5.51606859 4.15052483]
[5.11586561 4.48221239]
[5.62348976 4.89471586]
[5.77668311 4.42271691]
[5.6130033 4.58950206]
[5.9172977 4.02449068]
[5.03959288 4.67345989]
[5.52858926 4.91908862]
[5.45933588 4.82682533]
[5.06234958 4.88552027]]
<matplotlib.collections.PathCollection at 0x12cd73070>
%% Cell type:code id:partial-faculty tags:
``` python
```
%% Cell type:code id:included-county tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment