-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref_1_distances.py
648 lines (477 loc) · 20 KB
/
ref_1_distances.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#-*- coding:utf-8 -*-
"""Utilities to evaluate pairwise distances or metrics between 2
sets of points.
"""
# Authors: Vinnicyus Gracindo <vini.gracindo@gmail.com>
# License: GNU GPL.
import pycuda.autoinit
import pycuda.driver as drv
from pycuda.compiler import SourceModule
from math import sqrt
import numpy
from ref_1_utils import safe_asarray
#Only main Device
MAX_THREADS_PER_BLOCK = \
drv.Device(0).get_attribute(pycuda._driver.device_attribute.MAX_THREADS_PER_BLOCK)
BLOCK_SIZE = int(sqrt(MAX_THREADS_PER_BLOCK))
# Utility Functions
def check_pairwise_arrays(X, Y, dtype=numpy.float32):
""" Set X and Y appropriately and checks inputs
If Y is None, it is set as a pointer to X (i.e. not a copy).
If Y is given, this does not happen.
All distance metrics should use this function first to assert that the
given parameters are correct and safe to use.
Specifically, this function first ensures that both X and Y are arrays,
then checks that they are at least two dimensional while ensuring that
their elements are floats. Finally, the function checks that the size
of the second dimension of the two arrays is equal.
Parameters
----------
X : {array-like}, shape = [n_samples_a, n_features]
Y : {array-like}, shape = [n_samples_b, n_features]
Returns
-------
safe_X : {array-like}, shape = [n_samples_a, n_features]
An array equal to X, guarenteed to be a numpy array.
safe_Y : {array-like}, shape = [n_samples_b, n_features]
An array equal to Y if Y was not None, guarenteed to be a numpy array.
If Y was None, safe_Y will be a pointer to X.
"""
if Y is X or Y is None:
X = Y = safe_asarray(X, dtype=dtype)
else:
X = safe_asarray(X, dtype=dtype)
Y = safe_asarray(Y, dtype=dtype)
if len(X.shape) < 2:
raise ValueError("X is required to be at least two dimensional.")
if len(Y.shape) < 2:
raise ValueError("Y is required to be at least two dimensional.")
if X.shape[1] != Y.shape[1]:
raise ValueError("Incompatible dimension for X and Y matrices: "
"X.shape[1] == %d while Y.shape[1] == %d" % (
X.shape[1], Y.shape[1]))
return X, Y
def euclidean_distances(X, Y=None, inverse=True):
"""
Considering the rows of X (and Y=X) as vectors, compute the
distance matrix between each pair of vectors.
Parameters
----------
X : {array-like}, shape = [n_samples_1, n_features]
Y : {array-like}, shape = [n_samples_2, n_features]
inverse: boolean, optional
This routine will return the inverse Euclidean distances instead.
Returns
-------
distances : {array}, shape = [n_samples_1, n_samples_2]
Examples
--------
>>> from pycudadistances.distances import euclidean_distances
>>> X = [[0, 1], [1, 1]]
>>> # distance between rows of X
>>> euclidean_distances(X, X, inverse=False)
array([[ 0., 1.],
[ 1., 0.]])
>>> # get distance to origin
>>> euclidean_distances(X, [[0, 0]], inverse=False)
array([[ 1. ],
[ 1.41421356]])
"""
X, Y = check_pairwise_arrays(X,Y)
solution_rows = X.shape[0]
solution_cols = Y.shape[0]
dx, mx = divmod(solution_cols, BLOCK_SIZE)
dy, my = divmod(X.shape[1], BLOCK_SIZE)
gdim = ( (dx + (mx>0)), (dy + (my>0)) )
solution = numpy.zeros((solution_rows, solution_cols))
solution = solution.astype(numpy.float32)
kernel_code_template = """
#include <math.h>
__global__ void euclidean(float *x, float *y, float *solution) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
int idy = threadIdx.y + blockDim.y * blockIdx.y;
if ( ( idx < %(NCOLS)s ) && ( idy < %(NROWS)s ) ) {
float result = 0.0;
for(int iter = 0; iter < %(NDIM)s; iter++) {
float x_e = x[%(NDIM)s * idx + iter];
float y_e = y[%(NDIM)s * idy + iter];
result += pow((x_e - y_e), 2);
}
int pos = idy + %(NROWS)s * idx;
solution[pos] = sqrt(result);
}
}
"""
kernel_code = kernel_code_template % {
'NCOLS': solution_rows,
'NROWS': solution_cols,
'NDIM': X.shape[1]
}
mod = SourceModule(kernel_code)
func = mod.get_function("euclidean")
func(drv.In(X), drv.In(Y), drv.Out(solution), block=(BLOCK_SIZE, BLOCK_SIZE, 1), grid=gdim)
return numpy.divide(1.0, (1.0 + solution)) if inverse else solution
def pearson_correlation(X, Y=None):
"""
Considering the rows of X (and Y=X) as vectors, compute the
distance matrix between each pair of vectors.
This correlation implementation is equivalent to the cosine similarity
since the data it receives is assumed to be centered -- mean is 0. The
correlation may be interpreted as the cosine of the angle between the two
vectors defined by the users' preference values.
Parameters
----------
X : {array-like}, shape = [n_samples_1, n_features]
Y : {array-like}, shape = [n_samples_2, n_features]
Returns
-------
distances : {array}, shape = [n_samples_1, n_samples_2]
Examples
--------
>>> from pycudadistances.distances import pearson_correlation
>>> X = [[2.5, 3.5, 3.0, 3.5, 2.5, 3.0],[2.5, 3.5, 3.0, 3.5, 2.5, 3.0]]
>>> # distance between rows of X
>>> pearson_correlation(X, X)
array([[ 1., 1.],
[ 1., 1.]])
>>> pearson_correlation(X, [[3.0, 3.5, 1.5, 5.0, 3.5,3.0]])
array([[ 0.39605904],
[ 0.39605904]])
"""
X, Y = check_pairwise_arrays(X,Y)
rows = X.shape[0]
cols = Y.shape[0]
dx, mx = divmod(cols, BLOCK_SIZE)
dy, my = divmod(X.shape[1], BLOCK_SIZE)
gdim = ( (dx + (mx>0)), (dy + (my>0)) )
solution = numpy.zeros((rows, cols))
solution = solution.astype(numpy.float32)
kernel_code_template = """
#include <math.h>
__global__ void pearson(float *x, float *y, float *solution) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
int idy = threadIdx.y + blockDim.y * blockIdx.y;
if ( ( idx < %(NCOLS)s ) && ( idy < %(NDIM)s ) ) {
float sum_xy, sum_x, sum_y, sum_square_x, sum_square_y;
sum_x = sum_y = sum_xy = sum_square_x = sum_square_y = 0.0f;
for(int iter = 0; iter < %(NDIM)s; iter ++) {
float x_e = x[%(NDIM)s * idy + iter];
float y_e = y[%(NDIM)s * idx + iter];
sum_x += x_e;
sum_y += y_e;
sum_xy += x_e * y_e;
sum_square_x += pow(x_e, 2);
sum_square_y += pow(y_e, 2);
}
int pos = idx + %(NCOLS)s * idy;
float denom = sqrt(sum_square_x - (pow(sum_x, 2) / %(NDIM)s)) * sqrt(sum_square_y - (pow(sum_y, 2) / %(NDIM)s));
if (denom == 0) {
solution[pos] = 0;
} else {
float quot = sum_xy - ((sum_x * sum_y) / %(NDIM)s);
solution[pos] = quot / denom;
}
}
}
"""
kernel_code = kernel_code_template % {
'NCOLS': cols,
'NDIM': X.shape[1]
}
mod = SourceModule(kernel_code)
func = mod.get_function("pearson")
func(drv.In(X), drv.In(Y), drv.Out(solution), block=(BLOCK_SIZE, BLOCK_SIZE, 1), grid=gdim)
return solution
def manhattan_distances(X, Y=None):
"""
Considering the rows of X (and Y=X) as vectors, compute the
distance matrix between each pair of vectors.
This distance implementation is the distance between two points in a grid
based on a strictly horizontal and/or vertical path (that is, along the
grid lines as opposed to the diagonal or "as the crow flies" distance.
The Manhattan distance is the simple sum of the horizontal and vertical
components, whereas the diagonal distance might be computed by applying the
Pythagorean theorem.
Parameters
----------
X : {array-like}, shape = [n_samples_1, n_features]
Y : {array-like}, shape = [n_samples_2, n_features]
Returns
-------
distances : {array}, shape = [n_samples_1, n_samples_2]
Examples
--------
>>> from pycudadistances.distances import manhattan_distances
>>> X = [[2.5, 3.5, 3.0, 3.5, 2.5, 3.0],[2.5, 3.5, 3.0, 3.5, 2.5, 3.0]]
>>> # distance between rows of X
>>> manhattan_distances(X, X)
array([[ 1., 1.],
[ 1., 1.]])
>>> manhattan_distances(X, [[3.0, 3.5, 1.5, 5.0, 3.5,3.0]])
array([[ 0.25],
[ 0.25]])
"""
X, Y = check_pairwise_arrays(X,Y)
rows = X.shape[0]
cols = Y.shape[0]
dx, mx = divmod(cols, BLOCK_SIZE)
dy, my = divmod(X.shape[1], BLOCK_SIZE)
gdim = ( (dx + (mx>0)), (dy + (my>0)) )
solution = numpy.zeros((rows, cols))
solution = solution.astype(numpy.float32)
kernel_code_template = """
#include <math.h>
__global__ void manhattan(float *x, float *y, float *solution) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
int idy = threadIdx.y + blockDim.y * blockIdx.y;
if ( ( idx < %(NCOLS)s ) && ( idy < %(NDIM)s ) ) {
float result = 0.0;
for(int iter = 0; iter < %(NDIM)s; iter++) {
float x_e = x[%(NDIM)s * idy + iter];
float y_e = y[%(NDIM)s * idx + iter];
result += fabs((x_e - y_e));
}
int pos = idx + %(NCOLS)s * idy;
solution[pos] = result;
}
}
"""
kernel_code = kernel_code_template % {
'NCOLS': cols,
'NDIM': X.shape[1]
}
mod = SourceModule(kernel_code)
func = mod.get_function("manhattan")
func(drv.In(X), drv.In(Y), drv.Out(solution), block=(BLOCK_SIZE, BLOCK_SIZE, 1), grid=gdim)
return 1.0 - (solution / float(X.shape[1]))
def minkowski(X, Y=None, P=1):
"""
This is the generalized metric distance. When P=1 it becomes city
block distance and when P=2, it becomes Euclidean distance.
Computes the Minkowski distance between two vectors ``u`` and ``v``,
defined as
.. math::
{||u-v||}_p = (\sum{|u_i - v_i|^p})^{1/p}.
Parameters
----------
X : {array-like}, shape = [n_samples_1, n_features]
Y : {array-like}, shape = [n_samples_2, n_features]
p : int
The order of the norm of the difference :math:`{||u-v||}_p`.
Returns
-------
distances : {array}, shape = [n_samples_1, n_samples_2]
Examples
>>> from pycudadistances.distances import minkowski
>>> X = [[2.5, 3.5, 3.0, 3.5, 2.5, 3.0],[2.5, 3.5, 3.0, 3.5, 2.5, 3.0]]
>>> # distance between rows of X
>>> minkowski(X, X, P=1)
array([[ 0., 0.],
[ 0., 0.]])
>>> minkowski(X, [[3.0, 3.5, 1.5, 5.0, 3.5,3.0]], P=3)
array([[ 1.98952866],
[ 1.98952866]])
--------
"""
X, Y = check_pairwise_arrays(X,Y)
rows = X.shape[0]
cols = Y.shape[0]
dx, mx = divmod(cols, BLOCK_SIZE)
dy, my = divmod(X.shape[1], BLOCK_SIZE)
gdim = ( (dx + (mx>0)), (dy + (my>0)) )
solution = numpy.zeros((rows, cols))
solution = solution.astype(numpy.float32)
kernel_code_template = """
#include <math.h>
#include <stdio.h>
__global__ void minkowski(float *x, float *y, float *solution) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
int idy = threadIdx.y + blockDim.y * blockIdx.y;
if ( ( idx < %(NCOLS)s ) && ( idy < %(NDIM)s ) ) {
float result = 0.0;
for(int iter = 0; iter < %(NDIM)s; iter++) {
float x_e = x[%(NDIM)s * idy + iter];
float y_e = y[%(NDIM)s * idx + iter];
result += pow(fabs(x_e - y_e), %(ORDER)s);
}
int pos = idx + %(NCOLS)s * idy;
solution[pos] = pow(result, 1/float(%(ORDER)s));
}
}
"""
kernel_code = kernel_code_template % {
'NCOLS': cols,
'NDIM': X.shape[1],
'ORDER': P
}
mod = SourceModule(kernel_code)
func = mod.get_function("minkowski")
func(drv.In(X), drv.In(Y), drv.Out(solution), block=(BLOCK_SIZE, BLOCK_SIZE, 1), grid=gdim)
return solution
def cosine_distances(X, Y=None):
"""
Considering the rows of X (and Y=X) as vectors, compute the
distance matrix between each pair of vectors.
An implementation of the cosine similarity. The result is the cosine of
the angle formed between the two preference vectors.
Note that this similarity does not "center" its data, shifts the user's
preference values so that each of their means is 0. For this behavior,
use Pearson Coefficient, which actually is mathematically
equivalent for centered data.
Parameters
----------
X: array of shape (n_samples_1, n_features)
Y: array of shape (n_samples_2, n_features)
Returns
-------
distances: array of shape (n_samples_1, n_samples_2)
Examples
--------
>>> from pycudadistances.distances import cosine_distances
>>> X = [[2.5, 3.5, 3.0, 3.5, 2.5, 3.0],[2.5, 3.5, 3.0, 3.5, 2.5, 3.0]]
>>> # distance between rows of X
>>> cosine_distances(X, X)
array([[ 1., 1.],
[ 1., 1.]])
>>> cosine_distances(X, [[3.0, 3.5, 1.5, 5.0, 3.5,3.0]])
array([[ 0.9606463],
[ 0.9606463]])
"""
X, Y = check_pairwise_arrays(X,Y)
rows = X.shape[0]
cols = Y.shape[0]
dx, mx = divmod(cols, BLOCK_SIZE)
dy, my = divmod(X.shape[1], BLOCK_SIZE)
gdim = ( (dx + (mx>0)), (dy + (my>0)) )
solution = numpy.zeros((rows, cols))
solution = solution.astype(numpy.float32)
kernel_code_template = """
#include <math.h>
__global__ void cosine(float *x, float *y, float *solution) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
int idy = threadIdx.y + blockDim.y * blockIdx.y;
if ( ( idx < %(NCOLS)s ) && ( idy < %(NDIM)s ) ) {
float sum_ab = 0.0;
float mag_a = 0.0;
float mag_b = 0.0;
for(int iter = 0; iter < %(NDIM)s; iter++) {
float x_e = x[%(NDIM)s * idy + iter];
float y_e = y[%(NDIM)s * idx + iter];
sum_ab += x_e * y_e;
mag_a += pow(x_e, 2);
mag_b += pow(y_e, 2);
}
int pos = idx + %(NCOLS)s * idy;
solution[pos] = sum_ab / (sqrt(mag_a) * sqrt(mag_b));
}
}
"""
kernel_code = kernel_code_template % {
'NCOLS': cols,
'NDIM': X.shape[1]
}
mod = SourceModule(kernel_code)
func = mod.get_function("cosine")
func(drv.In(X), drv.In(Y), drv.Out(solution), block=(BLOCK_SIZE, BLOCK_SIZE, 1), grid=gdim)
return solution
def hamming(X, Y=None):
"""
Computes the Hamming distance between two n-vectors ``u`` and
``v``, which is simply the proportion of disagreeing components in
``u`` and ``v``. If ``u`` and ``v`` are boolean vectors, the Hamming
distance is
.. math::
\frac{c_{01} + c_{10}}{n}
where :math:`c_{ij}` is the number of occurrences of
:math:`\mathtt{u[k]} = i` and :math:`\mathtt{v[k]} = j` for
:math:`k < n`.
Parameters
----------
X: array of shape (n_samples_1, n_features)
Y: array of shape (n_samples_2, n_features)
Returns
-------
distances: array of shape (n_samples_1, n_samples_2)
"""
X, Y = check_pairwise_arrays(X,Y)
rows = X.shape[0]
cols = Y.shape[0]
dx, mx = divmod(cols, BLOCK_SIZE)
dy, my = divmod(X.shape[1], BLOCK_SIZE)
gdim = ( (dx + (mx>0)), (dy + (my>0)) )
solution = numpy.zeros((rows, cols))
solution = solution.astype(numpy.float32)
kernel_code_template = """
#include <math.h>
__global__ void hamming(float *x, float *y, float *solution) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
int idy = threadIdx.y + blockDim.y * blockIdx.y;
if ( ( idx < %(NCOLS)s ) && ( idy < %(NDIM)s ) ) {
int diff = 0;
for(int iter = 0; iter < %(NDIM)s; iter++) {
float x_e = x[%(NDIM)s * idy + iter];
float y_e = y[%(NDIM)s * idx + iter];
if(x_e != y_e) diff++;
}
int pos = idx + %(NCOLS)s * idy;
solution[pos] = diff / float(%(NDIM)s);
}
}
"""
kernel_code = kernel_code_template % {
'NCOLS': cols,
'NDIM': X.shape[1]
}
mod = SourceModule(kernel_code)
func = mod.get_function("hamming")
func(drv.In(X), drv.In(Y), drv.Out(solution), block=(BLOCK_SIZE, BLOCK_SIZE, 1), grid=gdim)
return solution
def canberra(X, Y):
X, Y = check_pairwise_arrays(X,Y)
rows = X.shape[0]
cols = Y.shape[0]
dx, mx = divmod(cols, BLOCK_SIZE)
dy, my = divmod(X.shape[1], BLOCK_SIZE)
gdim = ( (dx + (mx>0)), (dy + (my>0)) )
solution = numpy.zeros((rows, cols))
solution = solution.astype(numpy.float32)
kernel_code_template = """
#include <math.h>
__global__ void canberra(float *x, float *y, float *solution) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
int idy = threadIdx.y + blockDim.y * blockIdx.y;
if ( ( idx < %(NCOLS)s ) && ( idy < %(NDIM)s ) ) {
float result = 0.0;
for(int iter = 0; iter < %(NDIM)s; iter++) {
float x_e = x[%(NDIM)s * idy + iter];
float y_e = y[%(NDIM)s * idx + iter];
float denom = (fabs(x_e) + fabs(y_e));
if (denom != 0) result += fabs(x_e - y_e) / denom;
}
int pos = idx + %(NCOLS)s * idy;
solution[pos] = result;
}
}
"""
kernel_code = kernel_code_template % {
'NCOLS': cols,
'NDIM': X.shape[1]
}
mod = SourceModule(kernel_code)
func = mod.get_function("canberra")
func(drv.In(X), drv.In(Y), drv.Out(solution), block=(BLOCK_SIZE, BLOCK_SIZE, 1), grid=gdim)
return solution
def chebyshev(X, Y):
raise NotImplementedError
def jaccard_coefficient(X, Y):
raise NotImplementedError
def mahalanobis(X, Y):
raise NotImplementedError
def braycurtis(X, Y):
raise NotImplementedError
def sorensen_coefficient(X, Y):
raise NotImplementedError
def spearman_coefficient(X, Y):
raise NotImplementedError
def loglikehood_coefficient(X, Y):
raise NotImplementedError