FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-benchmarks/pairwise/pairwise_python.py at master · numfocus/python-benchmarks · GitHub
numfocus
python-benchmarks
Repository navigation
Code
Issues
7
(7)
Pull requests
2
(2)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-benchmarks
/
pairwise
/
pairwise_python.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
45 lines (34 loc) · 1.24 KB
Breadcrumbs
python-benchmarks
/
pairwise
/
pairwise_python.py
Copy path
File metadata and controls
45 lines (34 loc) · 1.24 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
# Authors: Jake Vanderplas, Alex Rubinsteyn, Olivier Grisel
# License: MIT
import
numpy
as
np
def
pairwise_python_nested_for_loops
(
data
):
n_samples
,
n_features
=
data
.
shape
distances
=
np
.
empty
((
n_samples
,
n_samples
),
dtype
=
data
.
dtype
)
#"omp parallel for private(j, d, k, tmp)"
for
i
in
range
(
n_samples
):
for
j
in
range
(
n_samples
):
d
=
0.0
for
k
in
range
(
n_features
):
tmp
=
data
[
i
,
k
]
-
data
[
j
,
k
]
d
+=
tmp
*
tmp
distances
[
i
,
j
]
=
np
.
sqrt
(
d
)
return
distances
def
pairwise_python_inner_numpy
(
data
):
n_samples
=
data
.
shape
[
0
]
result
=
np
.
empty
((
n_samples
,
n_samples
),
dtype
=
data
.
dtype
)
for
i
in
xrange
(
n_samples
):
for
j
in
xrange
(
n_samples
):
result
[
i
,
j
]
=
np
.
sqrt
(
np
.
sum
((
data
[
i
, :]
-
data
[
j
, :])
**
2
))
return
result
def
pairwise_python_broadcast_numpy
(
data
):
return
np
.
sqrt
(((
data
[:,
None
, :]
-
data
)
**
2
).
sum
(
axis
=
2
))
def
pairwise_python_numpy_dot
(
data
):
X_norm_2
=
(
data
**
2
).
sum
(
axis
=
1
)
dists
=
np
.
sqrt
(
2
*
X_norm_2
-
np
.
dot
(
data
,
data
.
T
))
return
dists
benchmarks
=
(
pairwise_python_nested_for_loops
,
pairwise_python_inner_numpy
,
pairwise_python_broadcast_numpy
,
pairwise_python_numpy_dot
,
)
Back
|
FazBrowse Home
|
New Git URL