MCPcopy Index your code
hub / github.com/python/cpython / kde

Function kde

Lib/statistics.py:935–1091  ·  view source on GitHub ↗

Kernel Density Estimation: Create a continuous probability density function or cumulative distribution function from discrete samples. The basic idea is to smooth the data using a kernel function to help draw inferences about a population from a sample. The degree of smoothing is

(data, h, kernel='normal', *, cumulative=False)

Source from the content-addressed store, hash-verified

933
934
935def kde(data, h, kernel='normal', *, cumulative=False):
936 """Kernel Density Estimation: Create a continuous probability density
937 function or cumulative distribution function from discrete samples.
938
939 The basic idea is to smooth the data using a kernel function
940 to help draw inferences about a population from a sample.
941
942 The degree of smoothing is controlled by the scaling parameter h
943 which is called the bandwidth. Smaller values emphasize local
944 features while larger values give smoother results.
945
946 The kernel determines the relative weights of the sample data
947 points. Generally, the choice of kernel shape does not matter
948 as much as the more influential bandwidth smoothing parameter.
949
950 Kernels that give some weight to every sample point:
951
952 normal (gauss)
953 logistic
954 sigmoid
955
956 Kernels that only give weight to sample points within
957 the bandwidth:
958
959 rectangular (uniform)
960 triangular
961 parabolic (epanechnikov)
962 quartic (biweight)
963 triweight
964 cosine
965
966 If *cumulative* is true, will return a cumulative distribution function.
967
968 A StatisticsError will be raised if the data sequence is empty.
969
970 Example
971 -------
972
973 Given a sample of six data points, construct a continuous
974 function that estimates the underlying probability density:
975
976 >>> sample = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2]
977 >>> f_hat = kde(sample, h=1.5)
978
979 Compute the area under the curve:
980
981 >>> area = sum(f_hat(x) for x in range(-20, 20))
982 >>> round(area, 4)
983 1.0
984
985 Plot the estimated probability density function at
986 evenly spaced points from -6 to 10:
987
988 >>> for x in range(-6, 11):
989 ... density = f_hat(x)
990 ... plot = ' ' * int(density * 400) + 'x'
991 ... print(f'{x:2}: {density:.3f} {plot}')
992 ...

Callers 1

test_kdeMethod · 0.85

Calls 2

StatisticsErrorClass · 0.85
getMethod · 0.45

Tested by 1

test_kdeMethod · 0.68