TZJ
TZJ

Reputation: 159

Python Scipy Kernel Density Estimate Smoothing Issues

Sorry to ask a question with probably a very obvious answer but I'm a bit confused as to how to tweak how much I can smooth with the KDE. My code looks something like this in python:

kde = scipy.stats.gaussian_kde(c)
P_0 = kde(3)
P_c = kde(c)

where c is just a column of numbers and I want to do an integral with the above (that's not too important for the problem I'm having). I'm a bit confused as to how I would change the scott/silverman method in scipy to allow a bit of over/undersmoothing.

Upvotes: 1

Views: 1199

Answers (1)

Gabriel
Gabriel

Reputation: 42439

You appear to want to tweak the set_bandwidth parameter. That link contains simple example code, which I reduce here to the most basic elements:

kde = stats.gaussian_kde(c)
kde.set_bandwidth(bw_method=.3)
P = kde(c)

So basically, the bandwidth is set via the kde.set_bandwidth(bw_method=X) call, where X is usually a float or one of the methods silverman, scott. The full description actually states that bw_method:

can be ‘scott’, ‘silverman’, a scalar constant or a callable. If a scalar, this will be used directly as kde.factor. If a callable, it should take a gaussian_kde instance as only parameter and return a scalar.

Upvotes: 2

Related Questions