Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lazy-programmer-courses
/
cluster-analysis-and-unsupervised-learning
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e1699fa5
authored
Dec 14, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added visualizing class
parents
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
0 deletions
soft_k_means_9.py
visualizing_10.py
soft_k_means_9.py
0 → 100644
View file @
e1699fa5
import
numpy
as
np
import
matplotlib.pyplot
as
plt
def
d
(
u
,
v
):
# squared difference
diff
=
u
-
v
return
diff
.
dot
(
diff
)
def
cost
(
X
,
R
,
M
):
cost
=
0
for
k
in
range
(
len
(
M
)):
diff
=
X
-
M
[
k
]
sq_distances
=
(
diff
*
diff
)
.
sum
(
axis
=
1
)
cost
+=
(
R
[:,
k
]
*
sq_distances
)
.
sum
()
return
cost
def
plot_k_means
(
X
,
K
,
max_iter
=
20
,
beta
=
1.0
,
show_plots
=
True
):
N
,
D
=
X
.
shape
M
=
np
.
zeros
((
K
,
D
))
# means
exponents
=
np
.
empty
((
N
,
K
))
for
k
in
range
(
K
):
M
[
k
]
=
X
[
np
.
random
.
choice
(
N
)]
costs
=
np
.
zeros
(
max_iter
)
for
i
in
range
(
max_iter
):
for
k
in
range
(
K
):
for
n
in
range
(
N
):
exponents
[
n
,
k
]
=
np
.
exp
(
-
beta
*
d
(
M
[
k
],
X
[
n
]))
R
=
exponents
/
exponents
.
sum
(
axis
=
1
,
keepdims
=
True
)
for
k
in
range
(
K
):
M
[
k
]
=
R
[:,
k
]
.
dot
(
X
)
/
R
[:,
k
]
.
sum
()
costs
[
i
]
=
cost
(
X
,
R
,
M
)
if
i
>
0
:
if
np
.
abs
(
costs
[
i
]
-
costs
[
i
-
1
])
<
1e-5
:
break
if
show_plots
:
plt
.
plot
(
costs
)
plt
.
title
(
"Costs"
)
plt
.
show
()
random_colors
=
np
.
random
.
random
((
K
,
3
))
colors
=
R
.
dot
(
random_colors
)
plt
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
colors
)
plt
.
show
()
return
M
,
R
def
main
():
D
=
2
s
=
4
mu1
=
np
.
array
([
0
,
0
])
mu2
=
np
.
array
([
s
,
s
])
mu3
=
np
.
array
([
0
,
s
])
N
=
900
X
=
np
.
zeros
((
N
,
D
))
X
[:
300
,:]
=
np
.
random
.
randn
(
300
,
D
)
+
mu1
X
[
300
:
600
,:]
=
np
.
random
.
randn
(
300
,
D
)
+
mu2
X
[
600
:,:]
=
np
.
random
.
randn
(
300
,
D
)
+
mu3
plt
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
'green'
,
alpha
=
0.5
)
plt
.
show
()
K
=
3
plot_k_means
(
X
,
K
)
K
=
5
plot_k_means
(
X
,
K
,
max_iter
=
30
)
K
=
5
plot_k_means
(
X
,
K
,
max_iter
=
30
,
beta
=
0.3
)
if
__name__
==
'__main__'
:
main
()
\ No newline at end of file
visualizing_10.py
0 → 100644
View file @
e1699fa5
import
numpy
as
np
import
matplotlib.pyplot
as
plt
def
d
(
u
,
v
):
# squared difference
diff
=
u
-
v
return
diff
.
dot
(
diff
)
def
cost
(
X
,
R
,
M
):
cost
=
0
for
k
in
range
(
len
(
M
)):
diff
=
X
-
M
[
k
]
sq_distances
=
(
diff
*
diff
)
.
sum
(
axis
=
1
)
cost
+=
(
R
[:,
k
]
*
sq_distances
)
.
sum
()
return
cost
def
plot_k_means
(
X
,
K
,
max_iter
=
20
,
beta
=
1.0
,
show_plots
=
True
):
N
,
D
=
X
.
shape
M
=
np
.
zeros
((
K
,
D
))
# means
R
=
np
.
ones
((
N
,
K
))
/
K
for
k
in
range
(
K
):
M
[
k
]
=
X
[
np
.
random
.
choice
(
N
)]
grid_width
=
5
grid_height
=
max_iter
/
grid_width
random_colors
=
np
.
random
.
random
((
K
,
3
))
plt
.
figure
()
costs
=
np
.
zeros
(
max_iter
)
for
i
in
range
(
max_iter
):
colors
=
R
.
dot
(
random_colors
)
plt
.
subplot
(
grid_width
,
grid_height
,
i
+
1
)
plt
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
colors
)
for
k
in
range
(
K
):
for
n
in
range
(
N
):
R
[
n
,
k
]
=
np
.
exp
(
-
beta
*
d
(
M
[
k
],
X
[
n
]))
/
np
.
sum
(
np
.
exp
(
-
beta
*
d
(
M
[
j
],
X
[
n
]))
for
j
in
range
(
K
)
)
for
k
in
range
(
K
):
M
[
k
]
=
R
[:,
k
]
.
dot
(
X
)
/
R
[:,
k
]
.
sum
()
costs
[
i
]
=
cost
(
X
,
R
,
M
)
if
i
>
0
:
if
np
.
abs
(
costs
[
i
]
-
costs
[
i
-
1
])
<
1e-5
:
break
plt
.
show
()
return
M
,
R
def
main
():
D
=
2
s
=
4
mu1
=
np
.
array
([
0
,
0
])
mu2
=
np
.
array
([
s
,
s
])
mu3
=
np
.
array
([
0
,
s
])
N
=
900
X
=
np
.
zeros
((
N
,
D
))
X
[:
300
,:]
=
np
.
random
.
randn
(
300
,
D
)
+
mu1
X
[
300
:
600
,:]
=
np
.
random
.
randn
(
300
,
D
)
+
mu2
X
[
600
:,:]
=
np
.
random
.
randn
(
300
,
D
)
+
mu3
plt
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
'green'
,
alpha
=
0.5
)
plt
.
show
()
K
=
3
plot_k_means
(
X
,
K
)
K
=
5
plot_k_means
(
X
,
K
,
max_iter
=
30
)
K
=
5
plot_k_means
(
X
,
K
,
max_iter
=
30
,
beta
=
0.3
)
if
__name__
==
'__main__'
:
main
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment