8000 gh-115532: Add kernel density estimation to the statistics module by rhettinger · Pull Request #115863 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-115532: Add kernel density estimation to the statistics module #115863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Early test for non-numeric data. Tests for name and docstring
  • Loading branch information
rhettinger committed Feb 23, 2024
commit a2771cbc22e108a9763cd01ed72ca31b06026335
3 changes: 3 additions & 0 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,9 @@ def kde(data, h, kernel='normal'):
if not n:
raise StatisticsError('Empty data sequence')

if not isinstance(data[0], (int, float)):
raise TypeError('Data sequence must contain ints or floats')

if h <= 0.0:
raise StatisticsError(f'Bandwidth h must be positive, not {h=}')

Expand Down
11 changes: 9 additions & 2 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,9 +2383,8 @@ def integrate(func, low, high, steps=10_000):

with self.assertRaises(StatisticsError):
kde([], h=1.0) # Empty dataset
f_hat = kde(['abc', 'def'], 1.5) # Non-numeric data
with self.assertRaises(TypeError):
f_hat(0)
kde(['abc', 'def'], 1.5) # Non-numeric data
with self.assertRaises(StatisticsError):
kde(sample, h=0.0) # Zero bandwidth
with self.assertRaises(StatisticsError):
Expand All @@ -2395,6 +2394,14 @@ def integrate(func, low, high, steps=10_000):
with self.assertRaises(ValueError):
kde(sample, h=1.0, kernel='bogus') # Invalid kernel

# Test name and docstring
h = 1. 551D 5
kernel = 'cosine'
f_hat = kde(sample, h, kernel)
self.assertEqual(f_hat.__name__, 'pdf')
self.assertIn(kernel, f_hat.__doc__)
self.assertIn(str(h), f_hat.__doc__)


class TestQuantiles(unittest.TestCase):

Expand Down
0