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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Muniz Silva Samuel
Image classification
Commits
cdb0f72c
Commit
cdb0f72c
authored
Nov 8, 2022
by
Muniz Silva Samuel
Browse files
Options
Downloads
Patches
Plain Diff
final commit
parent
f9861327
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
knn.py
+41
-39
41 additions, 39 deletions
knn.py
with
41 additions
and
39 deletions
knn.py
+
41
−
39
View file @
cdb0f72c
...
...
@@ -10,16 +10,18 @@ import matplotlib.pyplot as plt
def
distance_matrix
(
data_test
,
data_train
):
dists
=
np
.
array
([
np
.
sum
((
data_train
-
l
)
**
2
,
axis
=
1
)
**
.
5
for
l
in
data_test
])
"""
Takes the matrix data_test and data_train. It returning a 2d array(N,M) such that dists[i,j] represents
the distance between the i-th data_test row and the j-th data_train row
"""
dists
=
np
.
array
([
np
.
sum
((
data_train
-
l
)
**
2
,
axis
=
1
)
**
0.5
for
l
in
data_test
])
return
dists
#receives a 2d array data_train(M,k) and a data_test (N,k),
#returning a 2d array(N,M) such that dists[i,j] represents
#the distance between the i-th data_test row and the j-th data_train row
#in resume, each column represent a distance of a training point to all other
def
knn_predict
(
dists
,
labels_train
,
k
):
"""
Take the matrix of distances dists, the labels for training and k nearest neighbor
It returns the classification given by the module KNN.
"""
# classif = np.array(0)
print
(
labels_train
[:
20
])
print
(
labels_train
.
size
)
...
...
@@ -37,35 +39,35 @@ def knn_predict(dists , labels_train , k):
return
classif
def
evaluate_knn
(
data_train
,
labels_train
,
data_test
,
labels_test
,
k
):
classif
=
np
.
array
(
knn_predict
(
distance_matrix
(
data_train
,
data_test
)
,
labels_train
,
k
))
def
evaluate_knn
(
data_train
,
labels_train
,
data_test
,
labels_test
,
k
):
"""
Receives the datas ans labels for training and teste and k nearest neighbor.
It retuns the accuracy of the KNN module
"""
classif
=
np
.
array
(
knn_predict
(
distance_matrix
(
data_train
,
data_test
),
labels_train
,
k
)
)
result
=
np
.
array
(
classif
==
labels_test
)
acc
=
np
.
count_nonzero
(
result
)
/
np
.
size
(
result
)
return
acc
*
100
datas
,
labels
=
read_cifar_batch
(
'
data_batch_1
'
)
print
(
datas
.
shape
,
labels
.
shape
)
datas
,
labels
=
read_cifar_batch
(
"
data_batch_1
"
)
dataTrain
,
dataTest
,
labelsTrain
,
labelsTest
=
split_dataset
(
datas
,
labels
)
print
(
dataTrain
.
shape
,
dataTest
.
shape
,
labelsTrain
.
shape
)
distanceMatrix
=
distance_matrix
(
dataTrain
,
dataTest
)
print
(
distanceMatrix
.
shape
)
print
()
result
=
[]
for
i
in
range
(
1
,
21
):
result
=
np
.
append
(
result
,
evaluate_knn
(
dataTrain
,
labelsTrain
,
dataTest
,
labelsTest
,
i
))
result
=
np
.
append
(
result
,
evaluate_knn
(
dataTrain
,
labelsTrain
,
dataTest
,
labelsTest
,
i
)
)
x
=
np
.
arange
(
1
,
21
)
# plot
ting
# plot
the graph of (Accuracy) x k
plt
.
title
(
"
Plot graph
"
)
plt
.
xlabel
(
"
K neighbors
"
)
plt
.
ylabel
(
"
Accuracy %
"
)
plt
.
plot
(
x
,
result
,
color
=
"
red
"
)
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