python核密度估计(KernelDensity)

在获得数据之后,我们需要对数据进行分析,以便了解数据的基本性质,为后续的模型选择和模型训练提供依据。了解特征的分布,是机器学习的第一步,同时也是相当关键的一步。我们引入了核密度估计来帮助我们了解数据的基本分布。

在上一篇文章中,我们重点描述了数据的导入和转换。在利用pandas的read_csv函数导入数据的过程中,根据自定义函数将特征转换为我们需要的形式。下面依然后贴上该代码。如下:

 

#--coding:utf-8
import warnings
warnings.filterwarnings("ignore")

import numpy as np
import pandas as pd
import os
from sklearn.neighbors import KernelDensity
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d

filepath = os.path.join("../dataset/Internet Advertisements/Data Folder","ad.data")

#对DataFrame的列做数据转换
def Converter_number(x):
    try:
        return np.float64(x)
    except ValueError:
        return np.nan

#字典推导式
converters = {key:Converter_number for key in range(1558)}
converters[1558] = lambda x: 1 if x.strip()=='ad.' else 0
ads = pd.read_csv(filepath,header=None,converters=converters)
print(ads[:5])
#     0      1       2     3     4     5     6     7     8     9     ...   1549  \
# 0  125.0  125.0  1.0000   1.0   0.0   0.0   0.0   0.0   0.0   0.0  ...    0.0   
# 1   57.0  468.0  8.2105   1.0   0.0   0.0   0.0   0.0   0.0   0.0  ...    0.0   
# 2   33.0  230.0  6.9696   1.0   0.0   0.0   0.0   0.0   0.0   0.0  ...    0.0   
# 3   60.0  468.0  7.8000   1.0   0.0   0.0   0.0   0.0   0.0   0.0  ...    0.0   
# 4   60.0  468.0  7.8000   1.0   0.0   0.0   0.0   0.0   0.0   0.0  ...    0.0   
# 
#    1550  1551  1552  1553  1554  1555  1556  1557  1558  
# 0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0     1  
# 1   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0     1  
# 2   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0     1  
# 3   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0     1  
# 4   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0     1  
# 
# [5 rows x 1559 columns]

 

 

 

 

#首先去掉空值,并查看数据的统计

 

 

ads = ads.dropna(axis=0)
print(ads.describe())
 
#我们可以看到,大量的特征的25%,50%,75%分位数显示,特征分布在0或1,我们可以大胆猜测,这些特征大量包含0或者1.
#              0            1            2            3            4     \
# count  2359.000000  2359.000000  2359.000000  2359.000000  2359.000000   
# mean     63.912251   155.631624     3.912982     0.759644     0.002120   
# std      54.881130   130.237867     6.047220     0.427390     0.045999   
# min       1.000000     1.000000     0.001500     0.000000     0.000000   
# 25%      25.000000    80.500000     1.033450     1.000000     0.000000   
# 50%      51.000000   110.000000     2.111100     1.000000     0.000000   
# 75%      84.000000   184.000000     5.333300     1.000000     0.000000   
# max     640.000000   640.000000    60.000000     1.000000     1.000000   
# 
#          5            6            7            8            9     \
# count  2359.0  2359.000000  2359.000000  2359.000000  2359.000000   
# mean      0.0     0.006359     0.004663     0.004663     0.014837   
# std       0.0     0.079504     0.068141     0.068141     0.120925   
# min       0.0     0.000000     0.000000     0.000000     0.000000   
# 25%       0.0     0.000000     0.000000     0.000000     0.000000   
# 50%       0.0     0.000000     0.000000     0.000000     0.000000   
# 75%       0.0     0.000000     0.000000     0.000000     0.000000   
# max       0.0     1.000000     1.000000     1.000000     1.000000   
# 
#           ...              1549         1550         1551         1552  \
# count     ...       2359.000000  2359.000000  2359.000000  2359.000000   
# mean      ...          0.003815     0.001272     0.002120     0.002543   
# std       ...          0.061662     0.035646     0.045999     0.050379   
# min       ...          0.000000     0.000000     0.000000     0.000000   
# 25%       ...          0.000000     0.000000     0.000000     0.000000   
# 50%       ...          0.000000     0.000000     0.000000     0.000000   
# 75%       ...          0.000000     0.000000     0.000000     0.000000   
# max       ...          1.000000     1.000000     1.000000     1.000000   
# 
#               1553         1554         1555        1556         1557  \
# count  2359.000000  2359.000000  2359.000000  2359.00000  2359.000000   
# mean      0.008478     0.013989     0.014837     0.00975     0.000848   
# std       0.091705     0.117470     0.120925     0.09828     0.029111   
# min       0.000000     0.000000     0.000000     0.00000     0.000000   
# 25%       0.000000     0.000000     0.000000     0.00000     0.000000   
# 50%       0.000000     0.000000     0.000000     0.00000     0.000000   
# 75%       0.000000     0.000000     0.000000     0.00000     0.000000   
# max       1.000000     1.000000     1.000000     1.00000     1.000000   
# 
#               1558  
# count  2359.000000  
# mean      0.161509  
# std       0.368078  
# min       0.000000  
# 25%       0.000000  
# 50%       0.000000  
# 75%       0.000000  
# max       1.000000  
# 
# [8 rows x 1559 columns]
from sklearn.grid_search import GridSearchCV

