我很抱歉这个可能很愚蠢的问题,但我现在花了几个小时试图从一组二维数据中估计密度。假设我的数据是由数组给出的:sample = np.random.uniform(0,1,size=(50,2))
。我只想使用scipys scikit学习包来估计样本数组的密度(这里当然是二维均匀密度),我正在尝试以下操作:
import numpy as np
from sklearn.neighbors.kde import KernelDensity
from matplotlib import pyplot as plt
sp = 0.01
samples = np.random.uniform(0,1,size=(50,2)) # random samples
x = y = np.linspace(0,1,100)
X,Y = np.meshgrid(x,y) # creating grid of data , to evaluate estimated density on
kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(samples) # creating density from samples
kde.score_samples(X,Y) # I want to evaluate the estimated density on the X,Y grid
但最后一步总是会产生错误:score_samples() takes 2 positional arguments but 3 were given
所以很可能。score_示例不能将网格作为输入,但是没有二维案例的教程/文档,所以我不知道如何解决这个问题。如果有人能帮忙,那就太好了。
看看Kernel Density Estimate of Species Distributions示例,您必须将x,y数据打包在一起(包括训练数据和新的示例网格)。
下面是一个简化sklearn API的函数。
这给你的xx,yy,zz需要像散点图或pcolormesh图。我已经从gaussian_kde函数的scipy页复制了这个示例。
相关问题 更多 >
编程相关推荐