当前位置: 首页>>代码示例>>Python>>正文


Python random.gamma方法代码示例

本文整理汇总了Python中numpy.random.gamma方法的典型用法代码示例。如果您正苦于以下问题:Python random.gamma方法的具体用法?Python random.gamma怎么用?Python random.gamma使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在numpy.random的用法示例。


在下文中一共展示了random.gamma方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: rand_mask

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def rand_mask(im_size):
    mask = np.zeros(shape=(im_size, im_size), dtype=np.uint8);
    cx = (im_size - 1) / 2.0;
    cy = (im_size - 1) / 2.0;
    a = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
    b = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
    ratio = float(max(a, b)) / min(a, b);
    mask = add_ellipse(mask, cx, cy, uniform() * math.pi, a, b);
    for i in range(np.random.randint(2, 5)):
        x = cx;
        y = cy;
        while ((x - cx)**2 + (y - cy)**2)**0.5 < im_size * 0.3:
            x = np.random.randint(0, im_size);
            y = np.random.randint(0, im_size);

        a = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
        b = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
        mask = add_ellipse(mask, x, y, uniform() * math.pi, a, b);

    return (mask, ratio); 
开发者ID:SBU-BMI,项目名称:u24_lymphocyte,代码行数:22,代码来源:ellipse.py

示例2: rand_mask

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def rand_mask(im_size):
    mask = np.zeros(shape=(im_size, im_size), dtype=np.uint8);
    cx = (im_size - 1) / 2.0;
    cy = (im_size - 1) / 2.0;
    a = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
    b = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
    ratio = float(max(a, b)) / min(a, b);
    mask = add_ellipse(mask, cx, cy, uniform() * math.pi, a, b);
    for i in range(np.random.randint(0, 4)):
        x = cx;
        y = cy;
        while ((x - cx)**2 + (y - cy)**2)**0.5 < im_size * 0.3:
            x = np.random.randint(0, im_size);
            y = np.random.randint(0, im_size);

        a = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
        b = im_size * (gamma(2.2, 0.6) / 10.0 + 0.04);
        mask = add_ellipse(mask, x, y, uniform() * math.pi, a, b);

    return (mask, ratio); 
开发者ID:SBU-BMI,项目名称:u24_lymphocyte,代码行数:22,代码来源:ellipse.py

示例3: _init_component

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def _init_component(self, m, dim):
        assert self.mode_dims[m] == dim
        K = self.n_components
        s = self.smoothness
        if not self.debug:
            gamma_DK = s * rn.gamma(s, 1. / s, size=(dim, K))
            delta_DK = s * rn.gamma(s, 1. / s, size=(dim, K))
        else:
            gamma_DK = s * np.ones((dim, K))
            delta_DK = s * np.ones((dim, K))
        self.gamma_DK_M[m] = gamma_DK
        self.delta_DK_M[m] = delta_DK
        self.E_DK_M[m] = gamma_DK / delta_DK
        self.sumE_MK[m, :] = self.E_DK_M[m].sum(axis=0)
        self.G_DK_M[m] = np.exp(sp.psi(gamma_DK) - np.log(delta_DK))
        if m == 0 or not self.debug:
            self.beta_M[m] = 1. / self.E_DK_M[m].mean() 
开发者ID:aschein,项目名称:bptf,代码行数:19,代码来源:bptf.py

示例4: generate

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def generate(shp=(30, 30, 20, 10), K=5, alpha=0.1, beta=0.1):
    """Generate a count tensor from the BPTF model.

    PARAMS:
    shp -- (tuple) shape of the generated count tensor
    K -- (int) number of latent components
    alpha -- (float) shape parameter of gamma prior over factors
    beta -- (float) rate parameter of gamma prior over factors

    RETURNS:
    Mu -- (np.ndarray) true Poisson rates
    Y -- (np.ndarray) generated count tensor
    """
    Theta_DK_M = [rn.gamma(alpha, 1./beta, size=(D, K)) for D in shp]
    Mu = parafac(Theta_DK_M)
    assert Mu.shape == shp
    Y = rn.poisson(Mu)
    return Mu, Y 
开发者ID:aschein,项目名称:bptf,代码行数:20,代码来源:anomaly_detection.py

示例5: corrupt

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def corrupt(Y, p=0.05):
    """Corrupt a count tensor with anomalies.

    The corruption noise model is:

        corrupt(y) = y * g, where g ~ Gamma(10, 2)

    PARAMS:
    p -- (float) proportion of tensor entries to corrupt

    RETURNS:
    out -- (np.ndarray) corrupted count tensor
    mask -- (np.ndarray) boolean array, same shape as count tensor
                         True means that entry was corrupted.
    """
    out = Y.copy()
    mask = (rn.random(size=out.shape) < p).astype(bool)
    out[mask] = rn.poisson(out[mask] * rn.gamma(10., 2., size=out[mask].shape))
    return out, mask 
开发者ID:aschein,项目名称:bptf,代码行数:21,代码来源:anomaly_detection.py

示例6: _create_prices

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def _create_prices(t):
    last_average = 100 if t==0 else source.data['average'][-1]
    returns = asarray(lognormal(mean.value, stddev.value, 1))
    average =  last_average * cumprod(returns)
    high = average * exp(abs(gamma(1, 0.03, size=1)))
    low = average / exp(abs(gamma(1, 0.03, size=1)))
    delta = high - low
    open = low + delta * uniform(0.05, 0.95, size=1)
    close = low + delta * uniform(0.05, 0.95, size=1)
    return open[0], high[0], low[0], close[0], average[0] 
开发者ID:pzwang,项目名称:bokeh-dashboard-webinar,代码行数:12,代码来源:step1.py

示例7: _create_prices

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def _create_prices(t):
    global last_average
    returns = asarray(lognormal(mean, stddev, 1))
    average =  last_average * cumprod(returns)
    last_average = average

    high = average * exp(abs(gamma(1, 0.03, size=1)))
    low = average / exp(abs(gamma(1, 0.03, size=1)))
    delta = high - low
    open = low + delta * uniform(0.05, 0.95, size=1)
    close = low + delta * uniform(0.05, 0.95, size=1)
    return open[0], high[0], low[0], close[0], average[0] 
开发者ID:pzwang,项目名称:bokeh-dashboard-webinar,代码行数:14,代码来源:step0.py

示例8: __init__

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def __init__(self, *args, **kwargs):
        """
        :name: Name of the parameter
        :shape: The shape of the gamma distribution.
        :scale: The scale of the gamme distribution
        :step:     (optional) number for step size required for some algorithms,
                eg. mcmc need a parameter of the variance for the next step
                default is median of rndfunc(*rndargs, size=1000)
        :optguess: (optional) number for start point of parameter
                default is quantile(0.5) - quantile(0.4) of
                rndfunc(*rndargs, size=1000)
        """

        super(Gamma, self).__init__(rnd.gamma, 'Gamma', *args, **kwargs) 
开发者ID:thouska,项目名称:spotpy,代码行数:16,代码来源:parameter.py

示例9: rvs

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def rvs(self, size=None):
        return random.gamma(self.a, scale=self.scale, size=size) 
开发者ID:nchopin,项目名称:particles,代码行数:4,代码来源:distributions.py

示例10: logpdf

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def logpdf(self, x):
        return stats.gamma.logpdf(x, self.a, scale=self.scale) 
开发者ID:nchopin,项目名称:particles,代码行数:4,代码来源:distributions.py

示例11: ppf

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def ppf(self, u):
        return stats.gamma.ppf(u, self.a, scale=self.scale) 
开发者ID:nchopin,项目名称:particles,代码行数:4,代码来源:distributions.py

示例12: detect

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def detect(Y, K=5, alpha=0.1, thresh=1e-5):
    """Detect anomalies using BPTF.

    This method fits BPTF to Y and obtains Mu, which is the model's
    reconstruction of Y (computed from the inferred latent factors).
    Anomalies are then all entries of Y whose probability given Mu
    is less than a given threshold.

        If P(y | mu) < thresh ==> y is  anomaly!

        Here P(y | mu) = Pois(y; mu), the PMF of the Poisson distribution.

    PARAMS:
    Y -- (np.ndarray) data count tensor
    K -- (int) number of latent components
    alpha -- (float) shape parameter of gamma prior over factors
    thresh -- (float) anomaly threshold (between 0 and 1).
    """
    bptf = BPTF(n_modes=Y.ndim,
                n_components=K,
                max_iter=100,
                tol=1e-4,
                smoothness=100,
                verbose=False,
                alpha=alpha,
                debug=False)
    bptf.fit(Y)
    Mu = bptf.reconstruct()

    return st.poisson.pmf(Y, Mu) < thresh 
开发者ID:aschein,项目名称:bptf,代码行数:32,代码来源:anomaly_detection.py

示例13: beta_geometric_beta_binom_model

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import gamma [as 别名]
def beta_geometric_beta_binom_model(N, alpha, beta, gamma, delta, size=1):
    """
    Generate artificial data according to the Beta-Geometric/Beta-Binomial
    Model.

    You may wonder why we can have frequency = n_periods, when frequency excludes their
    first order. When a customer purchases something, they are born, _and in the next
    period_ we start asking questions about their alive-ness. So really they customer has
    bought frequency + 1, and been observed for n_periods + 1

    Parameters
    ----------
    N: array_like
        Number of transaction opportunities for new customers.
    alpha, beta, gamma, delta: float
        Parameters in the model. See [1]_
    size: int, optional
        The number of customers to generate

    Returns
    -------
    DataFrame
        with index as customer_ids and the following columns:
        'frequency', 'recency', 'n_periods', 'lambda', 'p', 'alive', 'customer_id'

    References
    ----------
    .. [1] Fader, Peter S., Bruce G.S. Hardie, and Jen Shang (2010),
       "Customer-Base Analysis in a Discrete-Time Noncontractual Setting,"
       Marketing Science, 29 (6), 1086-1108.

    """

    if type(N) in [float, int, np.int64]:
        N = N * np.ones(size)
    else:
        N = np.asarray(N)

    probability_of_post_purchase_death = random.beta(a=alpha, b=beta, size=size)
    thetas = random.beta(a=gamma, b=delta, size=size)

    columns = ["frequency", "recency", "n_periods", "p", "theta", "alive", "customer_id"]
    df = pd.DataFrame(np.zeros((size, len(columns))), columns=columns)
    for i in range(size):
        p = probability_of_post_purchase_death[i]
        theta = thetas[i]

        # hacky until I can find something better
        current_t = 0
        alive = True
        times = []
        while current_t < N[i] and alive:
            alive = random.binomial(1, theta) == 0
            if alive and random.binomial(1, p) == 1:
                times.append(current_t)
            current_t += 1
        # adding in final death opportunity to agree with [1]
        if alive:
            alive = random.binomial(1, theta) == 0
        df.iloc[i] = len(times), times[-1] + 1 if len(times) != 0 else 0, N[i], p, theta, alive, i
    return df 
开发者ID:CamDavidsonPilon,项目名称:lifetimes,代码行数:63,代码来源:generate_data.py


注:本文中的numpy.random.gamma方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。