核密度函數

# coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KernelDensity

np.random.seed(1)
N = 20
X = np.concatenate((np.random.normal(0, 1,int( 0.3 * N)),
                    np.random.normal(5, 1,int( 0.7 * N))))[:, np.newaxis]
X_plot = np.linspace(-5, 10, 1000)[:, np.newaxis]
bins = np.linspace(-5, 10, 10)
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
fig.subplots_adjust(hspace=0.05, wspace=0.05)
# 直方圖 1 'Histogram'
ax[0, 0].hist(X[:, 0], bins=bins, fc='#AAAAFF', normed=True)
ax[0, 0].text(-3.5, 0.31, 'Histogram')
# 直方圖 2 'Histogram, bins shifted'
ax[0, 1].hist(X[:, 0], bins=bins + 0.75, fc='#AAAAFF', normed=True)
ax[0, 1].text(-3.5, 0.31, 'Histogram, bins shifted')
# 核密度估計 1 'tophat KDE'
kde = KernelDensity(kernel='tophat', bandwidth=0.75).fit(X)
log_dens = kde.score_samples(X_plot)
ax[1, 0].fill(X_plot[:, 0], np.exp(log_dens), fc='#AAAAFF')
ax[1, 0].text(-3.5, 0.31, 'Tophat Kernel Density')
# 核密度估計 2 'Gaussian KDE'
kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(X)
log_dens = kde.score_samples(X_plot)
ax[1, 1].fill(X_plot[:, 0], np.exp(log_dens), fc='#AAAAFF')
ax[1, 1].text(-3.5, 0.31, 'Gaussian Kernel Density')
for axi in ax.ravel():
    axi.plot(X[:, 0], np.zeros(X.shape[0]) - 0.01, '+k')
    axi.set_xlim(-4, 9)
    axi.set_ylim(-0.02, 0.34)
for axi in ax[:, 0]:
    axi.set_ylabel('Normalized Density')
for axi in ax[1, :]:
    axi.set_xlabel('x')
plt.show()

def plot_bootstrap_sample(plot_data: dict, report_dir, criterion_list):
    # 對於每個特徵提取器進行遍歷

    linestyles = ['-', '--', '-.', ':']
    markers = ['+', ',', '.']
    colors = ['red', 'green', 'blue', 'orange']

    def get(lst, i):
        return lst[i % len(lst)]

    plt.rcParams['figure.figsize'] = (18.0, 15.0)

    sns.set()
    row = 2
    col = 2
    ax_i = 1
    # 處理分層抽樣
    for feat_name in plot_data:
        feat_data = plot_data.get(feat_name)
        bootstrap_data = feat_data['bootstrap']  # len(criterion_list) 行, 每行(抽樣次數)列
        plt.subplot(row, col, ax_i)
        for i, (criterion, data) in enumerate(zip(criterion_list, bootstrap_data)):
            lx=np.percentile(data,2.5)
            ux=np.percentile(data,97.5)
            x_name = 'evaluation'
            y_name = 'dense'
            sns.kdeplot(data, shade=True,label=criterion,color=get(colors,i))
            plt.xlabel(x_name)
            plt.ylabel(y_name)
        plt.legend()
        plt.title(feat_name)

        ax_i += 1
    plt.suptitle('bootstrap sample')
    plt.savefig((Path(report_dir) / f'bootstrap_sample.png').as_posix())
    plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章