[英]I cannot figure out Imshow
我最近这样做了:
from sklearn.neighbors import KernelDensity
def kde2D(x, y, bandwidth, bins, **kwargs):
"""Build 2D kernel density estimate (KDE). Adapted from: https://stackoverflow.com/questions/41577705/how-does-2d-kernel-density-estimation-in-python-sklearn-work"""
# A grid representing the sampled space. Large bins make it faster
xx, yy = np.mgrid[df.x.min():df.x.max():bins,
df.y.min():df.y.max():bins]
xy_sample = np.vstack([yy.ravel(), xx.ravel()]).T
xy_train = np.vstack([y, x]).T
# Apply kernel density. Large bandwidth gives more regional effects and is slower
kde_skl = KernelDensity(bandwidth=bandwidth, **kwargs)
kde_skl.fit(xy_train)
# score_samples() returns the kernel density at the grid points linearly
z = np.exp(kde_skl.score_samples(xy_sample))
# Reshape it to x,y coordinates corresponding to the original grid
return np.reshape(z, xx.shape)
# Apply function and plot it
dens = kde2D(df.x, df.y, bandwidth = 80, bins = 100)
plt.imshow(dens)
干杯,里卡多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.