A python implementation of KernSmooth package (https://cran.r-project.org/web/packages/KernSmooth):kernel 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.
Attention:
Please use kern-smooth 1.1.0 or newer. Reason: found not needed log10 density transformation.
Installation:
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
Usage
Generate data for plotting
from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np
np.random.seed(0)
# 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)
plt.colorbar(sc)
plt.show()
Result
![Result](https://github.com/AlexanderButyaev/kern_smooth/blob/master/example_density.png)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file kern-smooth-1.1.1.tar.gz
.
File metadata
- Download URL: kern-smooth-1.1.1.tar.gz
- Upload date:
- Size: 293.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac268abb2aac2d5899f4226abdf98b7b3d4791f1ef332eb0a906f0d26ead60c9 |
|
MD5 | 045c4f80df640919629c22ba7d157c13 |
|
BLAKE2b-256 | 315753bbe4f4e03ed8ea39fe865e77f273b5c95fbd9fba3f5e2c359d93520cd4 |
File details
Details for the file kern_smooth-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: kern_smooth-1.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a373b5f0305e9c876a506028b65f9031eb6dd66fac494a8985963f2f0f493ad7 |
|
MD5 | 9dc9c67499d6335babda7b71934469e5 |
|
BLAKE2b-256 | 7488db76260b507c24a5e2b23b567b01bdf562cb55d9337ba2545de966a37e69 |