本文整理汇总了Python中scipy.stats.kendalltau方法的典型用法代码示例。如果您正苦于以下问题:Python stats.kendalltau方法的具体用法?Python stats.kendalltau怎么用?Python stats.kendalltau使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.kendalltau方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_corr_func
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def get_corr_func(method):
if method in ['kendall', 'spearman']:
from scipy.stats import kendalltau, spearmanr
def _pearson(a, b):
return np.corrcoef(a, b)[0, 1]
def _kendall(a, b):
rs = kendalltau(a, b)
if isinstance(rs, tuple):
return rs[0]
return rs
def _spearman(a, b):
return spearmanr(a, b)[0]
_cor_methods = {
'pearson': _pearson,
'kendall': _kendall,
'spearman': _spearman
}
return _cor_methods[method]
示例2: get_corr_func
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def get_corr_func(method):
if method in ['kendall', 'spearman']:
from scipy.stats import kendalltau, spearmanr
elif callable(method):
return method
def _pearson(a, b):
return np.corrcoef(a, b)[0, 1]
def _kendall(a, b):
rs = kendalltau(a, b)
if isinstance(rs, tuple):
return rs[0]
return rs
def _spearman(a, b):
return spearmanr(a, b)[0]
_cor_methods = {
'pearson': _pearson,
'kendall': _kendall,
'spearman': _spearman
}
return _cor_methods[method]
示例3: __call__
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def __call__(self):
all_results = np.empty((len(self.systems), len(self.measures)))
# TODO: parallelise?
for system, sys_results in zip(self.systems, all_results):
if self.gold is None:
result_dict = Evaluate.read_tab_format(utf8_open(system))
else:
result_dict = Evaluate(system, self.gold, measures=self.measures, fmt='none')()
sys_results[...] = [result_dict[measure]['fscore'] for measure in self.measures]
self.all_results = all_results
correlations = {}
scores_by_measure = zip(self.measures, all_results.T)
for (measure_i, scores_i), (measure_j, scores_j) in _pairs(scores_by_measure):
correlations[measure_i, measure_j] = {'pearson': stats.pearsonr(scores_i, scores_j),
'spearman': stats.spearmanr(scores_i, scores_j),
'kendall': stats.kendalltau(scores_i, scores_j)}
quartiles = {}
for measure_i, scores_i in scores_by_measure:
quartiles[measure_i] = np.percentile(scores_i, [0, 25, 50, 75, 100])
return self.format(self, {'quartiles': quartiles, 'correlations': correlations})
示例4: correlations
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def correlations(self, X):
"""
Compute the correlations of the specified data. Only available when dimension of copula is 2.
Parameters
----------
X : numpy array (of size n * 2)
Values to compute correlations.
Returns
-------
kendall : float
The Kendall tau.
pearson : float
The Pearson's R
spearman : float
The Spearman's R
"""
if self.dim != 2:
raise Exception("Correlations can not be computed when dimension is greater than 2.")
self.kendall = kendalltau(X[:,0], X[:,1])[0]
self.pearson = pearsonr(X[:,0], X[:,1])[0]
self.spearman = spearmanr(X[:,0], X[:,1])[0]
return self.kendall, self.pearson, self.spearman
示例5: _gaussian_PKTE
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def _gaussian_PKTE(X):
# the algorithm for this comes from the paper:
# "Gaussian Copula Precision Estimation with Missing Values"
# by Huahua Wang, Faridel Fazayeli, Soumyadeep Chatterjee, Arindam Banerjee
N = X.shape[1]
sigma_hat = np.ones((N,N))
for dim1 in range(0,N-1):
for dim2 in range(dim1+1,N):
rho = np.sin(math.pi/2 * kendalltau(X[:,dim1],X[:,dim2]))
# correlation matrix is symmetric
sigma_hat[dim1][dim2] = rho
sigma_hat[dim2][dim1] = rho
# ensure that sigma_hat is positive semidefinite
sigma_hat = _nearPD(sigma_hat)
return sigma_hat
# TODO: T copula stuff
示例6: kendalls_tau
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def kendalls_tau(X):
"""
Calculates a generalized Kendall's tau for a data set given by X, as
described by "Multivariate Extensions of Spearman's Rho and Related Statistics"
Inputs:
X - the input data, should be a numpy array of shape = M x N, where
M is the number of samples, and N is the dimensionality of the data
"""
M = X.shape[0]
N = X.shape[1]
if N<2:
raise ValueError('To calculate Kendall\'s Tau, need data of dimensionality >= 2')
ktau = 0.0
for dim1 in range(0,N-1):
for dim2 in range(dim1+1,N):
(t,p) = kendalltau(X[:,dim1],X[:,dim2])
ktau = ktau + t
# normalize
ktau = ktau / comb(N,2)
return ktau
示例7: compute_sparse_kendalltau
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def compute_sparse_kendalltau(model_ids, model_perfs, gt_perfs, threshold=1e-4,
fn_map_perf_to_new_rank=sparse_rank_by_simple_bin):
"""
Compute the sparse kendall tau, by compression
:param model_ids:
:param model_perfs:
:param gt_perfs:
:param threshold:
:param fn_map_perf_to_new_rank:
:return:
"""
avg_perfs_multiplier = 1e-2 if 1 < np.average(model_perfs) < 100 else 1.
model_perfs = [p * avg_perfs_multiplier for p in model_perfs]
gt_perfs, _ = sort_hash_perfs(gt_perfs, model_perfs)
sgt_model_ids, sgt_perfs = sort_hash_perfs(model_ids, gt_perfs, verbose=False)
sgt_sparse_ranks = fn_map_perf_to_new_rank(sgt_model_ids, sgt_perfs, threshold=threshold)
pred_sparse_ranks = [sgt_sparse_ranks[sgt_model_ids.index(i)] for i in model_ids]
print("Reduced ranks from {} to {}".format(len(set(model_ids)), len(set(sgt_sparse_ranks))))
return kendalltau(sgt_sparse_ranks, pred_sparse_ranks)
示例8: kendalltau
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def kendalltau(rankA, rankB):
if len(rankA) != len(rankB):
raise TypeError("The two rank lists must be of the same length.")
N = len(rankA)
if isinstance(rankA[0], tuple):
rankA = [rankA[i][0] for i in range(N)]
if isinstance(rankB[0], tuple):
rankB = [rankB[i][0] for i in range(N)]
listA = [i for i in range(N)]
listB = [rankB.index(rankA[i]) for i in range(N)]
return kendalltau(listA, listB)[0]
示例9: reward_ddpg_D
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def reward_ddpg_D(solution, use_cuda):
"""
Kendall-Tau correlation coefficient
"""
(batch_size, n, m) = solution.size()
if use_cuda:
solution = solution.data.cpu().numpy()
else:
solution = solution.data.numpy()
target = np.array(list(range(m)))
R = []
for i in range(batch_size):
R.append(torch.FloatTensor([stats.kendalltau(solution[i], target).correlation]))
R = torch.stack(R)
if use_cuda:
R = R.cuda()
return Variable(R, requires_grad=False)
示例10: score_function
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def score_function(self, allele):
full_ensemble_predictions = self.predictor.predict(
allele=allele,
peptides=self.peptides)
def score(predictor, additional_metadata_out=None):
predictions = predictor.predict(
allele=allele,
peptides=self.peptides,
)
tau = kendalltau(predictions, full_ensemble_predictions).correlation
if additional_metadata_out is not None:
additional_metadata_out["score_consensus_tau"] = tau
return tau * self.multiply_score_by_value
return ScoreFunction(
score, summary=self.plan_summary(allele))
示例11: test_nancorr_kendall
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def test_nancorr_kendall(self):
from scipy.stats import kendalltau
targ0 = kendalltau(self.arr_float_2d, self.arr_float1_2d)[0]
targ1 = kendalltau(self.arr_float_2d.flat, self.arr_float1_2d.flat)[0]
self.check_nancorr_nancov_2d(nanops.nancorr, targ0, targ1,
method='kendall')
targ0 = kendalltau(self.arr_float_1d, self.arr_float1_1d)[0]
targ1 = kendalltau(self.arr_float_1d.flat, self.arr_float1_1d.flat)[0]
self.check_nancorr_nancov_1d(nanops.nancorr, targ0, targ1,
method='kendall')
示例12: test_corr_rank
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def test_corr_rank(self):
import scipy
import scipy.stats as stats
# kendall and spearman
A = tm.makeTimeSeries()
B = tm.makeTimeSeries()
A[-5:] = A[:5]
result = A.corr(B, method='kendall')
expected = stats.kendalltau(A, B)[0]
tm.assert_almost_equal(result, expected)
result = A.corr(B, method='spearman')
expected = stats.spearmanr(A, B)[0]
tm.assert_almost_equal(result, expected)
# these methods got rewritten in 0.8
if LooseVersion(scipy.__version__) < LooseVersion('0.9'):
pytest.skip("skipping corr rank because of scipy version "
"{0}".format(scipy.__version__))
# results from R
A = Series(
[-0.89926396, 0.94209606, -1.03289164, -0.95445587, 0.76910310, -
0.06430576, -2.09704447, 0.40660407, -0.89926396, 0.94209606])
B = Series(
[-1.01270225, -0.62210117, -1.56895827, 0.59592943, -0.01680292,
1.17258718, -1.06009347, -0.10222060, -0.89076239, 0.89372375])
kexp = 0.4319297
sexp = 0.5853767
tm.assert_almost_equal(A.corr(B, method='kendall'), kexp)
tm.assert_almost_equal(A.corr(B, method='spearman'), sexp)
示例13: eval_per_query
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def eval_per_query(self, y, y_pred):
"""
This methods computes Kendall tau at per query level (on the instances
belonging to a specific query). The Kendall tau per query is
calculated as:
tau = (P - Q) / sqrt((P + Q + T) * (P + Q + U))
where P is the number of concordant pairs, Q the number of discordant
pairs, T the number of ties only in x, and U the number of ties only
in y. If a tie occurs for the same pair in both x and y, it is not
added to either T or U.
s
Whether to use lexsort or quicksort as the sorting method for the
initial sort of the inputs. Default is lexsort (True), for which
kendalltau is of complexity O(n log(n)). If False, the complexity
is O(n^2), but with a smaller pre-factor (so quicksort may be faster
for small arrays).
Parameters
----------
y: numpy array
Represents the labels of instances corresponding to one query in
the dataset (ground truth).
y_pred: numpy array.
Represents the predicted document scores obtained during the model
scoring phase for that query.
Returns
-------
kendalltau: float
The Kendall tau per query.
"""
kendall_tau = stats.kendalltau(y, y_pred, initial_lexsort=True)
return kendall_tau.correlation
示例14: test_kendalltau
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def test_kendalltau():
# with some ties
x1 = [12, 2, 1, 12, 2]
x2 = [1, 4, 7, 1, 0]
expected = (-0.47140452079103173, 0.24821309157521476)
res = stats.kendalltau(x1, x2)
assert_approx_equal(res[0], expected[0])
assert_approx_equal(res[1], expected[1])
# with only ties in one or both inputs
assert_(np.all(np.isnan(stats.kendalltau([2,2,2], [2,2,2]))))
assert_(np.all(np.isnan(stats.kendalltau([2,0,2], [2,2,2]))))
assert_(np.all(np.isnan(stats.kendalltau([2,2,2], [2,0,2]))))
# check two different sort methods
assert_approx_equal(stats.kendalltau(x1, x2, initial_lexsort=False)[1],
stats.kendalltau(x1, x2, initial_lexsort=True)[1])
# and with larger arrays
np.random.seed(7546)
x = np.array([np.random.normal(loc=1, scale=1, size=500),
np.random.normal(loc=1, scale=1, size=500)])
corr = [[1.0, 0.3],
[0.3, 1.0]]
x = np.dot(np.linalg.cholesky(corr), x)
expected = (0.19291382765531062, 1.1337108207276285e-10)
res = stats.kendalltau(x[0], x[1])
assert_approx_equal(res[0], expected[0])
assert_approx_equal(res[1], expected[1])
# and do we get a tau of 1 for identical inputs?
assert_approx_equal(stats.kendalltau([1,1,2], [1,1,2])[0], 1.0)
示例15: test_kendalltau
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import kendalltau [as 别名]
def test_kendalltau(self):
# Tests some computations of Kendall's tau
x = ma.fix_invalid([5.05, 6.75, 3.21, 2.66,np.nan])
y = ma.fix_invalid([1.65, 26.5, -5.93, 7.96, np.nan])
z = ma.fix_invalid([1.65, 2.64, 2.64, 6.95, np.nan])
assert_almost_equal(np.asarray(mstats.kendalltau(x,y)),
[+0.3333333,0.4969059])
assert_almost_equal(np.asarray(mstats.kendalltau(x,z)),
[-0.5477226,0.2785987])
#
x = ma.fix_invalid([0, 0, 0, 0,20,20, 0,60, 0,20,
10,10, 0,40, 0,20, 0, 0, 0, 0, 0, np.nan])
y = ma.fix_invalid([0,80,80,80,10,33,60, 0,67,27,
25,80,80,80,80,80,80, 0,10,45, np.nan, 0])
result = mstats.kendalltau(x,y)
assert_almost_equal(np.asarray(result), [-0.1585188, 0.4128009])
# make sure internal variable use correct precision with
# larger arrays
x = np.arange(2000, dtype=float)
x = ma.masked_greater(x, 1995)
y = np.arange(2000, dtype=float)
y = np.concatenate((y[1000:], y[:1000]))
assert_(np.isfinite(mstats.kendalltau(x,y)[1]))
# test for namedtuple attributes
res = mstats.kendalltau(x, y)
attributes = ('correlation', 'pvalue')
check_named_results(res, attributes, ma=True)