我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用numpy.random.standard_normal()。
def __init__(self, shape, input_data): assert isinstance(shape, (int, list, tuple)) assert isinstance(input_data, (list, np.ndarray)) if isinstance(shape, int): shape = [shape] if isinstance(input_data, list): input_data = np.array(input_data, dtype=np.float32) input_shape = tuple(input_data.shape) assert 2 == len(input_shape) self.shape = tuple(shape) self.input_layer = input_data self.input_num = input_shape[0] self.input_dim = input_shape[1] self.output_layer = rand.standard_normal((self.shape[0]*self.shape[1], self.input_dim)) x, y = np.meshgrid(range(self.shape[0]), range(self.shape[1])) self.index_map = np.hstack((y.flatten()[:, np.newaxis], x.flatten()[:, np.newaxis])) self._param_input_length_ratio = 0.25 self._life = self.input_num * self._param_input_length_ratio self._param_neighbor = 0.25 self._param_learning_rate = 0.1
def sample_weights(self): """ Create a random set of expansion coefficients. """ from numpy import add from numpy.random import standard_normal k = self._periodicities() k = add.outer(k ** 2, k ** 2) self.set([standard_normal(k.shape) for i in range(4)]) self.normalize(True)
def truncated_normal(shape=None, mu=0., sigma=1., x_min=None, x_max=None): """ Generates random variates from a lower-and upper-bounded normal distribution @param shape: shape of the random sample @param mu: location parameter @param sigma: width of the distribution (sigma >= 0.) @param x_min: lower bound of variate @param x_max: upper bound of variate @return: random variates of lower-bounded normal distribution """ from scipy.special import erf, erfinv from numpy.random import standard_normal from numpy import inf, sqrt if x_min is None and x_max is None: return standard_normal(shape) * sigma + mu elif x_min is None: x_min = -inf elif x_max is None: x_max = inf x_min = max(-1e300, x_min) x_max = min(+1e300, x_max) var = sigma ** 2 + 1e-300 sigma = sqrt(2 * var) a = erf((x_min - mu) / sigma) b = erf((x_max - mu) / sigma) return probability_transform(shape, erfinv, a, b) * sigma + mu
def inv_gaussian(mu=1., _lambda=1., shape=None): """ Generate random samples from inverse gaussian. """ from numpy.random import standard_normal, random from numpy import sqrt, less_equal, clip mu_2l = mu / _lambda / 2. Y = mu * standard_normal(shape) ** 2 X = mu + mu_2l * (Y - sqrt(4 * _lambda * Y + Y ** 2)) U = random(shape) m = less_equal(U, mu / (mu + X)) return clip(m * X + (1 - m) * mu ** 2 / X, 1e-308, 1e308)
def sample(self, n_samples, inv_T=1): from numpy.random import standard_normal from numpy import sqrt, sum from csb.statistics.rand import truncated_gamma x = standard_normal((self.d, n_samples)) x /= sqrt(sum(x ** 2, 0)) r = truncated_gamma(n_samples, 0.5 * self.d, self.k * inv_T, 0., 0.5) r = (2 * r) ** 0.5 return (x * r).T
def init_data(self): self.theta = randn((self.users, self.features)) self.X = randn((self.movies, self.features))
def generate_data(missing, datatype, const=False, ntk=(971, 7, 5), other_effects=0, rng=None): if rng is None: np.random.seed(12345) else: np.random.set_state(rng.get_state()) n, t, k = ntk k += const x = standard_normal((k, t, n)) beta = np.arange(1, k + 1)[:, None, None] / k y = (x * beta).sum(0) + standard_normal((t, n)) + 2 * standard_normal((1, n)) w = np.random.chisquare(5, (t, n)) / 5 c = None if other_effects == 1: cats = ['Industries'] else: cats = ['cat.' + str(i) for i in range(other_effects)] if other_effects: c = np.random.randint(0, 4, (other_effects, t, n)) vcats = ['varcat.' + str(i) for i in range(2)] vc2 = np.ones((2, t, 1)) @ np.random.randint(0, n // 2, (2, 1, n)) vc1 = vc2[[0]] if const: x[0] = 1.0 if missing > 0: locs = np.random.choice(n * t, int(n * t * missing)) y.flat[locs] = np.nan locs = np.random.choice(n * t * k, int(n * t * k * missing)) x.flat[locs] = np.nan if datatype in ('pandas', 'xarray'): entities = ['firm' + str(i) for i in range(n)] time = pd.date_range('1-1-1900', periods=t, freq='A-DEC') var_names = ['x' + str(i) for i in range(k)] y = pd.DataFrame(y, index=time, columns=entities) w = pd.DataFrame(w, index=time, columns=entities) x = pd.Panel(x, items=var_names, major_axis=time, minor_axis=entities) c = pd.Panel(c, items=cats, major_axis=time, minor_axis=entities) vc1 = pd.Panel(vc1, items=vcats[:1], major_axis=time, minor_axis=entities) vc2 = pd.Panel(vc2, items=vcats, major_axis=time, minor_axis=entities) if datatype == 'xarray': import xarray as xr x = xr.DataArray(x) y = xr.DataArray(y) w = xr.DataArray(w) c = xr.DataArray(c) vc1 = xr.DataArray(vc1) vc2 = xr.DataArray(vc2) if rng is not None: rng.set_state(np.random.get_state()) return AttrDict(y=y, x=x, w=w, c=c, vc1=vc1, vc2=vc2)