Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 选择带宽&;用于核密度估计的linspace。(为什么我的带宽不工作?)_Python_Machine Learning_Scikit Learn_Cluster Analysis_Kernel Density - Fatal编程技术网

Python 选择带宽&;用于核密度估计的linspace。(为什么我的带宽不工作?)

Python 选择带宽&;用于核密度估计的linspace。(为什么我的带宽不工作?),python,machine-learning,scikit-learn,cluster-analysis,kernel-density,Python,Machine Learning,Scikit Learn,Cluster Analysis,Kernel Density,我一直在关注核密度估计的应用。我的目标是为阵列组创建两个或更多不同的组/群集。以下代码适用于除此阵列之外的阵列组的每个成员: X = np.array([[77788], [77793],[77798], [77803], [92886], [92891], [92896], [92901]]) 因此,我的期望是看到两个不同的集群,例如: 第一组=([77788]、[77793]、[77798]、[77803]) 第二组=([92886]、[92891]、[92896]、[92901]) 我有

我一直在关注核密度估计的应用。我的目标是为阵列组创建两个或更多不同的组/群集。以下代码适用于除此阵列之外的阵列组的每个成员:

X = np.array([[77788], [77793],[77798], [77803], [92886], [92891], [92896], [92901]])
因此,我的期望是看到两个不同的集群,例如:

第一组=([77788]、[77793]、[77798]、[77803])

第二组=([92886]、[92891]、[92896]、[92901])

我有一个动态列表,因此无法为linspace确定值。因为此阵列可能是0到10或100000到2000000。这就是为什么我把数组的最大点和最小点放在邻域空间中

毕竟,即使我尝试了不同的带宽,我也无法获得不同的集群。我的代码如下所示:

a = X.reshape(-1,1)
kde = KernelDensity(kernel='gaussian', bandwidth=8).fit(a)
s = linspace(min(a),max(a))
e = kde.score_samples(s.reshape(-1,1))
plot(s, e)

s[mi]和s[ma]值为空,这意味着此阵列没有两个不同的群集。在可视化中可以看到,我们至少有一个最小点。为什么无法看到s[mi]输出的此值

我为不同的带宽应用了相同的代码,如下所示,但是,这个集群没有最小值或最大值。你知道我做错了什么吗

bandwidth=0.008


尝试10000带宽,或者尝试使用启发式方法选择带宽


为了使您的代码更健壮,还可以按连续的最小值拆分集群。因为你的问题是这里没有一个唯一的最小值,而是一个间隔。

你可以考虑尝试网格搜索:

params = {'bandwidth': np.logspace(-1, 1, 20)}
grid   = GridSearchCV(KernelDensity(), params)
grid.fit(a)

print("best bandwidth: {0}".format(grid.best_estimator_.bandwidth))

kde = grid.best_estimator_

对不起,我不明白我该怎么做才能使我的代码更加健壮?你能给我举个例子吗@他已经辞职了——一只老鼠
bandwidth = 0.00002
params = {'bandwidth': np.logspace(-1, 1, 20)}
grid   = GridSearchCV(KernelDensity(), params)
grid.fit(a)

print("best bandwidth: {0}".format(grid.best_estimator_.bandwidth))

kde = grid.best_estimator_