Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scikit-learn
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ian Johnson
scikit-learn
Commits
5144bb66
Commit
5144bb66
authored
14 years ago
by
James Bergstra
Committed by
Gael Varoquaux
14 years ago
Browse files
Options
Downloads
Patches
Plain Diff
k-means - added optional parameters "precompute_distances" and "x_squared_norms"
parent
0552ea05
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
scikits/learn/cluster/k_means_.py
+5
-12
5 additions, 12 deletions
scikits/learn/cluster/k_means_.py
with
5 additions
and
12 deletions
scikits/learn/cluster/k_means_.py
+
5
−
12
View file @
5144bb66
...
...
@@ -251,7 +251,7 @@ def _m_step(x, z ,k):
return
centers
def
_e_step
(
x
,
centers
):
def
_e_step
(
x
,
centers
,
precompute_distances
=
True
,
x_squared_norms
=
None
):
"""
E step of the K-means EM algorithm
Computation of the input-to-cluster assignment
...
...
@@ -276,22 +276,15 @@ def _e_step(x, centers):
n_samples
=
x
.
shape
[
0
]
k
=
centers
.
shape
[
0
]
there_is_memory_to_compute_distances_matrix
=
True
if
there_is_memory_to_compute_distances_matrix
:
distances
=
(
(
x
**
2
).
sum
(
axis
=
1
)
+
(
centers
**
2
).
sum
(
axis
=
1
).
reshape
((
k
,
1
))
-
2
*
np
.
dot
(
centers
,
x
.
T
))
# distances is a matrix of shape (k, n_samples)
if
precompute_distances
:
distances
=
all_pairs_l2_distance_squared
(
centers
,
x
,
x_squared_norms
)
z
=
-
np
.
ones
(
n_samples
).
astype
(
np
.
int
)
mindist
=
np
.
infty
*
np
.
ones
(
n_samples
)
for
q
in
range
(
k
):
if
there_is_memory_to_
compute_distances
_matrix
:
if
pre
compute_distances
:
dist
=
distances
[
q
]
else
:
dist
=
np
.
sum
((
x
-
centers
[
q
])
**
2
,
1
)
dist
=
np
.
sum
((
x
-
centers
[
q
])
**
2
,
axis
=
1
)
z
[
dist
<
mindist
]
=
q
mindist
=
np
.
minimum
(
dist
,
mindist
)
inertia
=
mindist
.
sum
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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