-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathmisc.py
More file actions
executable file
·53 lines (48 loc) · 1.59 KB
/
Copy pathmisc.py
File metadata and controls
executable file
·53 lines (48 loc) · 1.59 KB
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
import itertools
import numpy as np
def transform_pairwise(X, y, blocks=None):
"""Transforms data into pairs with balanced labels for ranking
Transforms a n-class ranking problem into a two-class classification
problem.
In this method only pairs from the same block value are selected,
except for those that have the
same target value. The output is an array of balanced classes, i.e.
there are the same number of -1 as +1
Parameters
----------
X : array, shape (n_samples, n_features)
The data
y : array, shape (n_samples,)
Target labels.
b : array, shape (n_samples,), optional
Returns
-------
X_trans : array, shape (k, n_feaures)
Data as pairs
y_trans : array, shape (k,)
Output class labels, where classes have values {-1, +1}
diff: array
target difference for each considered samples
"""
X_new = []
y_new = []
diff = []
if blocks is None:
blocks = np.ones(len(y))
y = np.asarray(y)
comb = itertools.combinations(range(X.shape[0]), 2)
k = 0
for (i, j) in comb:
if y[i] == y[j] or blocks[i] != blocks[j]:
# skip if same target or different group
continue
X_new.append(X[i] - X[j])
diff.append(y[i] - y[j])
y_new.append(np.sign(diff[-1]))
# output balanced classes
if y_new[-1] != (-1) ** k:
y_new[-1] = - y_new[-1]
X_new[-1] = - X_new[-1]
diff[-1] = - diff[-1]
k += 1
return np.asarray(X_new), np.asarray(y_new).ravel(), np.array(diff).ravel()