Skip to main content

A python implementation of KernSmooth package ( smoothing and density estimation functions based on the book: Wand, M.P. and Jones, M.C. (1995) "Kernel Smoothing".

Project description

Porting popular R library KernSmooth to python.

Functions for Kernel Smoothing and Density Estimation.

Transformed R and Fortran functions into Python(2,3) code.


Please use kern-smooth 1.1.0 or newer. Reason: found not needed log10 density transformation.


pip install kern-smooth

Main function of the module:

def densCols(x, y=None, nbin=128, bandwidth=None)

Produces a vector of numbers which encode the local densities at each point in dataset.

x, y : 1D numpy array with coordinates of the points density will be estimated on

nbin : [optional] int or [int, int] - number of bins along each axis
(in case of single value - [nbin, nbin] will be used). Default value 128.

bandwidth : [optional] numeric vector (len of 1 or 2) of smoothing bandwidth.

Returns: numpy array with numerical representation (in range [0,1]) of point densities.

Attention: For return value numpy.nan values are allowed in case of nan / infinite values in original dataset

Source: R::grDevices::densCols


Generate data for plotting

from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np
# create two 'bulbs' with normal distributions
mean1 = [0, 0]
cov1 = [[5, 0], [0, 30]]  # diagonal covariance
x1, y1 = np.random.multivariate_normal(mean1, cov1, 50000).T
mean2 = [5, 17]
cov2 = [[30, 0], [0, 5]]  # diagonal covariance
x2, y2 = np.random.multivariate_normal(mean2, cov2, 50000).T
x = np.hstack([x1,x2])
y = np.hstack([y1,y2])

Generate point densities:

from kern_smooth import densCols
densities = densCols(x, y, nbin = 128)

Plot the result

sc = plt.scatter(x, y, c=densities, s=15, edgecolors='none', alpha=0.75, cmap=cm.jet)


Alexander Butyaev

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for kern-smooth, version 1.1.1
Filename, size File type Python version Upload date Hashes
Filename, size kern_smooth-1.1.1-py3-none-any.whl (6.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size kern-smooth-1.1.1.tar.gz (293.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page