本文整理汇总了Python中scipy.stats.pearsonr方法的典型用法代码示例。如果您正苦于以下问题:Python stats.pearsonr方法的具体用法?Python stats.pearsonr怎么用?Python stats.pearsonr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.pearsonr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: joint_plot
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def joint_plot(x, y, xlabel=None,
ylabel=None, xlim=None, ylim=None,
loc="best", color='#0485d1',
size=8, markersize=50, kind="kde",
scatter_color="r"):
with sns.axes_style("darkgrid"):
if xlabel and ylabel:
g = SubsampleJointGrid(xlabel, ylabel,
data=DataFrame(data={xlabel: x, ylabel: y}),
space=0.1, ratio=2, size=size, xlim=xlim, ylim=ylim)
else:
g = SubsampleJointGrid(x, y, size=size,
space=0.1, ratio=2, xlim=xlim, ylim=ylim)
g.plot_joint(sns.kdeplot, shade=True, cmap="Blues")
g.plot_sub_joint(plt.scatter, 1000, s=20, c=scatter_color, alpha=0.3)
g.plot_marginals(sns.distplot, kde=False, rug=False)
g.annotate(ss.pearsonr, fontsize=25, template="{stat} = {val:.2g}\np = {p:.2g}")
g.ax_joint.set_yticklabels(g.ax_joint.get_yticks())
g.ax_joint.set_xticklabels(g.ax_joint.get_xticks())
return g
示例2: test_compute_correlations_between_versions_custom_columns
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def test_compute_correlations_between_versions_custom_columns(self):
df_old = pd.DataFrame({'id': ['a', 'b', 'c'],
'feature1': [1.3, 1.5, 2.1],
'feature2': [1.1, 6.2, 2.1],
'r1': [2, 3, 4]})
df_new = pd.DataFrame({'id': ['a', 'b', 'c'],
'feature1': [-1.3, -1.5, -2.1],
'feature2': [1.1, 6.2, 2.1],
'r1': [2, 3, 4]})
df_cors = Comparer.compute_correlations_between_versions(df_old,
df_new,
human_score='r1',
id_column='id')
assert_almost_equal(df_cors.at['feature1', 'old_new'], -1.0)
assert_almost_equal(df_cors.at['feature2', 'old_new'], 1.0)
assert_equal(df_cors.at['feature1', 'human_old'], pearsonr(df_old['feature1'],
df_old['r1'])[0])
assert_equal(df_cors.at['feature1', 'human_new'], pearsonr(df_new['feature1'],
df_new['r1'])[0])
assert_equal(df_cors.at['feature1', "N"], 3)
示例3: betaseries_file
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def betaseries_file(tmpdir_factory,
deriv_betaseries_fname=deriv_betaseries_fname):
bfile = tmpdir_factory.mktemp("beta").ensure(deriv_betaseries_fname)
np.random.seed(3)
num_trials = 40
tgt_corr = 0.1
bs1 = np.random.rand(num_trials)
# create another betaseries with a target correlation
bs2 = minimize(lambda x: abs(tgt_corr - pearsonr(bs1, x)[0]),
np.random.rand(num_trials)).x
# two identical beta series
bs_data = np.array([[[bs1, bs2]]])
# the nifti image
bs_img = nib.Nifti1Image(bs_data, np.eye(4))
bs_img.to_filename(str(bfile))
return bfile
示例4: time_dist
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [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)))
示例5: word_sim_test
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [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
示例6: pearsonr
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def pearsonr(self, log=False, pseudocount=1, clip=None):
""" Compute target PearsonR vector. """
pcor = 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].astype('float64')
targets_ti = self.targets[~self.targets_na, ti].astype('float64')
else:
preds_ti = self.preds[:, :, ti].flatten().astype('float64')
targets_ti = self.targets[:, :, ti].flatten().astype('float64')
if clip is not None:
preds_ti = np.clip(preds_ti, 0, clip)
targets_ti = np.clip(targets_ti, 0, clip)
if log:
preds_ti = np.log2(preds_ti + pseudocount)
targets_ti = np.log2(targets_ti + pseudocount)
pc, _ = stats.pearsonr(targets_ti, preds_ti)
pcor[ti] = pc
return pcor
示例7: test_tie1
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [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.
示例8: __call__
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [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})
示例9: test_compute_correlations_between_versions_default_columns
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def test_compute_correlations_between_versions_default_columns(self):
df_old = pd.DataFrame({'spkitemid': ['a', 'b', 'c'],
'feature1': [1.3, 1.5, 2.1],
'feature2': [1.1, 6.2, 2.1],
'sc1': [2, 3, 4]})
df_new = pd.DataFrame({'spkitemid': ['a', 'b', 'c'],
'feature1': [-1.3, -1.5, -2.1],
'feature2': [1.1, 6.2, 2.1],
'sc1': [2, 3, 4]})
df_cors = Comparer.compute_correlations_between_versions(df_old, df_new)
assert_almost_equal(df_cors.at['feature1', 'old_new'], -1.0)
assert_almost_equal(df_cors.at['feature2', 'old_new'], 1.0)
assert_equal(df_cors.at['feature1', 'human_old'], pearsonr(df_old['feature1'],
df_old['sc1'])[0])
assert_equal(df_cors.at['feature1', 'human_new'], pearsonr(df_new['feature1'],
df_new['sc1'])[0])
assert_equal(df_cors.at['feature1', "N"], 3)
示例10: produce_the_kde_plot
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def produce_the_kde_plot(cycles, color, save_name):
ground_truth_and_suggested = [(eval_code.get_best_qed_from_smiles_bag(elem['ground_truth_product']),
eval_code.get_best_qed_from_smiles_bag(elem['suggested_product']))
for elem in cycles]
len_out = len(ground_truth_and_suggested)
ground_truth_and_suggested = [elem for elem in ground_truth_and_suggested if elem[1] != -np.inf]
len_filter = len(ground_truth_and_suggested)
num_discarding = len_out - len_filter
if num_discarding:
warnings.warn(f"Discarding {num_discarding} our of {len_out} as no successful reconstruction")
ground_truth_and_suggested = np.array(ground_truth_and_suggested)
ground_truth_product_qed = ground_truth_and_suggested[:, 0]
suggested_product_qed = ground_truth_and_suggested[:, 1]
g = sns.jointplot(x=ground_truth_product_qed, y=suggested_product_qed, kind="kde", color=color,
)
g.set_axis_labels("product's QED", "reconstructed product's QED", fontsize=16)
rsquare = lambda a, b: stats.pearsonr(ground_truth_product_qed, suggested_product_qed)[0] ** 2
g = g.annotate(rsquare, template="{stat}: {val:.2f}",
stat="$R^2$", loc="upper left", fontsize=12)
print(f"Rsquare: {stats.pearsonr(ground_truth_product_qed, suggested_product_qed)[0] ** 2}")
print(f"scipystats: {stats.linregress(ground_truth_product_qed, suggested_product_qed)}")
plt.tight_layout()
plt.savefig(f"{save_name}.pdf")
示例11: test_parallel_create_independant_random
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def test_parallel_create_independant_random(self):
"""...Test that random number generator creates independant
samples in a multithreaded environment
"""
for thread_type in self.thread_types:
samples = self._generate_samples_in_parallel(
parallelization_type=thread_type)
# We check that we do not have any lines that is identical to the
# one following
following_samples_are_different = \
np.prod(np.linalg.norm(samples[:-1] - samples[1:], axis=1) > 0)
self.assertEqual(
following_samples_are_different, 1,
"Two samples generated in parallel are identical")
# We check that our generated samples are not correlated
for (i, sample_i), (j, sample_j) in \
itertools.product(enumerate(samples), enumerate(samples)):
if i != j:
corr_coeff = stats.pearsonr(sample_i, sample_j)[0]
self.assertLess(np.abs(corr_coeff), 0.1)
示例12: pearson_and_spearman
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [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 pearsonr [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: test_corr
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def test_corr(self, datetime_series):
import scipy.stats as stats
# full overlap
tm.assert_almost_equal(datetime_series.corr(datetime_series), 1)
# partial overlap
tm.assert_almost_equal(datetime_series[:15].corr(datetime_series[5:]),
1)
assert isna(datetime_series[:15].corr(datetime_series[5:],
min_periods=12))
ts1 = datetime_series[:15].reindex(datetime_series.index)
ts2 = datetime_series[5:].reindex(datetime_series.index)
assert isna(ts1.corr(ts2, min_periods=12))
# No overlap
assert np.isnan(datetime_series[::2].corr(datetime_series[1::2]))
# all NA
cp = datetime_series[:10].copy()
cp[:] = np.nan
assert isna(cp.corr(cp))
A = tm.makeTimeSeries()
B = tm.makeTimeSeries()
result = A.corr(B)
expected, _ = stats.pearsonr(A, B)
tm.assert_almost_equal(result, expected)
示例15: pearson_r2_score
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import pearsonr [as 别名]
def pearson_r2_score(y, y_pred):
"""Computes Pearson R^2 (square of Pearson correlation)."""
return pearsonr(y, y_pred)[0]**2