本文整理匯總了Python中numpy.inner方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.inner方法的具體用法?Python numpy.inner怎麽用?Python numpy.inner使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.inner方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: random_projection
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def random_projection(X):
data_demension = X.shape[1]
new_data_demension = random.randint(2, data_demension)
new_X = np.empty((data_demension, new_data_demension))
minus_one = 0.1
positive_one = 0.9
for i in range(len(new_X)):
for j in range(len(new_X[i])):
rand = random.random()
if rand < minus_one:
new_X[i][j] = -1.0
elif rand >= positive_one:
new_X[i][j] = 1.0
else:
new_X[i][j] = 0.0
new_X = np.inner(X, new_X.T)
return new_X
示例2: __init__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def __init__(self, y, x, height, width, angle):
"""
A efficient tool to rotate multiple images in the same size.
:author 申瑞瑉 (Ruimin Shen)
:param y: The y coordinate of rotation point.
:param x: The x coordinate of rotation point.
:param height: Image height.
:param width: Image width.
:param angle: Rotate angle.
"""
self._mat = cv2.getRotationMatrix2D((x, y), angle, 1.0)
r = np.abs(self._mat[0, :2])
_height, _width = np.inner(r, [height, width]), np.inner(r, [width, height])
fix_y, fix_x = _height / 2 - y, _width / 2 - x
self._mat[:, 2] += [fix_x, fix_y]
self._size = int(_width), int(_height)
示例3: test_4
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_4(self):
"""
Test of take, transpose, inner, outer products.
"""
x = self.arange(24)
y = np.arange(24)
x[5:6] = self.masked
x = x.reshape(2, 3, 4)
y = y.reshape(2, 3, 4)
assert self.allequal(np.transpose(y, (2, 0, 1)), self.transpose(x, (2, 0, 1)))
assert self.allequal(np.take(y, (2, 0, 1), 1), self.take(x, (2, 0, 1), 1))
assert self.allequal(np.inner(self.filled(x, 0), self.filled(y, 0)),
self.inner(x, y))
assert self.allequal(np.outer(self.filled(x, 0), self.filled(y, 0)),
self.outer(x, y))
y = self.array(['abc', 1, 'def', 2, 3], object)
y[2] = self.masked
t = self.take(y, [0, 3, 4])
assert t[0] == 'abc'
assert t[1] == 2
assert t[2] == 3
示例4: test_testTakeTransposeInnerOuter
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_testTakeTransposeInnerOuter(self):
# Test of take, transpose, inner, outer products
x = arange(24)
y = np.arange(24)
x[5:6] = masked
x = x.reshape(2, 3, 4)
y = y.reshape(2, 3, 4)
assert_(eq(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1))))
assert_(eq(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1)))
assert_(eq(np.inner(filled(x, 0), filled(y, 0)),
inner(x, y)))
assert_(eq(np.outer(filled(x, 0), filled(y, 0)),
outer(x, y)))
y = array(['abc', 1, 'def', 2, 3], object)
y[2] = masked
t = take(y, [0, 3, 4])
assert_(t[0] == 'abc')
assert_(t[1] == 2)
assert_(t[2] == 3)
示例5: test_einsum_all_contig_non_contig_output
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_einsum_all_contig_non_contig_output(self):
# Issue gh-5907, tests that the all contiguous special case
# actually checks the contiguity of the output
x = np.ones((5, 5))
out = np.ones(10)[::2]
correct_base = np.ones(10)
correct_base[::2] = 5
# Always worked (inner iteration is done with 0-stride):
np.einsum('mi,mi,mi->m', x, x, x, out=out)
assert_array_equal(out.base, correct_base)
# Example 1:
out = np.ones(10)[::2]
np.einsum('im,im,im->m', x, x, x, out=out)
assert_array_equal(out.base, correct_base)
# Example 2, buffering causes x to be contiguous but
# special cases do not catch the operation before:
out = np.ones((2, 2, 2))[..., 0]
correct_base = np.ones((2, 2, 2))
correct_base[..., 0] = 2
x = np.ones((2, 2), np.float32)
np.einsum('ij,jk->ik', x, x, out=out)
assert_array_equal(out.base, correct_base)
示例6: word_sim_test
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [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
示例7: test_lanczos_evolve
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_lanczos_evolve(n, N_cache, tol=5.e-14):
# generate Hermitian test array
leg = gen_random_legcharge(ch, n)
H = npc.Array.from_func_square(rmat.GUE, leg) - npc.diag(1., leg)
H_flat = H.to_ndarray()
H_Op = H # use `matvec` of the array
qtotal = leg.to_qflat()[0]
psi_init = npc.Array.from_func(np.random.random, [leg], qtotal=qtotal)
psi_init /= npc.norm(psi_init)
psi_init_flat = psi_init.to_ndarray()
lanc = lanczos.LanczosEvolution(H_Op, psi_init, {'verbose': 1, 'N_cache': N_cache})
for delta in [-0.1j, 0.1j, 1.j]: #, 0.1, 1.]:
psi_final_flat = expm(H_flat * delta).dot(psi_init_flat)
psi_final, N = lanc.run(delta)
ov = np.inner(psi_final.to_ndarray().conj(), psi_final_flat)
ov /= np.linalg.norm(psi_final_flat)
print("<psi1|psi1_flat>/norm=", ov)
assert (abs(1. - abs(ov)) < tol)
示例8: test_predict
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_predict(self):
X_test = np.random.rand(10, 2)
m, v = self.model.predict(X_test)
assert len(m.shape) == 1
assert m.shape[0] == X_test.shape[0]
assert len(v.shape) == 1
assert v.shape[0] == X_test.shape[0]
m, v = self.model.predict(X_test, full_cov=True)
assert len(m.shape) == 1
assert m.shape[0] == X_test.shape[0]
assert len(v.shape) == 2
assert v.shape[0] == X_test.shape[0]
assert v.shape[1] == X_test.shape[0]
K_zz = self.kernel.get_value(X_test)
K_zx = self.kernel.get_value(X_test, self.X)
K_nz = self.kernel.get_value(self.X) + self.model.noise * np.eye(self.X.shape[0])
inv = spla.inv(K_nz)
K_zz_x = K_zz - np.dot(K_zx, np.inner(inv, K_zx))
assert np.mean((K_zz_x - v) ** 2) < 10e-5
示例9: test_TakeTransposeInnerOuter
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_TakeTransposeInnerOuter(self):
# Test of take, transpose, inner, outer products
x = arange(24)
y = np.arange(24)
x[5:6] = masked
x = x.reshape(2, 3, 4)
y = y.reshape(2, 3, 4)
assert_equal(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1)))
assert_equal(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1))
assert_equal(np.inner(filled(x, 0), filled(y, 0)),
inner(x, y))
assert_equal(np.outer(filled(x, 0), filled(y, 0)),
outer(x, y))
y = array(['abc', 1, 'def', 2, 3], object)
y[2] = masked
t = take(y, [0, 3, 4])
assert_(t[0] == 'abc')
assert_(t[1] == 2)
assert_(t[2] == 3)
示例10: test_einsum_misc
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_einsum_misc(self):
# This call used to crash because of a bug in
# PyArray_AssignZero
a = np.ones((1, 2))
b = np.ones((2, 2, 1))
assert_equal(np.einsum('ij...,j...->i...', a, b), [[[2], [2]]])
# The iterator had an issue with buffering this reduction
a = np.ones((5, 12, 4, 2, 3), np.int64)
b = np.ones((5, 12, 11), np.int64)
assert_equal(np.einsum('ijklm,ijn,ijn->', a, b, b),
np.einsum('ijklm,ijn->', a, b))
# Issue #2027, was a problem in the contiguous 3-argument
# inner loop implementation
a = np.arange(1, 3)
b = np.arange(1, 5).reshape(2, 2)
c = np.arange(1, 9).reshape(4, 2)
assert_equal(np.einsum('x,yx,zx->xzy', a, b, c),
[[[1, 3], [3, 9], [5, 15], [7, 21]],
[[8, 16], [16, 32], [24, 48], [32, 64]]])
示例11: fit
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def fit(self):
"""
Fits the model by application of the Kalman filter
Returns
-------
RecursiveLSResults
"""
# Get the smoother results with an arbitrary measurement variance
smoother_results = self.smooth(return_ssm=True)
# Compute the MLE of sigma2 (see Harvey, 1989 equation 4.2.5)
resid = smoother_results.standardized_forecasts_error[0]
sigma2 = (np.inner(resid, resid) /
(self.nobs - self.loglikelihood_burn))
# Now construct a results class, where the params are the final
# estimates of the regression coefficients
self['obs_cov', 0, 0] = sigma2
return self.smooth()
示例12: test_4
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_4(self):
"Test of take, transpose, inner, outer products"
x = self.arange(24)
y = np.arange(24)
x[5:6] = self.masked
x = x.reshape(2, 3, 4)
y = y.reshape(2, 3, 4)
assert self.allequal(np.transpose(y, (2, 0, 1)), self.transpose(x, (2, 0, 1)))
assert self.allequal(np.take(y, (2, 0, 1), 1), self.take(x, (2, 0, 1), 1))
assert self.allequal(np.inner(self.filled(x, 0), self.filled(y, 0)),
self.inner(x, y))
assert self.allequal(np.outer(self.filled(x, 0), self.filled(y, 0)),
self.outer(x, y))
y = self.array(['abc', 1, 'def', 2, 3], object)
y[2] = self.masked
t = self.take(y, [0, 3, 4])
assert t[0] == 'abc'
assert t[1] == 2
assert t[2] == 3
#----------------------------------
示例13: test_testTakeTransposeInnerOuter
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_testTakeTransposeInnerOuter(self):
"Test of take, transpose, inner, outer products"
x = arange(24)
y = np.arange(24)
x[5:6] = masked
x = x.reshape(2, 3, 4)
y = y.reshape(2, 3, 4)
assert_(eq(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1))))
assert_(eq(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1)))
assert_(eq(np.inner(filled(x, 0), filled(y, 0)),
inner(x, y)))
assert_(eq(np.outer(filled(x, 0), filled(y, 0)),
outer(x, y)))
y = array(['abc', 1, 'def', 2, 3], object)
y[2] = masked
t = take(y, [0, 3, 4])
assert_(t[0] == 'abc')
assert_(t[1] == 2)
assert_(t[2] == 3)
示例14: test_TakeTransposeInnerOuter
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def test_TakeTransposeInnerOuter(self):
"Test of take, transpose, inner, outer products"
x = arange(24)
y = np.arange(24)
x[5:6] = masked
x = x.reshape(2, 3, 4)
y = y.reshape(2, 3, 4)
assert_equal(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1)))
assert_equal(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1))
assert_equal(np.inner(filled(x, 0), filled(y, 0)),
inner(x, y))
assert_equal(np.outer(filled(x, 0), filled(y, 0)),
outer(x, y))
y = array(['abc', 1, 'def', 2, 3], object)
y[2] = masked
t = take(y, [0, 3, 4])
assert_(t[0] == 'abc')
assert_(t[1] == 2)
assert_(t[2] == 3)
示例15: main
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import inner [as 別名]
def main(args_dict):
text_embedder = FakeTextEmbedder()
text_embedder.embed(["How are you?", "What is the time?", "What time it is?"])
texts = ["Regenerácia vnútroblokov sídlisk mesta Brezno",
"Územný plán mesta Brezno",
"Oprava miestnych komunikácií v katastrálnom území mesta Brezno",
"Kompostéry pre obec Kamenec pod Vtáčnikom",
"Most cez potok Kamenec , Bardejov - mestská časť Dlhá Lúka",
"Oprava miestnych komunikácií v katastrálnom území Trencin"]
word2vec_embedder = Word2VecEmbedder(texts)
embeddings = word2vec_embedder.embed(texts)
print('Similarities:')
for emb1, text1 in zip(embeddings, texts):
for emb2, text2 in zip(embeddings, texts):
similarity = np.inner(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
print(similarity, text1, ' ----', text2)