Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Images 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
Saidi Aya
Images classification
Commits
eec797e0
Commit
eec797e0
authored
2 years ago
by
Saidi Aya
Browse files
Options
Downloads
Patches
Plain Diff
Update knn.py
parent
0c096b6e
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
knn.py
+7
-3
7 additions, 3 deletions
knn.py
with
7 additions
and
3 deletions
knn.py
+
7
−
3
View file @
eec797e0
...
@@ -4,9 +4,13 @@ import torch
...
@@ -4,9 +4,13 @@ import torch
#Functions
#Functions
def
distance_matrix
(
Y
,
X
):
def
distance_matrix
(
Y
,
X
):
#This function takes as parameters two matrices X and Y
#This function takes as parameters two matrices X and Y
dists
=
np
.
sqrt
(
np
.
sum
(
-
2
*
np
.
multiply
(
X
,
Y
)
+
np
.
multiply
(
Y
,
Y
)
+
np
.
multiply
(
X
,
X
)))
a_2
=
(
Y
**
2
).
sum
(
axis
=
1
)
#dists is the euclidian distance between two matrices
a_2
=
a_2
.
reshape
(
-
1
,
1
)
return
dists
b_2
=
(
X
**
2
).
sum
(
axis
=
1
)
b_2
=
b_2
.
reshape
(
1
,
-
1
)
dist
=
np
.
sqrt
(
a_2
+
b_2
-
2
*
Y
.
dot
(
X
.
T
))
#dist is the euclidian distance between two matrices
return
dist
def
knn_predict
(
dists
,
labels_train
,
k
):
def
knn_predict
(
dists
,
labels_train
,
k
):
#This function takes as parameters: dists (from above), labels_train, and k the number of neighbors
#This function takes as parameters: dists (from above), labels_train, and k the number of neighbors
...
...
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