Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带宽内核密度_Python_Scikit Learn - Fatal编程技术网

Python 带宽内核密度

Python 带宽内核密度,python,scikit-learn,Python,Scikit Learn,我试图计算一系列值的核密度函数: x=[-0.04124324405924407, 0, 0.005249724476788287, 0.03599351958245578, -0.00252785423151014, 0.01007584102031178, -0.002510349639322063, -0.01264302961474806, -0.01797169063489579] 下面这个网站:我想计算带宽的最佳值,所以我写了这段代码: from sklearn.grid_sear

我试图计算一系列值的核密度函数:

x=[-0.04124324405924407, 0, 0.005249724476788287, 0.03599351958245578, -0.00252785423151014, 0.01007584102031178, -0.002510349639322063, -0.01264302961474806, -0.01797169063489579]
下面这个网站:我想计算带宽的最佳值,所以我写了这段代码:

from sklearn.grid_search import GridSearchCV
grid = GridSearchCV(KernelDensity(),{'bandwidth': np.linspace(-1.0, 1.0, 30)},cv=20) # 20-fold cross-validation
grid.fit(x[:, None])
grid.best_params_
但当我运行这个:

grid.fit(x[:, None])
我得到这个错误:

Error: TypeError: list indices must be integers, not tuple
有人知道怎么修吗?谢谢

您使用的是python,您应该使用。后者支持富人


考虑到小样本量,我会使用的是的
KernelSmoothing
类。默认情况下,它提供Scott的多维规则。如果需要,我们可以使用Sheapler和Jones的直接插件算法,它在许多情况下提供了良好的带宽,即使分布是多模式的

以下脚本使用默认带宽

x = [
    -0.04124324405924407,
    0,
    0.005249724476788287,
    0.03599351958245578,
    -0.00252785423151014,
    0.01007584102031178,
    -0.002510349639322063,
    -0.01264302961474806,
    -0.01797169063489579,
]
import openturns as ot
sample = ot.Sample(x, 1)
factory = ot.KernelSmoothing()
distribution = factory.build(sample)
bandwidth = factory.computePluginBandwidth(sample)
distribution = factory.build(sample, bandwidth)
distribution.drawPDF()
就这样

如果要使用更智能的带宽选择,我们可以使用
computePluginBandwidth
方法,该方法基于Sheapler和Jones的直接“求解方程”规则。在下面的脚本中,我在评估带宽后绘制了分布图

x = [
    -0.04124324405924407,
    0,
    0.005249724476788287,
    0.03599351958245578,
    -0.00252785423151014,
    0.01007584102031178,
    -0.002510349639322063,
    -0.01264302961474806,
    -0.01797169063489579,
]
import openturns as ot
sample = ot.Sample(x, 1)
factory = ot.KernelSmoothing()
distribution = factory.build(sample)
bandwidth = factory.computePluginBandwidth(sample)
distribution = factory.build(sample, bandwidth)
distribution.drawPDF()
带宽评估为0.00941247。PDF格式如下


谢谢@llja。我可以这样做:x=np.array(x)?