本文整理汇总了Python中pylab.randn函数的典型用法代码示例。如果您正苦于以下问题:Python randn函数的具体用法?Python randn怎么用?Python randn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了randn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: normal_test_case
def normal_test_case():
'''
Runs a test case with simulated data from a normal distribution.
'''
obs, fa, dur = [], [], []
for n in range(15):
d, f, o = make_test_data(
5, split=min(plt.rand()*50+120, 170),
intercept=plt.rand()*50 + 225,
slope1=1 + plt.randn()/0.75, slope2=plt.randn()/.75)
obs.append(o+n)
fa.append(f)
dur.append(d)
plt.plot(f, d, 'o', alpha=0.1)
dur, fa, obs = (np.hstack(dur)[:, np.newaxis],
np.hstack(fa)[:, np.newaxis],
np.hstack(obs)[:, np.newaxis])
dur_mean = dur.mean()
dur_std = dur.std()
dur = (dur-dur_mean)/dur_std
m = normal_model(dur, fa, obs)
trace = sample_model(m, 5000)
predict(trace, 5, 2500, {'mean': dur_mean, 'std': dur_std})
plt.figure()
traceplot(trace, 2, 2500)
return dur, fa, obs, (dur_mean, dur_std), trace
示例2: gamma_test_case
def gamma_test_case():
'''
Runs a test case with simulated data from a normal distribution.
'''
obs, fa, dur = [], [], []
delta_angle = np.arange(180)
for n in range(15):
mode = piecewise_predictor(
delta_angle,
100 + plt.randn()*20,
250 + plt.randn()*20,
1 + plt.randn()/2.0,
-1 + plt.randn()/2.0)
a, b = np_gamma_params(mode, 10)
for _ in range(10):
d = gamma.rvs(a=a, scale=1.0/b)
fa.append(delta_angle)
dur.append(d)
obs.append(d*0+n)
dur, fa, obs = np.concatenate(dur), np.concatenate(fa), np.concatenate(obs)
m = gamma_model(dur, fa, obs.astype(int))
trace = sample_model(m, 5000)
predict(trace, 5, 2500 )
plt.figure()
traceplot(trace, 2, 2500)
return dur, fa, obs, trace
示例3: __init__
def __init__(self, numstates, numclasses):
self.numstates = numstates
self.numclasses = numclasses
self.numparams = self.numclasses * self.numstates\
+ self.numstates\
+ self.numstates**2
self.params = zeros(self.numparams, dtype=float)
self.logEmissionProbs = \
self.params[:self.numclasses * self.numstates].reshape(
self.numstates, self.numclasses)
self.logInitProbs = self.params[self.numclasses * self.numstates:
self.numclasses * self.numstates+
self.numstates]
self.logTransitionProbs = self.params[-self.numstates**2:].reshape(
self.numstates, self.numstates)
self.logEmissionProbs[:] = \
ones((self.numstates, self.numclasses),dtype=numpy.double)\
/numpy.double(self.numclasses) +\
randn(self.numstates, self.numclasses)*0.001
self.logEmissionProbs /= self.logEmissionProbs.sum(1)[:,newaxis]
self.logEmissionProbs = log(self.logEmissionProbs)
self.logInitProbs[:] = ones(self.numstates, dtype=float) \
/ self.numstates+\
randn(self.numstates)*0.001
self.logInitProbs /= self.logInitProbs.sum()
self.logInitProbs[:] = log(self.logInitProbs)
self.logTransitionProbs[:] = ones((self.numstates, self.numstates),
dtype=float)/self.numstates+\
randn(self.numstates,self.numstates)*0.001
self.logTransitionProbs /= self.logTransitionProbs.sum(1)[:,newaxis]
self.logTransitionProbs[:] = log(self.logTransitionProbs)
示例4: plot_F_and_pi
def plot_F_and_pi(F, pi, causes, title=""):
N, T, J = F.shape
pl.figure(figsize=(0.5 * T, 1.0 * J))
left = 2.0 / (T + 5.0)
right = 1 - 0.05 / T
bottom = 2.0 / (T + 5.0)
top = 1 - 0.05 / T
xmax = F.max()
dj = (top - bottom) / J
dt = (right - left) / T
ax = {}
for jj, j in enumerate(sorted(range(J), key=lambda j: pi[:, :, j].mean())):
for t in range(T):
pl.axes([left + t * dt, bottom + jj * dj, dt, dj])
pl.plot(pl.randn(N), F[:, t, j], "b.", alpha=0.5, zorder=-100)
pl.plot(0.5 * pl.randn(N), pi[:N, t, j], "g.", alpha=0.5, zorder=100)
# pi[:,t,j].sort()
# below = pi[:, t, j].mean() - pi[:,t,j][.025*N]
# above = pi[:,t,j][.975*N] - pi[:, t, j].mean()
# pl.errorbar([0], pi[:, t, j].mean(), [[below], [above]],
# fmt='gs', ms=10, mew=1, mec='white', linewidth=3, capsize=10,
# zorder=100)
pl.text(
-2.75,
xmax * 0.9,
"%.0f\n%.0f\n%.0f"
% (
100 * F[:, t, j].mean(),
100 * pi[:, t, j].mean(),
100 * pi[:, t, j].mean() - 100 * F[:, t, j].mean(),
),
va="top",
ha="left",
)
pl.xticks([])
pl.yticks([0.25, 0.5, 0.75])
if jj == 0:
pl.xlabel("%d\n%.0f" % (t + 1980, 100 * F[:, t, :].sum() / N))
if t > 0:
pl.yticks([])
else:
pl.ylabel(causes[j])
pl.axis([-3, 3, 0, xmax])
if title:
pl.figtext(0.01, 0.99, title, va="top", ha="left")
示例5: example_histogram_3
def example_histogram_3():
# first create a single histogram
mu, sigma = 200, 25
x = mu + sigma * plb.randn(10000)
plb.figure(6)
# finally: make a multiple-histogram of data-sets with different length
x0 = mu + sigma * plb.randn(10000)
x1 = mu + sigma * plb.randn(7000)
x2 = mu + sigma * plb.randn(3000)
n, bins, patches = plb.hist([x0, x1, x2], 10, histtype='bar')
plb.show()
示例6: simulate
def simulate(self, f_u, x0, tf):
"""
Simulate the system.
Parameters
----------
f_u: The input function f_u(t, x, i)
x0: The initial state.
tf: The final time.
Return
------
data : A StateSpaceDataArray object.
"""
#pylint: disable=too-many-locals, no-member
x0 = pl.matrix(x0)
assert x0.shape[1] == 1
t = 0
x = x0
dt = self.dt
data = StateSpaceDataList([], [], [], [])
i = 0
n_x = self.A.shape[0]
n_y = self.C.shape[0]
assert pl.matrix(f_u(0, x0, 0)).shape[1] == 1
assert pl.matrix(f_u(0, x0, 0)).shape[0] == n_y
# take square root of noise cov to prepare for noise sim
if pl.norm(self.Q) > 0:
sqrtQ = scipy.linalg.sqrtm(self.Q)
else:
sqrtQ = self.Q
if pl.norm(self.R) > 0:
sqrtR = scipy.linalg.sqrtm(self.R)
else:
sqrtR = self.R
# main simulation loop
while t + dt < tf:
u = f_u(t, x, i)
v = sqrtR.dot(pl.randn(n_y, 1))
y = self.measurement(x, u, v)
data.append(t, x, y, u)
w = sqrtQ.dot(pl.randn(n_x, 1))
x = self.dynamics(x, u, w)
t += dt
i += 1
return data.to_StateSpaceDataArray()
示例7: genExampleAxisAligned
def genExampleAxisAligned():
# first choose a label
y = ''
feats = {}
if pylab.rand() < 0.5: # negative example
# from Nor([-1,0], 1)
y = '-1'
feats['x'] = pylab.randn() - 1
feats['y'] = pylab.randn()
else:
# from Nor([+1,0], 1)
y = '1'
feats['x'] = pylab.randn() + 1
feats['y'] = pylab.randn()
return (y, feats)
示例8: matplotlib_plot
def matplotlib_plot(fname):
Xs, Ys = 6, 6
fig = pylab.figure(figsize=(Xs, Ys), frameon=False)
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False)
ax.set_xticks([])
ax.set_yticks([])
N = 1000
x, y = pylab.randn(N), pylab.randn(N)
color = pylab.randn(N)
size = abs(400*pylab.randn(N))
p = pylab.scatter(x, y, c=color, s=size, alpha=0.75)
pylab.xlim(-2.0, 2.0)
pylab.ylim(-2.0, 2.0)
pylab.savefig(fname, dpi=200)
return Xs, Ys
示例9: estimate_vol_vol
def estimate_vol_vol(self):
# dummy time series: must be replaced by a reader from an external source
rng = pd.date_range(start = '2010-01-01', end = '2011-01-01', freq='D')
tmp_0 = 100000. + 10000.*pl.randn(len(rng))
ts_volume = pd.Series(tmp_0, index=rng)
tmp_1 = 0.02 + 0.002*pl.randn(len(rng))
ts_volatility = pd.Series(tmp_1, index=rng)
# estimation of the daily volume and of the daily volatility as a flat average of the previuos n_days_mav
n_days_mav = 10
period_start = pd.to_datetime('2010-03-01') + pd.DateOffset(days=-(n_days_mav+1))
period_end = pd.to_datetime('2010-03-01') + pd.DateOffset(days=-1)
self.volume_est = ts_volume[period_start:period_end].mean()
self.volatility_est = ts_volatility[period_start:period_end].mean()
示例10: test_speriodogram_2d
def test_speriodogram_2d():
data = randn(1024,2)
speriodogram(data)
data = np.array([marple_data, marple_data]).reshape(64,2)
speriodogram(data)
示例11: __init__
def __init__(self,numin,numclasses):
self.numin = numin
self.numclasses = numclasses
self.params = 0.01 * randn(self.numin*self.numclasses+self.numclasses)
self.scorefunc = logreg_score(self.numin,self.numclasses,self.params)
self.scorefuncs = [scorefunc]
Contrastive.__init__(self,normalizeacrosscliques=False)
示例12: __init__
def __init__(self,numstates, numdims):
self.numstates = numstates
self.numdims = numdims
self.numparams = self.numdims * self.numstates\
+ self.numdims**2 * self.numstates\
+ self.numstates\
+ self.numstates**2
self.params = zeros(self.numparams, dtype=float)
self.means = self.params[:self.numdims*self.numstates].reshape(
self.numdims,self.numstates)
self.covs = self.params[self.numdims * self.numstates:
self.numdims*self.numstates+
self.numdims**2 * self.numstates].reshape(
self.numdims, self.numdims, self.numstates)
self.logInitProbs = self.params[self.numdims * self.numstates +
self.numdims**2 * self.numstates:
self.numdims * self.numstates +
self.numdims**2 * self.numstates+
self.numstates]
self.logTransitionProbs = self.params[-self.numstates**2:].reshape(
self.numstates, self.numstates)
self.means[:] = 0.1 * randn(self.numdims, self.numstates)
for k in range(self.numstates):
self.covs[:,:,k] = eye(self.numdims) * 0.1;
self.logInitProbs[:] = log(ones(self.numstates, dtype=float) /
self.numstates)
self.logTransitionProbs[:] = log(ones((self.numstates,
self.numstates), dtype=float) /
self.numstates)
示例13: __init__
def __init__(self,numdims):
self.numdims = numdims
self.params = 0.001 * randn(numdims*2)
self.wp = self.params[:numdims]
self.wm = self.params[numdims:]
self.b = 0.0
self.t = 1.0
示例14: synthbeats
def synthbeats(duration, meanhr=60, stdhr=1, samplingfreq=250, sinfreq=None):
#Minimaly based on the parameters from:
#http://physionet.cps.unizar.es/physiotools/ecgsyn/Matlab/ecgsyn.m
#If frequ exist it will be used to generate a sin instead of using rand
#Inputs: duration in seconds
#Returns: signal, peaks
t = np.arange(duration * samplingfreq) / float(samplingfreq)
signal = np.zeros(len(t))
print(len(t))
print(len(signal))
if sinfreq == None:
npeaks = 1.2 * (duration * meanhr / 60)
# add 20% more beats for some cummulative error
hr = pl.randn(npeaks) * stdhr + meanhr
peaks = pl.cumsum(60. / hr) * samplingfreq
peaks = peaks.astype('int')
peaks = peaks[peaks < t[-1] * samplingfreq]
else:
hr = meanhr + sin(2 * pi * t * sinfreq) * float(stdhr)
index = int(60. / hr[0] * samplingfreq)
peaks = []
while index < len(t):
peaks += [index]
index += int(60. / hr[index] * samplingfreq)
signal[peaks] = 1.0
return t, signal, peaks
示例15: generate_fe
def generate_fe(out_fname='data.csv'):
""" Generate random data based on a fixed effects model
This function generates data for all countries in all regions, based on the model::
Y_r,c,t = beta * X_r,c,t
beta = [10., -.5, .1, .1, -.1, 0., 0., 0., 0., 0.]
X_r,c,t[0] = 1
X_r,c,t[1] = t - 1990.
X_r,c,t[k] ~ N(0, 1) for k >= 2
"""
c4 = countries_by_region()
a = 20.
beta = [10., -.5, .1, .1, -.1, 0., 0., 0., 0., 0.]
data = col_names()
for t in time_range:
for r in c4:
for c in c4[r]:
x = [1] + [t-1990.] + list(pl.randn(8))
y = float(pl.dot(beta, x))
se = 0.
data.append([r, c, t, a, y, se] + list(x))
write(data, out_fname)