FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mlstatpy/src/mlstatpy/ml/kppv.py at dev-pvalues · sdpython/mlstatpy · GitHub
sdpython
mlstatpy
Repository navigation
Code
Issues
10
(10)
Pull requests
1
(1)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mlstatpy
/
src
/
mlstatpy
/
ml
/
kppv.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
99 lines (83 loc) · 2.62 KB
Breadcrumbs
mlstatpy
/
src
/
mlstatpy
/
ml
/
kppv.py
Copy path
File metadata and controls
99 lines (83 loc) · 2.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
@file
@brief Implements classic k-nn.
"""
import
numpy
import
numpy
.
linalg
from
scipy
.
spatial
.
distance
import
euclidean
class
NuagePoints
:
"""
Définit une classe de nuage de points.
On suppose qu'ils sont définis par une matrice,
chaque ligne est un élément.
"""
def
__init__
(
self
):
"""
constructeur
"""
pass
def
fit
(
self
,
X
,
y
=
None
):
"""
Follows sklearn API.
@param X training set
@param y labels
"""
self
.
nuage
=
X
self
.
labels
=
y
def
kneighbors
(
self
,
X
,
n_neighbors
=
1
,
return_distance
=
True
):
"""
Return the k nearest neighbors.
@param X test set
@param n_neighbors number of neighbors
@param return_distance return distance as well
@return array (dist), array (indices)
"""
if
n_neighbors
!=
1
:
raise
NotImplementedError
(
"Not implemented when n_neighbors != 1."
)
if
not
return_distance
:
raise
NotImplementedError
(
"Not implemented when return_distance is False."
)
dist
=
numpy
.
zeros
(
X
.
shape
[
0
])
ind
=
numpy
.
zeros
(
X
.
shape
[
0
],
dtype
=
numpy
.
int64
)
for
i
in
range
(
X
.
shape
[
0
]):
row
=
X
[
i
, :]
row
.
resize
((
1
,
X
.
shape
[
1
]))
r
=
self
.
ppv
(
row
)
dist
[
i
],
ind
[
i
]
=
r
return
dist
,
ind
@
property
def
shape
(
self
):
"""
Retourne la dimension du nuage.
"""
return
self
.
nuage
.
shape
def
distance
(
self
,
obj1
,
obj2
):
"""
Retourne une distance entre deux éléments.
@param obj1 object 1
@param obj2 object 2
@return distance
"""
return
euclidean
(
obj1
,
obj2
)
def
label
(
self
,
i
):
"""
Retourne le label de l'object d'indice ``i``.
@param i indice
@return label or None if there is no label
"""
return
self
.
label
[
i
]
if
self
.
label
is
not
None
else
None
def
ppv
(
self
,
obj
):
"""
Retourne l'élément le plus proche de obj et sa distance avec obj.
@param obj object
@return ``tuple(dist, index)``
"""
ones
=
numpy
.
ones
((
self
.
nuage
.
shape
[
0
],
1
))
mat
=
ones
@
obj
if
len
(
mat
.
shape
)
==
1
:
mat
.
resize
((
mat
.
shape
[
0
],
1
))
delta
=
self
.
nuage
-
mat
norm
=
numpy
.
linalg
.
norm
(
delta
,
axis
=
1
)
i
=
numpy
.
argmin
(
norm
)
return
norm
[
i
],
i
Back
|
FazBrowse Home
|
New Git URL