本文整理匯總了Python中scipy.rand方法的典型用法代碼示例。如果您正苦於以下問題:Python scipy.rand方法的具體用法?Python scipy.rand怎麽用?Python scipy.rand使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy
的用法示例。
在下文中一共展示了scipy.rand方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: setUp
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def setUp(self):
random.seed(0) # make tests repeatable
self.real_matrices = []
self.real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 1], 5, 5))
self.real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 1], 4, 5))
self.real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 2], 5, 5))
self.real_matrices.append(rand(3,3))
self.real_matrices.append(rand(5,4))
self.real_matrices.append(rand(4,5))
self.real_matrices = [csc_matrix(x).astype('d') for x
in self.real_matrices]
self.complex_matrices = [x.astype(np.complex128)
for x in self.real_matrices]
_DeprecationAccept.setUp(self)
# Skip methods if umfpack not present
示例2: compare_solutions
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def compare_solutions(A,B,m):
n = A.shape[0]
numpy.random.seed(0)
V = rand(n,m)
X = linalg.orth(V)
eigs,vecs = lobpcg(A, X, B=B, tol=1e-5, maxiter=30)
eigs.sort()
#w,v = symeig(A,B)
w,v = eig(A,b=B)
w.sort()
assert_almost_equal(w[:int(m/2)],eigs[:int(m/2)],decimal=2)
#from pylab import plot, show, legend, xlabel, ylabel
#plot(arange(0,len(w[:m])),w[:m],'bx',label='Results by symeig')
#plot(arange(0,len(eigs)),eigs,'r+',label='Results by lobpcg')
#legend()
#xlabel(r'Eigenvalue $i$')
#ylabel(r'$\lambda_i$')
#show()
示例3: setUp
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def setUp(self):
random.seed(0) # make tests repeatable
real_matrices = []
real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 1], 5, 5))
real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 1], 4, 5))
real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 2], 5, 5))
real_matrices.append(rand(3,3))
real_matrices.append(rand(5,4))
real_matrices.append(rand(4,5))
self.real_matrices = [csc_matrix(x).astype('d')
for x in real_matrices]
self.complex_matrices = [x.astype(np.complex128)
for x in self.real_matrices]
self.real_int64_matrices = [_to_int64(x)
for x in self.real_matrices]
self.complex_int64_matrices = [_to_int64(x)
for x in self.complex_matrices]
_DeprecationAccept.setUp(self)
示例4: generate_data
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def generate_data(N, S, L):
# generate genetics
G = 1.0 * (sp.rand(N, S) < 0.2)
G -= G.mean(0)
G /= G.std(0) * sp.sqrt(G.shape[1])
# generate latent phenotypes
Zg = sp.dot(G, sp.randn(G.shape[1], L))
Zn = sp.randn(N, L)
# generate variance exapleind
vg = sp.linspace(0.8, 0, L)
# rescale and sum
Zg *= sp.sqrt(vg / Zg.var(0))
Zn *= sp.sqrt((1 - vg) / Zn.var(0))
Z = Zg + Zn
return Z, G
示例5: test_warm_start
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def test_warm_start(self):
# Big problem
sp.random.seed(2)
self.n = 100
self.m = 200
self.A = sparse.random(self.m, self.n, density=0.9, format='csc')
self.l = -sp.rand(self.m) * 2.
self.u = sp.rand(self.m) * 2.
P = sparse.random(self.n, self.n, density=0.9)
self.P = sparse.triu(P.dot(P.T), format='csc')
self.q = sp.randn(self.n)
# Setup solver
self.model = osqp.OSQP()
self.model.setup(P=self.P, q=self.q, A=self.A, l=self.l, u=self.u,
**self.opts)
# Solve problem with OSQP
res = self.model.solve()
# Store optimal values
x_opt = res.x
y_opt = res.y
tot_iter = res.info.iter
# Warm start with zeros and check if number of iterations is the same
self.model.warm_start(x=np.zeros(self.n), y=np.zeros(self.m))
res = self.model.solve()
self.assertEqual(res.info.iter, tot_iter)
# Warm start with optimal values and check that number of iter < 10
self.model.warm_start(x=x_opt, y=y_opt)
res = self.model.solve()
self.assertLess(res.info.iter, 10)
示例6: test_primal_infeasible_problem
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def test_primal_infeasible_problem(self):
# Simple QP problem
sp.random.seed(4)
self.n = 50
self.m = 500
# Generate random Matrices
Pt = sparse.random(self.n, self.n)
self.P = sparse.triu(Pt.T.dot(Pt), format='csc')
self.q = sp.randn(self.n)
self.A = sparse.random(self.m, self.n).tolil() # Lil for efficiency
self.u = 3 + sp.randn(self.m)
self.l = -3 + sp.randn(self.m)
# Make random problem primal infeasible
self.A[int(self.n/2), :] = self.A[int(self.n/2)+1, :]
self.l[int(self.n/2)] = self.u[int(self.n/2)+1] + 10 * sp.rand()
self.u[int(self.n/2)] = self.l[int(self.n/2)] + 0.5
# Convert A to csc
self.A = self.A.tocsc()
self.model = osqp.OSQP()
self.model.setup(P=self.P, q=self.q, A=self.A, l=self.l, u=self.u,
**self.opts)
# Solve problem with OSQP
res = self.model.solve()
# Assert close
self.assertEqual(res.info.status_val,
constant('OSQP_PRIMAL_INFEASIBLE'))
示例7: compare_solutions
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def compare_solutions(A,B,m):
n = A.shape[0]
np.random.seed(0)
V = rand(n,m)
X = linalg.orth(V)
eigs,vecs = lobpcg(A, X, B=B, tol=1e-5, maxiter=30)
eigs.sort()
w,v = eig(A,b=B)
w.sort()
assert_almost_equal(w[:int(m/2)],eigs[:int(m/2)],decimal=2)
示例8: test_diagonal
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def test_diagonal():
# This test was moved from '__main__' in lobpcg.py.
# Coincidentally or not, this is the same eigensystem
# required to reproduce arpack bug
# http://forge.scilab.org/index.php/p/arpack-ng/issues/1397/
# even using the same n=100.
np.random.seed(1234)
# The system of interest is of size n x n.
n = 100
# We care about only m eigenpairs.
m = 4
# Define the generalized eigenvalue problem Av = cBv
# where (c, v) is a generalized eigenpair,
# and where we choose A to be the diagonal matrix whose entries are 1..n
# and where B is chosen to be the identity matrix.
vals = np.arange(1, n+1, dtype=float)
A = scipy.sparse.diags([vals], [0], (n, n))
B = scipy.sparse.eye(n)
# Let the preconditioner M be the inverse of A.
M = scipy.sparse.diags([np.reciprocal(vals)], [0], (n, n))
# Pick random initial vectors.
X = np.random.rand(n, m)
# Require that the returned eigenvectors be in the orthogonal complement
# of the first few standard basis vectors.
m_excluded = 3
Y = np.eye(n, m_excluded)
eigs, vecs = lobpcg(A, X, B, M=M, Y=Y, tol=1e-4, maxiter=40, largest=False)
assert_allclose(eigs, np.arange(1+m_excluded, 1+m_excluded+m))
_check_eigen(A, eigs, vecs, rtol=1e-3, atol=1e-3)
示例9: test_hermitian
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def test_hermitian():
np.random.seed(1234)
sizes = [3, 10, 50]
ks = [1, 3, 10, 50]
gens = [True, False]
for size, k, gen in itertools.product(sizes, ks, gens):
if k > size:
continue
H = np.random.rand(size, size) + 1.j * np.random.rand(size, size)
H = 10 * np.eye(size) + H + H.T.conj()
X = np.random.rand(size, k)
if not gen:
B = np.eye(size)
w, v = lobpcg(H, X, maxiter=5000)
w0, v0 = eigh(H)
else:
B = np.random.rand(size, size) + 1.j * np.random.rand(size, size)
B = 10 * np.eye(size) + B.dot(B.T.conj())
w, v = lobpcg(H, X, B, maxiter=5000)
w0, v0 = eigh(H, B)
for wx, vx in zip(w, v.T):
# Check eigenvector
assert_allclose(np.linalg.norm(H.dot(vx) - B.dot(vx) * wx) / np.linalg.norm(H.dot(vx)),
0, atol=5e-4, rtol=0)
# Compare eigenvalues
j = np.argmin(abs(w0 - wx))
assert_allclose(wx, w0[j], rtol=1e-4)
示例10: sequence_generation
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import rand [as 別名]
def sequence_generation(volume, duration, c, fs, max_rate=10000):
# repeated constant
fpcv = 4 * np.pi * c ** 3 / volume
# initial time
t0 = ((2 * np.log(2)) / fpcv) ** (1.0 / 3.0)
times = [t0]
while times[-1] < t0 + duration:
# uniform random variable
z = np.random.rand()
# rate of the point process at this time
mu = np.minimum(fpcv * (t0 + times[-1]) ** 2, max_rate)
# time interval to next point
dt = np.log(1 / z) / mu
times.append(times[-1] + dt)
# convert from continuous to discrete time
indices = (np.array(times) * fs).astype(np.int)
seq = np.zeros(indices[-1] + 1)
seq[indices] = np.random.choice([1, -1], size=len(indices))
return seq