def transform_to_positive_corrs(data, sun_idx):
y_corr = np.corrcoef(data.T)
positive = y_corr[sun_idx]
positive = positive >= 0
return positive
python类corrcoef()的实例源码
def AR1(constrained=False):
g = .95
sn = .3
y, c, s = [a[0] for a in gen_data([g], sn, N=1)]
result = constrained_oasisAR1(y, g, sn) if constrained else oasisAR1(y, g, lam=2.4)
result_foopsi = constrained_foopsi(y, [g], sn) if constrained else foopsi(y, [g], lam=2.4)
npt.assert_allclose(np.corrcoef(result[0], result_foopsi[0])[0, 1], 1)
npt.assert_allclose(np.corrcoef(result[1], result_foopsi[1])[0, 1], 1)
npt.assert_allclose(np.corrcoef(result[0], c)[0, 1], 1, .03)
npt.assert_allclose(np.corrcoef(result[1], s)[0, 1], 1, .2)
def AR2(constrained=False):
g = [1.7, -.712]
sn = .3
y, c, s = [a[0] for a in gen_data(g, sn, N=1, seed=3)]
result = constrained_onnlsAR2(y, g, sn) if constrained else onnls(y, g, lam=25)
result_foopsi = constrained_foopsi(y, g, sn) if constrained else foopsi(y, g, lam=25)
npt.assert_allclose(np.corrcoef(result[0], result_foopsi[0])[0, 1], 1, 1e-3)
npt.assert_allclose(np.corrcoef(result[1], result_foopsi[1])[0, 1], 1, 1e-2)
npt.assert_allclose(np.corrcoef(result[0], c)[0, 1], 1, .03)
npt.assert_allclose(np.corrcoef(result[1], s)[0, 1], 1, .2)
result2 = constrained_oasisAR2(y, g[0], g[1], sn) if constrained \
else oasisAR2(y, g[0], g[1], lam=25)
npt.assert_allclose(np.corrcoef(result2[0], c)[0, 1], 1, .03)
npt.assert_allclose(np.corrcoef(result2[1], s)[0, 1], 1, .2)
def test_non_array(self):
assert_almost_equal(np.corrcoef([0, 1, 0], [1, 0, 1]),
[[1., -1.], [-1., 1.]])
def test_simple(self):
tgt1 = corrcoef(self.A)
assert_almost_equal(tgt1, self.res1)
assert_(np.all(np.abs(tgt1) <= 1.0))
tgt2 = corrcoef(self.A, self.B)
assert_almost_equal(tgt2, self.res2)
assert_(np.all(np.abs(tgt2) <= 1.0))
def test_ddof(self):
# ddof raises DeprecationWarning
with catch_warn_nfb():
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, self.A, ddof=-1)
warnings.simplefilter("ignore")
# ddof has no or negligible effect on the function
assert_almost_equal(corrcoef(self.A, ddof=-1), self.res1)
assert_almost_equal(corrcoef(self.A, self.B, ddof=-1), self.res2)
assert_almost_equal(corrcoef(self.A, ddof=3), self.res1)
assert_almost_equal(corrcoef(self.A, self.B, ddof=3), self.res2)
def test_bias(self):
# bias raises DeprecationWarning
with catch_warn_nfb():
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, self.A, self.B, 1, 0)
assert_warns(DeprecationWarning, corrcoef, self.A, bias=0)
warnings.simplefilter("ignore")
# bias has no or negligible effect on the function
assert_almost_equal(corrcoef(self.A, bias=1), self.res1)
def test_complex(self):
x = np.array([[1, 2, 3], [1j, 2j, 3j]])
res = corrcoef(x)
tgt = np.array([[1., -1.j], [1.j, 1.]])
assert_allclose(res, tgt)
assert_(np.all(np.abs(res) <= 1.0))
def test_empty(self):
with warnings.catch_warnings(record=True):
warnings.simplefilter('always', RuntimeWarning)
assert_array_equal(corrcoef(np.array([])), np.nan)
assert_array_equal(corrcoef(np.array([]).reshape(0, 2)),
np.array([]).reshape(0, 0))
assert_array_equal(corrcoef(np.array([]).reshape(2, 0)),
np.array([[np.nan, np.nan], [np.nan, np.nan]]))
def test_extreme(self):
x = [[1e-100, 1e100], [1e100, 1e-100]]
with np.errstate(all='raise'):
c = corrcoef(x)
assert_array_almost_equal(c, np.array([[1., -1.], [-1., 1.]]))
assert_(np.all(np.abs(c) <= 1.0))
def test_ddof(self):
# ddof raises DeprecationWarning
x, y = self.data, self.data2
expected = np.corrcoef(x)
expected2 = np.corrcoef(x, y)
with catch_warn_mae():
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, x, ddof=-1)
warnings.simplefilter("ignore")
# ddof has no or negligible effect on the function
assert_almost_equal(np.corrcoef(x, ddof=0), corrcoef(x, ddof=0))
assert_almost_equal(corrcoef(x, ddof=-1), expected)
assert_almost_equal(corrcoef(x, y, ddof=-1), expected2)
assert_almost_equal(corrcoef(x, ddof=3), expected)
assert_almost_equal(corrcoef(x, y, ddof=3), expected2)
def test_bias(self):
x, y = self.data, self.data2
expected = np.corrcoef(x)
# bias raises DeprecationWarning
with catch_warn_mae():
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, x, y, True, False)
assert_warns(DeprecationWarning, corrcoef, x, y, True, True)
assert_warns(DeprecationWarning, corrcoef, x, bias=False)
warnings.simplefilter("ignore")
# bias has no or negligible effect on the function
assert_almost_equal(corrcoef(x, bias=1), expected)
def test_1d_wo_missing(self):
# Test cov on 1D variable w/o missing values
x = self.data
assert_almost_equal(np.corrcoef(x), corrcoef(x))
assert_almost_equal(np.corrcoef(x, rowvar=False),
corrcoef(x, rowvar=False))
with catch_warn_mae():
warnings.simplefilter("ignore")
assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True),
corrcoef(x, rowvar=False, bias=True))
def test_1d_w_missing(self):
# Test corrcoef 1 1D variable w/missing values
x = self.data
x[-1] = masked
x -= x.mean()
nx = x.compressed()
assert_almost_equal(np.corrcoef(nx), corrcoef(x))
assert_almost_equal(np.corrcoef(nx, rowvar=False),
corrcoef(x, rowvar=False))
with catch_warn_mae():
warnings.simplefilter("ignore")
assert_almost_equal(np.corrcoef(nx, rowvar=False, bias=True),
corrcoef(x, rowvar=False, bias=True))
try:
corrcoef(x, allow_masked=False)
except ValueError:
pass
# 2 1D variables w/ missing values
nx = x[1:-1]
assert_almost_equal(np.corrcoef(nx, nx[::-1]), corrcoef(x, x[::-1]))
assert_almost_equal(np.corrcoef(nx, nx[::-1], rowvar=False),
corrcoef(x, x[::-1], rowvar=False))
with catch_warn_mae():
warnings.simplefilter("ignore")
# ddof and bias have no or negligible effect on the function
assert_almost_equal(np.corrcoef(nx, nx[::-1]),
corrcoef(x, x[::-1], bias=1))
assert_almost_equal(np.corrcoef(nx, nx[::-1]),
corrcoef(x, x[::-1], ddof=2))
def ncc(ypred, y):
return np.corrcoef(ypred, y)[1,0]
def test_convergence(self):
size = 100
buf = prioritized.PrioritizedBuffer(capacity=size)
for x in range(size):
buf.append(x)
priority_init = list(range(size))
random.shuffle(priority_init)
count_sampled = [0] * size
def priority(x, n):
return priority_init[x] + 1 / count_sampled[x]
count_none = 0
for t in range(200):
sampled, probabilities = buf.sample(16)
if all([p is not None for p in probabilities]):
priority_old = [priority(x, count_sampled[x]) for x in sampled]
# assert: probabilities \propto priority_old
qs = [x / y for x, y in zip(probabilities, priority_old)]
for q in qs:
self.assertAlmostEqual(q, qs[0])
else:
count_none += 1
for x in sampled:
count_sampled[x] += 1
priority_new = [priority(x, count_sampled[x]) for x in sampled]
buf.set_last_priority(priority_new)
for cnt in count_sampled:
self.assertGreaterEqual(cnt, 1)
self.assertLessEqual(count_none, size // 16 + 1)
corr = np.corrcoef(np.array([priority_init, count_sampled]))[0, 1]
self.assertGreater(corr, 0.8)
def corr2_coeff(AB,msk,myrad,bcast_var):
if not np.all(msk):
return None
A,B = (AB[0], AB[1])
A = A.reshape((-1,A.shape[-1]))
B = B.reshape((-1,B.shape[-1]))
corrAB = np.corrcoef(A.T,B.T)[16:,:16]
classical_within = np.mean(corrAB[0:8,0:8])
jazz_within = np.mean(corrAB[8:16,8:16])
classJazz_between = np.mean(corrAB[8:16,0:8])
jazzClass_between = np.mean(corrAB[0:8,8:16])
within_genre = np.mean([classical_within,jazz_within])
between_genre = np.mean([classJazz_between,jazzClass_between])
diff = within_genre - between_genre
return diff
def cross_correlation(data1, data2):
"""
:param data1:
:param data2:
:return:
"""
# correlation test
corr_min = 1.0
corr_mat = np.corrcoef(data1, data2)
corr = np.min(corr_mat)
corr_min = min(corr, corr_min)
return corr_min
def apply(self, data):
return np.corrcoef(data)
def compute_PCC(A, B, masks=None):
"""Computes the Pearson product-moment correlation coefficients (PCC) for
the two images.
Parameters
-------------
A,B : ndarray
The two images to be compared
masks : list of ndarrays, optional
If supplied, the data under each mask is computed separately.
Returns
----------------
covariances : array, list of arrays
"""
covariances = []
if masks is None:
data = np.vstack((np.ravel(A), np.ravel(B)))
return np.corrcoef(data)
for m in masks:
weights = m[m > 0]
masked_B = B[m > 0]
masked_A = A[m > 0]
data = np.vstack((masked_A, masked_B))
# covariances.append(np.cov(data,aweights=weights))
covariances.append(np.corrcoef(data))
return covariances