Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cmda3634_materials
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
srigavili
cmda3634_materials
Commits
8c135a75
Commit
8c135a75
authored
1 year ago
by
Jason R Wilson
Browse files
Options
Downloads
Patches
Plain Diff
visuals
parent
d077f6a4
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
FSA04/kcenter.py
+28
-0
28 additions, 0 deletions
FSA04/kcenter.py
with
28 additions
and
0 deletions
FSA04/kcenter.py
0 → 100644
+
28
−
0
View file @
8c135a75
import
sys
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# load the points
data
=
np
.
loadtxt
(
sys
.
stdin
,
skiprows
=
1
)
# set the aspect ratio to equal
plt
.
gca
().
set_aspect
(
'
equal
'
)
# plot the centers (if additional command line arguments present)
if
(
len
(
sys
.
argv
)
-
2
>
0
):
ctr
=
np
.
array
([
int
(
s
)
for
s
in
sys
.
argv
[
2
:]])
dist_sq
=
np
.
zeros
((
len
(
ctr
),
len
(
data
)))
for
i
in
range
(
len
(
ctr
)):
dist_sq
[
i
]
=
np
.
sum
((
data
-
data
[
ctr
[
i
]])
*
(
data
-
data
[
ctr
[
i
]]),
axis
=
1
)
clu
=
np
.
argmin
(
dist_sq
,
axis
=
0
)
ex
=
np
.
argmax
(
np
.
min
(
dist_sq
,
axis
=
0
))
cost_sq
=
np
.
max
(
np
.
min
(
dist_sq
,
axis
=
0
))
plt
.
scatter
(
data
[:,
0
],
data
[:,
1
],
c
=
clu
,
cmap
=
"
tab10
"
,
s
=
10
,
alpha
=
0.5
)
plt
.
scatter
(
data
[
ctr
,
0
],
data
[
ctr
,
1
],
c
=
range
(
len
(
ctr
)),
cmap
=
"
tab10
"
,
s
=
100
)
plt
.
scatter
(
data
[
ex
,
0
],
data
[
ex
,
1
],
s
=
50
,
facecolors
=
'
none
'
,
edgecolors
=
'
black
'
)
plt
.
title
(
'
Cost = %g
'
%
np
.
sqrt
(
cost_sq
))
else
:
plt
.
scatter
(
data
[:,
0
],
data
[:,
1
],
s
=
10
,
color
=
'
black
'
)
#save the plot as an image
plt
.
savefig
(
sys
.
argv
[
1
])
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment