Exemplo n.º 1
0
def interp_scatter(x,
                   y,
                   z,
                   ranges,
                   cmap='afmhot',
                   plot=True,
                   interp=True,
                   **kwargs):

    xlim, ylim, (vmin, vmax) = ranges

    if interp:
        logger.debug('Instantiating KernelReg')
        model = nparam.KernelReg(
            [z], [x, y],
            defaults=nparam.EstimatorSettings(efficient=True),
            reg_type='ll',
            var_type='cc',
            bw='cv_ls')
        X, Y = np.mgrid[slice(xlim[0], xlim[1], 101j),
                        slice(ylim[0], ylim[1], 101j)]
        positions = np.vstack([X.ravel(), Y.ravel()]).T

        logger.debug('Fitting kernel regression model...')
        sm_mean, sm_mfx = model.fit(positions)
        Z = np.reshape(sm_mean, X.shape)

    if plot:
        color_args = dict(cmap=cmap, vmin=vmin, vmax=vmax, alpha=1)
        if interp:
            logger.debug('Plotting fitted model on mesh grid...')
            im = plt.pcolormesh(X, Y, Z, shading='gouraud', **color_args)

        kwargs.update(color_args)

        logger.debug('Plotting scatter...')
        if 's' not in kwargs:
            kwargs['s'] = 20
        plt.scatter(x, y, c=z, lw=0.5, edgecolor='darkgray', **kwargs)
        plt.gca().set(xlim=xlim, ylim=ylim)
    else:
        return X, Y, Z
Exemplo n.º 2
0
    np.random.seed(500)
    nobs = [250, 1000][0]
    sig_fac = 1
    x = np.random.uniform(-2, 2, size=nobs)
    x.sort()
    y_true = np.sin(x * 5) / x + 2 * x
    y = y_true + sig_fac * (np.sqrt(
        np.abs(3 + x))) * np.random.normal(size=nobs)

    model = nparam.KernelReg(endog=[y],
                             exog=[x],
                             reg_type='lc',
                             var_type='c',
                             bw='cv_ls',
                             defaults=nparam.EstimatorSettings(efficient=True))

    sm_bw = model.bw

    sm_mean, sm_mfx = model.fit()

    model1 = nparam.KernelReg(endog=[y],
                              exog=[x],
                              reg_type='lc',
                              var_type='c',
                              bw='cv_ls')
    mean1, mfx1 = model1.fit()

    model2 = nparam.KernelReg(endog=[y],
                              exog=[x],
                              reg_type='ll',