-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from ljchang/rank
Rank Former-commit-id: 3228dc7
- Loading branch information
Showing
6 changed files
with
213 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,12 @@ | |
|
||
import numpy as np | ||
import pandas as pd | ||
from scipy.stats import ss, pearsonr, spearmanr, kendalltau | ||
from scipy.stats import pearsonr, spearmanr, kendalltau | ||
from copy import deepcopy | ||
import nibabel as nib | ||
from scipy.interpolate import interp1d | ||
import warnings | ||
import itertools | ||
|
||
def pearson(x, y): | ||
""" Correlates row vector x with each row vector in 2D array y. | ||
|
@@ -39,7 +40,8 @@ def pearson(x, y): | |
data = np.vstack((x, y)) | ||
ms = data.mean(axis=1)[(slice(None, None, None), None)] | ||
datam = data - ms | ||
datass = np.sqrt(ss(datam, axis=1)) | ||
datass = np.sqrt(np.sum(datam*datam, axis=1)) | ||
# datass = np.sqrt(ss(datam, axis=1)) | ||
temp = np.dot(datam[1:], datam[0].T) | ||
rs = temp / (datass[1:] * datass[0]) | ||
return rs | ||
|
@@ -466,3 +468,50 @@ def correlation_permutation(data1, data2, n_permute=5000, metric='spearman'): | |
else: | ||
stats['p'] = np.mean(all_p <= stats['correlation']) | ||
return stats | ||
|
||
def transform_pairwise(X, y): | ||
'''Transforms data into pairs with balanced labels for ranking | ||
Transforms a n-class ranking problem into a two-class classification | ||
problem. Subclasses implementing particular strategies for choosing | ||
pairs should override this method. | ||
In this method, all pairs are choosen, 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 | ||
Reference: "Large Margin Rank Boundaries for Ordinal Regression", | ||
R. Herbrich, T. Graepel, K. Obermayer. | ||
Authors: Fabian Pedregosa <[email protected]> | ||
Alexandre Gramfort <[email protected]> | ||
Args: | ||
X : array, shape (n_samples, n_features) | ||
The data | ||
y : array, shape (n_samples,) or (n_samples, 2) | ||
Target labels. If it's a 2D array, the second column represents | ||
the grouping of samples, i.e., samples with different groups will | ||
not be considered. | ||
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} | ||
''' | ||
|
||
X_new = [] | ||
y_new = [] | ||
y = np.asarray(y).flatten() | ||
if y.ndim == 1: | ||
y = np.c_[y, np.ones(y.shape[0])] | ||
comb = itertools.combinations(range(X.shape[0]), 2) | ||
for k, (i, j) in enumerate(comb): | ||
if y[i, 0] == y[j, 0] or y[i, 1] != y[j, 1]: | ||
# skip if same target or different group | ||
continue | ||
X_new.append(X[i] - X[j]) | ||
y_new.append(np.sign(y[i, 0] - y[j, 0])) | ||
# output balanced classes | ||
if y_new[-1] != (-1) ** k: | ||
y_new[-1] = - y_new[-1] | ||
X_new[-1] = - X_new[-1] | ||
return np.asarray(X_new), np.asarray(y_new).ravel() |
Oops, something went wrong.