本文整理汇总了Python中scipy.stats.spearmanr方法的典型用法代码示例。如果您正苦于以下问题:Python stats.spearmanr方法的具体用法?Python stats.spearmanr怎么用?Python stats.spearmanr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.spearmanr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: time_dist
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def time_dist(datasets_dimred, time):
time_dist = euclidean_distances(time, time)
time_dists, scores = [], []
for i in range(time_dist.shape[0]):
for j in range(time_dist.shape[1]):
if i >= j:
continue
score = np.mean(euclidean_distances(
datasets_dimred[i], datasets_dimred[j]
))
time_dists.append(time_dist[i, j])
scores.append(score)
print('Spearman rho = {}'.format(spearmanr(time_dists, scores)))
print('Pearson rho = {}'.format(pearsonr(time_dists, scores)))
示例2: evaluate
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def evaluate(wv, reference):
"""Evaluate wv against reference, return (rho, count) where rwo is
Spearman's rho and count is the number of reference word pairs
that could be evaluated against.
"""
gold, predicted = [], []
for words, sim in sorted(reference, key=lambda ws: ws[1]):
try:
v1, v2 = wv[words[0]], wv[words[1]]
except KeyError:
continue
gold.append((words, sim))
predicted.append((words, cosine(v1, v2)))
simlist = lambda ws: [s for w,s in ws]
rho, p = spearmanr(simlist(gold), simlist(predicted))
return (rho, len(gold))
示例3: get_corr_func
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [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]
示例4: word_sim_test
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def word_sim_test(filename, pos_vectors):
delim = ','
actual_sim_list, pred_sim_list = [], []
missed = 0
with open(filename, 'r') as pairs:
for pair in pairs:
w1, w2, actual_sim = pair.strip().split(delim)
try:
w1_vec = create_word_vector(w1, pos_vectors)
w2_vec = create_word_vector(w2, pos_vectors)
pred = float(np.inner(w1_vec, w2_vec))
actual_sim_list.append(float(actual_sim))
pred_sim_list.append(pred)
except KeyError:
missed += 1
spearman, _ = st.spearmanr(actual_sim_list, pred_sim_list)
pearson, _ = st.pearsonr(actual_sim_list, pred_sim_list)
return spearman, pearson, missed
示例5: feature_corr_matrix
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def feature_corr_matrix(df):
"""
Return the Spearman's rank-order correlation between all pairs
of features as a matrix with feature names as index and column names.
The diagonal will be all 1.0 as features are self correlated.
Spearman's correlation is the same thing as converting two variables
to rank values and then running a standard Pearson's correlation
on those ranked variables. Spearman's is nonparametric and does not
assume a linear relationship between the variables; it looks for
monotonic relationships.
:param df_train: dataframe containing features as columns, and
without the target variable.
:return: a data frame with the correlation matrix
"""
corr = np.round(spearmanr(df).correlation, 4)
df_corr = pd.DataFrame(data=corr, index=df.columns, columns=df.columns)
return df_corr
示例6: spearmanr
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def spearmanr(self):
""" Compute target SpearmanR vector. """
scor = np.zeros(self.num_targets)
for ti in range(self.num_targets):
if self.targets_na is not None:
preds_ti = self.preds[~self.targets_na, ti]
targets_ti = self.targets[~self.targets_na, ti]
else:
preds_ti = self.preds[:, :, ti].flatten()
targets_ti = self.targets[:, :, ti].flatten()
sc, _ = stats.spearmanr(targets_ti, preds_ti)
scor[ti] = sc
return scor
################################################################################
# __main__
################################################################################
示例7: imputation_score
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def imputation_score(trainer_both, data_spatial, gene_ids_test, normalized=True):
_, fish_imputation = trainer_both.get_imputed_values(normalized=normalized)
original, imputed = (
data_spatial.X[:, gene_ids_test],
fish_imputation[:, gene_ids_test],
)
if normalized:
original /= data_spatial.X.sum(axis=1).reshape(-1, 1)
spearman_gene = []
for g in range(imputed.shape[1]):
if np.all(imputed[:, g] == 0):
correlation = 0
else:
correlation = spearmanr(original[:, g], imputed[:, g])[0]
spearman_gene.append(correlation)
return np.median(np.array(spearman_gene))
示例8: eval_per_query
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def eval_per_query(self, y, y_pred):
"""
This methods computes Spearman Rho at per query level (on the instances
belonging to a specific query).
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
-------
rho: float
The Spearman Rho per query.
"""
spearman_rho = stats.spearmanr(y, y_pred)
return spearman_rho.correlation
示例9: get_corr_func
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [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]
示例10: test_tie1
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def test_tie1(self):
# Data
x = [1.0, 2.0, 3.0, 4.0]
y = [1.0, 2.0, 2.0, 3.0]
# Ranks of the data, with tie-handling.
xr = [1.0, 2.0, 3.0, 4.0]
yr = [1.0, 2.5, 2.5, 4.0]
# Result of spearmanr should be the same as applying
# pearsonr to the ranks.
sr = stats.spearmanr(x, y)
pr = stats.pearsonr(xr, yr)
assert_almost_equal(sr, pr)
## W.II.E. Tabulate X against X, using BIG as a case weight. The values
## should appear on the diagonal and the total should be 899999955.
## If the table cannot hold these values, forget about working with
## census data. You can also tabulate HUGE against TINY. There is no
## reason a tabulation program should not be able to distinguish
## different values regardless of their magnitude.
### I need to figure out how to do this one.
示例11: __call__
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [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})
示例12: pearson_and_spearman
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def pearson_and_spearman(preds, labels):
pearson_corr = pearsonr(preds, labels)[0]
spearman_corr = spearmanr(preds, labels)[0]
return {
"pearson": pearson_corr,
"spearmanr": spearman_corr,
"corr": (pearson_corr + spearman_corr) / 2,
}
示例13: time_align_correlate
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def time_align_correlate(alignments, time):
time_dist = euclidean_distances(time, time)
assert(time_dist.shape == alignments.shape)
time_dists, scores = [], []
for i in range(time_dist.shape[0]):
for j in range(time_dist.shape[1]):
if i >= j:
continue
time_dists.append(time_dist[i, j])
scores.append(alignments[i, j])
print('Spearman rho = {}'.format(spearmanr(time_dists, scores)))
print('Pearson rho = {}'.format(pearsonr(time_dists, scores)))
示例14: validate_spearman_correlation
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def validate_spearman_correlation(overall_imp, shap_overall_imp, threshold):
# Calculate the spearman rank-order correlation
rho, p_val = stats.spearmanr(overall_imp, shap_overall_imp)
# Validate that the coefficients from the linear model are highly correlated with the results from shap
test_logger.info(
"Calculated spearman correlation coefficient rho: "
+ str(rho)
+ " and p_val: "
+ str(p_val)
)
assert rho > threshold
示例15: test_fit
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import spearmanr [as 别名]
def test_fit(self):
np.random.seed(1)
tf.reset_default_graph()
tf.set_random_seed(0)
latent_dim = 2
res_ranks, res_biplot = paired_omics(
self.microbes, self.metabolites,
epochs=1000, latent_dim=latent_dim,
min_feature_count=1, learning_rate=0.1
)
res_ranks = clr_inv(res_ranks.T)
s_r, s_p = spearmanr(np.ravel(res_ranks), np.ravel(self.exp_ranks))
self.assertGreater(s_r, 0.5)
self.assertLess(s_p, 1e-2)
# make sure the biplot is of the correct dimensions
npt.assert_allclose(
res_biplot.samples.shape,
np.array([self.microbes.shape[0], latent_dim]))
npt.assert_allclose(
res_biplot.features.shape,
np.array([self.metabolites.shape[0], latent_dim]))
# make sure that the biplot has the correct ordering
self.assertGreater(res_biplot.proportion_explained[0],
res_biplot.proportion_explained[1])
self.assertGreater(res_biplot.eigvals[0],
res_biplot.eigvals[1])