#我们来看看前4个特征的分布
fig = plt.figure()
df = (ads[0].sort_values().values)[:,np.newaxis]
grid_param = {
    'bandwidth':list(range(1,31))
}
kde_grid = GridSearchCV(KernelDensity(),grid_param)
kde = kde_grid.fit(df).best_estimator_
print(kde_grid.best_params_)
plt.subplot(221)
plt.plot(df[:,0],np.exp(kde.score_samples(df)),'-')

df = (ads[1].sort_values().values)[:,np.newaxis]
grid_param = {
    'bandwidth':list(range(1,31))
}
kde_grid = GridSearchCV(KernelDensity(),grid_param)
kde = kde_grid.fit(df).best_estimator_
print(kde_grid.best_params_)
plt.subplot(222)
plt.plot(df[:,0],np.exp(kde.score_samples(df)),'-')

df = (ads[2].sort_values().values)[:,np.newaxis]
grid_param = {
    'bandwidth':list(range(1,11))
}
kde_grid = GridSearchCV(KernelDensity(),grid_param)
kde = kde_grid.fit(df).best_estimator_
print(kde_grid.best_params_)
plt.subplot(223)
plt.plot(df[:,0],np.exp(kde.score_samples(df)),'-')

df = (ads[3].sort_values().values)[:,np.newaxis]
grid_param = {
    'bandwidth':np.linspace(0.01,0.1,10)
}
kde_grid = GridSearchCV(KernelDensity(),grid_param)
kde = kde_grid.fit(df).best_estimator_
print(kde_grid.best_params_)
plt.subplot(224)
plt.plot(df[:,0],np.exp(kde.score_samples(df)),'-')

#可用来对类别变量进行计数
from collections import Counter
df1558 = ads[1558].values
# c = Counter(df1558)
# plt.bar(list(c.keys()),list(c.values()))
fig = plt.figure()
plt.hist(df1558,bins=2,rwidth=0.8)

plt.show()

 

 

 

if __name__ == "__main__":
    print("OK")

 

图1

图2

这样,我们就得到了特征的分布情况。

上述代码中,我们主要用到了KernelDensity函数,是一个核密度估计函数。kernel density estimation是在概率论中用来估计未知的密度函数,属于非参数检验方法之一,由Rosenblatt (1955)和Emanuel Parzen(1962)提出,又名Parzen窗(Parzen window)。Ruppert和Cline基于数据集密度函数聚类算法提出修订的核密度估计方法。

由给定样本点集合求解随机变量的分布密度函数问题是概率统计学的基本问题之一。解决这一问题的方法包括参数估计和非参数估计。参数估计又可分为参数回归分析和参数判别分析。在参数回归分析中,人们假定数据分布符合某种特定的性态,如线性、可化线性或指数性态等,然后在目标函数族中寻找特定的解,即确定回归模型中的未知参数。在参数判别分析中,人们需要假定作为判别依据的、随机取值的数据样本在各个可能的类别中都服从特定的分布。经验和理论说明,参数模型的这种基本假定与实际的物理模型之间常常存在较大的差距,这些方法并非总能取得令人满意的结果。由于上述缺陷,Rosenblatt和Parzen提出了非参数估计方法,即核密度估计方法.由于核密度估计方法不利用有关数据分布的先验知识,对数据分布不附加任何假定,是一种从数据样本本身出发研究数据分布特征的方法,因而,在统计学理论和应用领域均受到高度的重视。

 

一些比较常用的核函数

均匀核函数 k(x)=1/2,-1≤x≤1 加入带宽h后: kh(x)=1/(2h),-h≤x≤h
三角 核函数 k(x)=1-|x|,-1≤x≤1 加入带宽h后: kh(x)=(h-|x|)/h^2,-h≤x≤h
伽马核函数 kxi(x)=[x^(α-1)exp{-xα/xi}]/[(xi/α)^α.Γ(α)]
高斯核函数K(x,xc)=exp(-||x-xc||^2/(2*σ)^2)其中xc为核函数中心,σ为函数的宽度参数。
 
参数和方法如下:
Parameters:

bandwidth : float

The bandwidth of the kernel.

algorithm : string

The tree algorithm to use. Valid options are [‘kd_tree’|’ball_tree’|’auto’]. Default is ‘auto’.

kernel : string

The kernel to use. Valid kernels are [‘gaussian’|’tophat’|’epanechnikov’|’exponential’|’linear’|’cosine’] Default is ‘gaussian’.

metric : string

The distance metric to use. Note that not all metrics are valid with all algorithms. Refer to the documentation of BallTree and KDTree for a description of available algorithms. Note that the normalization of the density output is correct only for the Euclidean distance metric. Default is ‘euclidean’.

atol : float

The desired absolute tolerance of the result. A larger tolerance will generally lead to faster execution. Default is 0.

rtol : float

The desired relative tolerance of the result. A larger tolerance will generally lead to faster execution. Default is 1E-8.

breadth_first : boolean

If true (default), use a breadth-first approach to the problem. Otherwise use a depth-first approach.

leaf_size : int

Specify the leaf size of the underlying tree. See BallTree or KDTree for details. Default is 40.

metric_params : dict

Additional parameters to be passed to the tree for use with the metric. For more information, see the documentation of BallTree or KDTree.

 

Methods

fit(X[, y])Fit the Kernel Density model on the data.
get_params([deep])Get parameters for this estimator.
sample([n_samples, random_state])Generate random samples from the model.
score(X[, y])Compute the total log probability under the model.
score_samples(X)Evaluate the density model on the data.
set_params(\*\*params)Set the parameters of this estimator.

对于单变量分布,后面会介绍更简单的分析方式。但是核密度估计是一种重要的非参数检验方法,感兴趣的同学可以深入了解。

 

  • 15
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值