Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
512148cf
Commit
512148cf
authored
May 28, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
StatRed ass4: Added some comments.
parent
f6eeea9a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
2 deletions
+5
-2
statred/ass4/k-means.py
statred/ass4/k-means.py
+5
-2
No files found.
statred/ass4/k-means.py
View file @
512148cf
from
pylab
import
loadtxt
,
array
,
scatter
,
figure
,
show
,
mean
,
argmin
,
\
append
,
savefig
from
random
import
random
,
seed
,
randint
from
random
import
random
,
seed
from
sys
import
argv
,
exit
def
init
(
X
,
k
):
...
...
@@ -9,18 +9,21 @@ def init(X, k):
def
init_pp
(
X
,
k
):
"""Use the k-means++ algorithm to find initial means."""
# Choose first
center
at random
# Choose first
mean
at random
N
=
X
.
shape
[
0
]
indices
=
[
int
(
N
*
random
())]
m
=
[
X
[
indices
[
0
]]]
# Initial distances
D
=
[((
x
-
m
[
0
])
**
2
).
sum
()
for
x
in
X
]
while
len
(
m
)
<
k
:
# Find new best mean
best_sum
=
new
=
-
1
for
i
in
range
(
N
):
if
i
not
in
indices
:
Dsum
=
sum
([
min
(
D
[
j
],
((
X
[
j
]
-
X
[
i
])
**
2
).
sum
())
for
j
in
range
(
N
)])
if
best_sum
==
-
1
or
best_sum
<
Dsum
:
best_sum
,
new
=
Dsum
,
i
# Add new mean and update distances
m
.
append
(
X
[
new
])
indices
.
append
(
new
)
D
=
[
min
(
D
[
i
],
((
X
[
i
]
-
X
[
new
])
**
2
).
sum
())
for
i
in
range
(
N
)]
...
...
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