Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TP-Clustering
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
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IA et Machine Learning
TP-Clustering
Commits
1976fb11
Commit
1976fb11
authored
1 year ago
by
thibault.capt
Browse files
Options
Downloads
Patches
Plain Diff
add dataframe for variance
parent
8ce122ef
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
iris.py
+19
-8
19 additions, 8 deletions
iris.py
with
19 additions
and
8 deletions
iris.py
+
19
−
8
View file @
1976fb11
import
pandas
as
pd
import
numpy
as
np
from
collections
import
Counter
import
matplotlib.pyplot
as
plt
def
manhattan_distance
(
x1
:
np
.
ndarray
,
x2
:
np
.
ndarray
)
->
float
:
distance
=
0
...
...
@@ -26,6 +26,7 @@ if __name__ == '__main__':
centroids
=
X
[
np
.
random
.
choice
(
X
.
shape
[
0
],
k
,
replace
=
False
)]
# Nombre maximal d'itérations
max_iter
=
100
total_variances
=
[]
# Algorithme K-Means
for
i
in
range
(
max_iter
):
...
...
@@ -45,15 +46,17 @@ if __name__ == '__main__':
if
len
(
clusters
[
j
])
>
0
:
centroids
[
j
]
=
np
.
mean
(
clusters
[
j
],
axis
=
0
)
# Calculer la variance totale à cette itération
total_variance
=
0.0
for
j
in
range
(
k
):
for
point
in
clusters
[
j
]:
total_variance
+=
manhattan_distance
(
point
,
centroids
[
j
])
**
2
total_variances
.
append
(
total_variance
)
# Convergence ?
if
np
.
all
(
old_centroids
==
centroids
):
break
# Calculer la variance totale
total_variance
=
0.0
for
cluster_index
,
cluster_points
in
enumerate
(
clusters
):
cluster_center
=
centroids
[
cluster_index
]
for
point
in
cluster_points
:
total_variance
+=
manhattan_distance
(
point
,
cluster_center
)
**
2
# Calculer le taux de classification par cluster et la classe majoritaire par cluster
cluster_classifications
=
{}
...
...
@@ -71,3 +74,11 @@ if __name__ == '__main__':
for
cluster_index
in
range
(
k
):
print
(
f
'
Cluster
{
cluster_index
+
1
}
- Taux de classification :
{
cluster_classifications
[
cluster_index
]
}
'
)
print
(
f
'
Cluster
{
cluster_index
+
1
}
- Classe majoritaire :
{
cluster_majority_class
[
cluster_index
]
}
'
)
# Afficher les variances totales à chaque itération
plt
.
plot
(
range
(
1
,
len
(
total_variances
)
+
1
),
total_variances
,
marker
=
'
o
'
)
plt
.
xlabel
(
'
Itération
'
)
plt
.
ylabel
(
'
Variance totale
'
)
plt
.
title
(
'
Variance totale à chaque itération de K-Means
'
)
plt
.
grid
(
True
)
plt
.
show
()
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