Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Image Classification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
El Alimi Sara
Image Classification
Commits
efa3fceb
Commit
efa3fceb
authored
1 year ago
by
selalimi
Browse files
Options
Downloads
Patches
Plain Diff
Update knn file
parent
6f171b98
Loading
Loading
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
knn.py
+27
-30
27 additions, 30 deletions
knn.py
with
27 additions
and
30 deletions
knn.py
+
27
−
30
View file @
efa3fceb
...
@@ -5,16 +5,15 @@ import matplotlib.pyplot as plt
...
@@ -5,16 +5,15 @@ import matplotlib.pyplot as plt
import
plotly.graph_objects
as
go
import
plotly.graph_objects
as
go
# Commentaire global expliquant le but du code
'''
Here is the code to compute the L2 Euclidean distance matrix and predict labels using k-nearest neighbors:
'''
# Create distance Matrix
# Create distance Matrix
'''
'''
Arguments:
Arguments:
-
Deux
matrices.
-
Two
matrices.
Returns:
Returns:
dists
:
la matrice de distances euclidiennes L2
.
dists:
the L2 Euclidean distance matrix
.
La
computation
de cette
f
o
nction
doit être effectuée uniquement avec des
manipulations
de matrices
.
The
computation
of this
f
u
nction
should be done solely through matrix
manipulations.
'''
'''
def
distance_matrix
(
X
,
Y
):
def
distance_matrix
(
X
,
Y
):
XX
=
np
.
sum
(
X
**
2
,
axis
=
1
,
keepdims
=
True
)
XX
=
np
.
sum
(
X
**
2
,
axis
=
1
,
keepdims
=
True
)
...
@@ -26,12 +25,12 @@ def distance_matrix(X, Y):
...
@@ -26,12 +25,12 @@ def distance_matrix(X, Y):
# KNN predict
# KNN predict
'''
'''
Arguments:
Arguments:
-dists
:
la matrice de distances entre l
'
ensemble d
'
entraînement et l
'
ensemble d
e test.
-
dists:
the distance matrix between the training set and th
e test
set
.
-labels_train
:
les étiquettes d
'
entraînement
.
-
labels_train:
training labels
.
- k
:
l
e n
o
mb
re de voisin
s.
- k:
th
e n
u
mb
er of neighbor
s.
Returns:
Returns:
-
Les étiquettes prédites pour les élé
ments
de
data_test.
-
Predicted labels for the ele
ments
in
data_test.
'''
'''
def
knn_predict
(
dists
,
labels_train
,
k
):
def
knn_predict
(
dists
,
labels_train
,
k
):
n_test
=
dists
.
shape
[
0
]
n_test
=
dists
.
shape
[
0
]
...
@@ -46,15 +45,14 @@ def knn_predict(dists, labels_train, k):
...
@@ -46,15 +45,14 @@ def knn_predict(dists, labels_train, k):
'''
Here is the code to evaluate k-nearest neighbors and plot the accuracy as a function of k:
'''
'''
Here is the code to evaluate k-nearest neighbors and plot the accuracy as a function of k:
'''
'''
'''
Arguments:
Arguments:
-data_train
:
les données d
'
entraînement
.
-
data_train:
training data
.
-labels_train
:
les étiquettes
correspond
ante
s.
-
labels_train: correspond
ing label
s.
-data_test
:
les donné
es d
e test
.
-
data_test:
t
es
t
d
ata
.
-labels_test
:
les étiquettes
correspond
ante
s.
-
labels_test: correspond
ing label
s.
-
k : l
e n
o
mb
re de voisin
s.
-
k: th
e n
u
mb
er of neighbor
s.
Returns:
Returns:
-La précision du modèle Knn : le taux de classification entre les valeurs prédites et les observations
- Accuracy of the Knn model: the classification rate between predicted values and actual observations from test data.
réelles des données de test.
'''
'''
def
evaluate_knn
(
data_train
,
labels_train
,
data_test
,
labels_test
,
k
):
def
evaluate_knn
(
data_train
,
labels_train
,
data_test
,
labels_test
,
k
):
dists
=
distance_matrix
(
data_test
,
data_train
)
dists
=
distance_matrix
(
data_test
,
data_train
)
...
@@ -63,13 +61,13 @@ def evaluate_knn(data_train, labels_train, data_test, labels_test, k):
...
@@ -63,13 +61,13 @@ def evaluate_knn(data_train, labels_train, data_test, labels_test, k):
return
accuracy
return
accuracy
# Plot Accuracy of KNN model
# Plot Accuracy of KNN model
'''
The function plots the variation of accuracy with the number of neighbors K.
'''
'''
'''
******La fonction trace la variation de la précision en fonction du nombre de voisins K****
Arguments:
Arguments:
-X_train
:
données d
'
entraînement
-
X_train:
training data.
-y_train
:
étiquettes d
'
entraînement
-
y_train:
training labels.
-X_test
:
donné
es d
e test
-
X_test:
t
es
t
d
ata.
-y_test
:
étiquettes de test
-
y_test:
test labels.
'''
'''
def
plot_KNN
(
X_train
,
y_train
,
X_test
,
y_test
,
max_k
=
20
):
def
plot_KNN
(
X_train
,
y_train
,
X_test
,
y_test
,
max_k
=
20
):
neighbors
=
np
.
arange
(
1
,
max_k
+
1
)
neighbors
=
np
.
arange
(
1
,
max_k
+
1
)
...
@@ -79,4 +77,3 @@ def plot_KNN(X_train, y_train, X_test, y_test, max_k=20):
...
@@ -79,4 +77,3 @@ def plot_KNN(X_train, y_train, X_test, y_test, max_k=20):
plt
.
ylabel
(
'
Accuracy
'
)
plt
.
ylabel
(
'
Accuracy
'
)
plt
.
title
(
'
Variation of Accuracy with K
'
)
plt
.
title
(
'
Variation of Accuracy with K
'
)
plt
.
savefig
(
"
Results/knn.png
"
)
plt
.
savefig
(
"
Results/knn.png
"
)
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