예제 #1
0
def plot_violinplot(ax,data, log, group_labels=None):
    '''
    create violin plots on an axis
    '''
    
    if log:
        warning("log option ignored for violin plot")
    
    pos = range(len(data))
    dist = max(pos)-min(pos)
    w = min(0.15*max(dist,1.0),0.5)
    for data,p in zip(data,pos):
        if len(data)>0:
            kde = gaussian_kde(data) #calculates the kernel density
            x = np.linspace(np.min(data),np.max(data),250.) # support for violin
            v = kde.evaluate(x) #violin profile (density curve)
            
            scl = 1 / (v.max() / 0.4)
            v = v*scl #scaling the violin to the available space
            ax.fill_betweenx(x,p-v,p+v,facecolor=COLOR_LIST[p],alpha=0.6, lw=1.5)
            
            for percentile in [25, 75]:
                quant = scoreatpercentile(data.ravel(), percentile)
                q_x = kde.evaluate(quant) * scl 
                q_x = [p - q_x, p + q_x]
                ax.plot(q_x, [quant, quant], linestyle=":", c='k')
            med = np.median(data)
            m_x = kde.evaluate(med) * scl 
            m_x = [p - m_x, p + m_x]
            ax.plot(m_x, [med, med], linestyle="--", c='k', lw=1.5)            
        
    if group_labels:
        labels = group_labels[:]
        labels.insert(0, '')
        ax.set_xticklabels(labels, rotation='vertical')
def plot_violinplot(ax, value, log, group_labels=None):
    '''
    helper function for plotting violin plots on axes
    
    Parameters
    ----------
    ax : axes instance
    value : ndarray
    log : bool
    group_labels : list of str, optional

    '''

    if log:
        warning("log option ignored for violin plot")

    pos = range(len(value))
    dist = max(pos) - min(pos)
    _ = min(0.15 * max(dist, 1.0), 0.5)
    for data, p in zip(value, pos):
        if len(data) > 0:
            kde = gaussian_kde(data)  #calculates the kernel density
            x = np.linspace(np.min(data), np.max(data),
                            250.)  # support for violin
            v = kde.evaluate(x)  #violin profile (density curve)

            scl = 1 / (v.max() / 0.4)
            v = v * scl  #scaling the violin to the available space
            ax.fill_betweenx(x,
                             p - v,
                             p + v,
                             facecolor=COLOR_LIST[p],
                             alpha=0.6,
                             lw=1.5)

            for percentile in [25, 75]:
                quant = scoreatpercentile(data.ravel(), percentile)
                q_x = kde.evaluate(quant) * scl
                q_x = [p - q_x, p + q_x]
                ax.plot(q_x, [quant, quant], linestyle=":", c='k')
            med = np.median(data)
            m_x = kde.evaluate(med) * scl
            m_x = [p - m_x, p + m_x]
            ax.plot(m_x, [med, med], linestyle="--", c='k', lw=1.5)

    if group_labels:
        labels = group_labels[:]
        labels.insert(0, '')
        ax.set_xticklabels(labels, rotation='vertical')
예제 #3
0
def plot_violinplot(ax, value, log, group_labels=None):
    '''
    helper function for plotting violin plots on axes

    Parameters
    ----------
    ax : axes instance
    value : ndarray
    log : bool
    group_labels : list of str, optional

    '''

    if log:
        _logger.warning("log option ignored for violin plot")

    pos = range(len(value))
    dist = max(pos) - min(pos)
    _ = min(0.15 * max(dist, 1.0), 0.5)
    for data, p in zip(value, pos):
        if len(data) > 0:
            kde = gaussian_kde(data)  # calculates the kernel density
            x = np.linspace(np.min(data), np.max(data),
                            250.)  # support for violin
            v = kde.evaluate(x)  # violin profile (density curve)

            scl = 1 / (v.max() / 0.4)
            v = v * scl  # scaling the violin to the available space
            ax.fill_betweenx(
                x, p - v, p + v, facecolor=get_color(p), alpha=0.6, lw=1.5)

            for percentile in [25, 75]:
                quant = scoreatpercentile(data.ravel(), percentile)
                q_x = kde.evaluate(quant) * scl
                q_x = [p - q_x, p + q_x]
                ax.plot(q_x, [quant, quant], linestyle=":", c='k')
            med = np.median(data)
            m_x = kde.evaluate(med) * scl
            m_x = [p - m_x, p + m_x]
            ax.plot(m_x, [med, med], linestyle="--", c='k', lw=1.5)

    if group_labels:
        labels = group_labels[:]
        labels.insert(0, '')
        ax.set_xticklabels(labels, rotation='vertical')
def kernel_weighted_samples(x, color, x_grid, **kwargs):
	from statsmodels.nonparametric.kde import KDEUnivariate
	import matplotlib.pyplot as plt
	"""Univariate Kernel Density Estimation with Statsmodels"""
	kde = KDEUnivariate(x)
	kde.fit(**kwargs) # kernel, bw, fft, weights, gridsize, ...]
	pdf = kde.evaluate(x_grid)

	plt.plot(x_grid, pdf, color=color, alpha=0.5, lw=3)
	plt.fill_between(x_grid, pdf, where=None, color=color, alpha = 0.2)

	return pdf, x_grid
예제 #5
0
def kernel_weighted_samples(x, color, x_grid, **kwargs):
    from statsmodels.nonparametric.kde import KDEUnivariate
    import matplotlib.pyplot as plt
    """Univariate Kernel Density Estimation with Statsmodels"""
    kde = KDEUnivariate(x)
    kde.fit(**kwargs)  # kernel, bw, fft, weights, gridsize, ...]
    pdf = kde.evaluate(x_grid)

    plt.plot(x_grid, pdf, color=color, alpha=0.5, lw=3)
    plt.fill_between(x_grid, pdf, where=None, color=color, alpha=0.2)

    return pdf, x_grid
예제 #6
0
def bayes_classifier(x_vec, kdes):
    """
    Classifies an input sample into class w_j determined by
    maximizing the class conditional probability for p(x|w_j).

    Keyword arguments:
        x_vec: A dx1 dimensional numpy array representing the sample.
        kdes: List of the gausssian_kde (kernel density) estimates

    Returns a tuple ( p(x|w_j)_value, class label ).

    """
    p_vals = []
    for kde in kdes:
        p_vals.append(kde.evaluate(x_vec))
    max_index, max_value = max(enumerate(p_vals), key=operator.itemgetter(1))
    return max_value, max_index + 1