本文整理汇总了Python中numpy.outer方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.outer方法的具体用法?Python numpy.outer怎么用?Python numpy.outer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.outer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: map_fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def map_fit(interface, state, label, inp):
"""
Function calculates matrices ete and etde for every sample, aggregates and output them.
"""
import numpy as np
ete, etde = 0, 0
out = interface.output(0)
for row in inp:
row = row.strip().split(state["delimiter"]) # split row
if len(row) > 1: # check if row is empty
# intercept term is added to every sample
x = np.array([(0 if v in state["missing_vals"] else float(v)) for i, v in enumerate(row) if
i in state["X_indices"]] + [-1])
# map label value to 1 or -1. If label does not match set error
y = 1 if state["y_map"][0] == row[state["y_index"]] else -1 if state["y_map"][1] == row[
state["y_index"]] else "Error"
ete += np.outer(x, x)
etde += x * y
out.add("etde", etde)
for i, row in enumerate(ete):
out.add(i, row)
示例2: map_fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def map_fit(interface, state, label, inp):
import numpy as np
combiner = dict([(k, [0, 0]) for k in state["samples"].keys()])
out = interface.output(0)
for row in inp:
row = row.strip().split(state["delimiter"])
if len(row) > 1:
x = np.array([1] + [(0 if v in state["missing_vals"] else float(v)) for i, v in enumerate(row) if
i in state["X_indices"]])
y = float(row[state["y_index"]])
for test_id, x1 in state["samples"].iteritems():
w = np.exp(np.dot(-(x1 - x).transpose(), x1 - x) / state["tau"])
combiner[test_id][0] += w * np.outer(x, x)
combiner[test_id][1] += w * x * y
for k, v in combiner.iteritems():
out.add(k + state["delimiter"] + "b", v[1])
for i in range(len(v[0])):
out.add(k + state["delimiter"] + "A" + state["delimiter"] + str(i), v[0][i])
示例3: map_fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def map_fit(interface, state, label, inp):
import numpy as np
a, b = 0, 0
out = interface.output(0)
for row in inp:
row = row.strip().split(state["delimiter"])
if len(row) > 1:
x = np.array([1] + [(0 if v in state["missing_vals"] else float(v)) for i, v in enumerate(row) if
i in state["X_indices"]])
y = float(row[state["y_index"]])
a += np.outer(x, x)
b += x * y
out.add("b", b)
for i, row in enumerate(a):
out.add(i, row)
示例4: _eigen_components
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def _eigen_components(self):
components = [(0, np.diag([1, 1, 1, 0, 1, 0, 0, 1]))]
nontrivial_part = np.zeros((3, 3), dtype=np.complex128)
for ij, w in zip([(1, 2), (0, 2), (0, 1)], self.weights):
nontrivial_part[ij] = w
nontrivial_part[ij[::-1]] = w.conjugate()
assert np.allclose(nontrivial_part, nontrivial_part.conj().T)
eig_vals, eig_vecs = np.linalg.eigh(nontrivial_part)
for eig_val, eig_vec in zip(eig_vals, eig_vecs.T):
exp_factor = -eig_val / np.pi
proj = np.zeros((8, 8), dtype=np.complex128)
nontrivial_indices = np.array([3, 5, 6], dtype=np.intp)
proj[nontrivial_indices[:, np.newaxis], nontrivial_indices] = (
np.outer(eig_vec.conjugate(), eig_vec))
components.append((exp_factor, proj))
return components
示例5: quat2mat
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def quat2mat(quaternion):
"""
Converts given quaternion (x, y, z, w) to matrix.
Args:
quaternion: vec4 float angles
Returns:
3x3 rotation matrix
"""
q = np.array(quaternion, dtype=np.float32, copy=True)[[3, 0, 1, 2]]
n = np.dot(q, q)
if n < EPS:
return np.identity(3)
q *= math.sqrt(2.0 / n)
q = np.outer(q, q)
return np.array(
[
[1.0 - q[2, 2] - q[3, 3], q[1, 2] - q[3, 0], q[1, 3] + q[2, 0]],
[q[1, 2] + q[3, 0], 1.0 - q[1, 1] - q[3, 3], q[2, 3] - q[1, 0]],
[q[1, 3] - q[2, 0], q[2, 3] + q[1, 0], 1.0 - q[1, 1] - q[2, 2]],
]
)
示例6: quintic_spline
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def quintic_spline(dx, dtype=np.float64):
def inner(x):
return 1 + x ** 3 / 12 * (-95 + 138 * x - 55 * x ** 2)
def middle(x):
return (x - 1) * (x - 2) / 24 * (-138 + 348 * x - 249 * x ** 2 + 55 * x ** 3)
def outer(x):
return (x - 2) * (x - 3) ** 2 / 24 * (-54 + 50 * x - 11 * x ** 2)
window = np.arange(-3, 4)
x = np.abs(dx - window)
result = np.piecewise(
x,
[x <= 1, (x > 1) & (x <= 2), (x > 2) & (x <= 3)],
[lambda x: inner(x), lambda x: middle(x), lambda x: outer(x)],
)
return result, window
示例7: tdhf_frozen_mask
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def tdhf_frozen_mask(eri, kind="ov"):
if isinstance(eri.nocc, int):
nocc = int(eri.model.mo_occ.sum() // 2)
mask = eri.space
else:
nocc = numpy.array(tuple(int(i.sum() // 2) for i in eri.model.mo_occ))
assert numpy.all(nocc == nocc[0])
assert numpy.all(eri.space == eri.space[0, numpy.newaxis, :])
nocc = nocc[0]
mask = eri.space[0]
mask_o = mask[:nocc]
mask_v = mask[nocc:]
if kind == "ov":
mask_ov = numpy.outer(mask_o, mask_v).reshape(-1)
return numpy.tile(mask_ov, 2)
elif kind == "1ov":
return numpy.outer(mask_o, mask_v).reshape(-1)
elif kind == "sov":
mask_ov = numpy.outer(mask_o, mask_v).reshape(-1)
nk = len(eri.model.mo_occ)
return numpy.tile(mask_ov, 2 * nk ** 2)
elif kind == "o,v":
return mask_o, mask_v
示例8: test_b2q
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def test_b2q():
# conversion of b matrix to q
q = np.array([1,2,3])
s = np.sqrt(np.sum(q * q)) # vector norm
B = np.outer(q, q)
assert_array_almost_equal(q*s, B2q(B))
q = np.array([1,2,3])
# check that the sign of the vector as positive x convention
B = np.outer(-q, -q)
assert_array_almost_equal(q*s, B2q(B))
q = np.array([-1, 2, 3])
B = np.outer(q, q)
assert_array_almost_equal(-q*s, B2q(B))
B = np.eye(3) * -1
assert_raises(ValueError, B2q, B)
# no error if we up the tolerance
q = B2q(B, tol=1)
示例9: optimization_variables_avg_QCQP
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def optimization_variables_avg_QCQP():
l = np.array([4, 1, 1, -5, 3], dtype=float)
A = np.array([[1, 2, 3, 4],
[3, 1, 2, 2],
[2, 3, 1, 1],
[1, 3, 5, 1]], dtype=float)
A = np.vstack([-np.mean(A, axis=1), A - np.mean(A, axis=1)])
Q = A.dot(A.T)
A_in = np.array([[1, 5, 2, 4],
[5, 2, 1, 2],
[0, 1, 1, 1],
[2, 4, 3, 5]], dtype=float)
A_in = np.vstack([-np.mean(A_in, axis=1), A_in - np.mean(A_in, axis=1)])
Q_in = A_in.dot(A_in.T)
Q_in += np.outer(l, l)
return l, Q, np.ones(5), Q_in
示例10: test_both_limit_angle_Q_iteration
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def test_both_limit_angle_Q_iteration(self, optimization_variables_avg_QCQP):
l, Q, A, Q_in = optimization_variables_avg_QCQP
# l, Q, A = optimization_variables_avg
# Q_in = 6 * np.eye(len(l)) + np.outer(l, l)
max_angle = 20
m = 2e-3
m1 = 4e-3
f = .01
x = optimization_methods.optimize_focality(l, Q, f,
max_el_current=m,
max_total_current=m1,
Qin=Q_in,
max_angle=max_angle)
x_sp = optimize_focality(
l, Q, f, max_el_current=m, max_total_current=m1,
Qin=Q_in, max_angle=max_angle)
assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
assert np.all(np.abs(x) <= m + 1e-4)
assert np.isclose(np.sum(x), 0)
assert np.isclose(l.dot(x), f)
assert np.arccos(l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
assert np.allclose(x.dot(Q.dot(x)), x_sp.dot(Q.dot(x_sp)), rtol=1e-4, atol=1e-5)
示例11: mutcoh
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def mutcoh(A):
""" mutcoh calculates the mutual coherence of a matrix A, i.e. the cosine
of the smallest angle between two columns
mutual_coherence = mutcoh(A)
Input:
A ... m x n matrix
Output:
mutual_coherence """
T = np.dot(A.conj().T,A)
s = np.sqrt(np.diag(T))
S = np.diag(s)
mutual_coherence = np.max(1.0*(T-S)/np.outer(s,s))
return mutual_coherence
示例12: reflection_matrix
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def reflection_matrix(point, normal):
"""Return matrix to mirror at plane defined by point and normal vector.
>>> v0 = numpy.random.random(4) - 0.5
>>> v0[3] = 1.0
>>> v1 = numpy.random.random(3) - 0.5
>>> R = reflection_matrix(v0, v1)
>>> numpy.allclose(2., numpy.trace(R))
True
>>> numpy.allclose(v0, numpy.dot(R, v0))
True
>>> v2 = v0.copy()
>>> v2[:3] += v1
>>> v3 = v0.copy()
>>> v2[:3] -= v1
>>> numpy.allclose(v2, numpy.dot(R, v3))
True
"""
normal = unit_vector(normal[:3])
M = numpy.identity(4)
M[:3, :3] -= 2.0 * numpy.outer(normal, normal)
M[:3, 3] = (2.0 * numpy.dot(point[:3], normal)) * normal
return M
示例13: shear_matrix
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [as 别名]
def shear_matrix(angle, direction, point, normal):
"""Return matrix to shear by angle along direction vector on shear plane.
The shear plane is defined by a point and normal vector. The direction
vector must be orthogonal to the plane's normal vector.
A point P is transformed by the shear matrix into P" such that
the vector P-P" is parallel to the direction vector and its extent is
given by the angle of P-P'-P", where P' is the orthogonal projection
of P onto the shear plane.
>>> angle = (random.random() - 0.5) * 4*math.pi
>>> direct = numpy.random.random(3) - 0.5
>>> point = numpy.random.random(3) - 0.5
>>> normal = numpy.cross(direct, numpy.random.random(3))
>>> S = shear_matrix(angle, direct, point, normal)
>>> numpy.allclose(1.0, numpy.linalg.det(S))
True
"""
normal = unit_vector(normal[:3])
direction = unit_vector(direction[:3])
if abs(numpy.dot(normal, direction)) > 1e-6:
raise ValueError("direction and normal vectors are not orthogonal")
angle = math.tan(angle)
M = numpy.identity(4)
M[:3, :3] += angle * numpy.outer(direction, normal)
M[:3, 3] = -angle * numpy.dot(point[:3], normal) * direction
return M
示例14: test_4
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [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
示例15: test_testTakeTransposeInnerOuter
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import outer [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)