question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

saving a KernelDensity estimator

See original GitHub issue

Hi!

I noticed that after saving a sklearn.neighbors.KernelDensity() using joblib.dump, restauring it using joblib.load does not result in a usable estimator. I notice that before reload, the estimator’s .tree_ is a sklearn.neighbors.kd_tree.KDTree whereas after it is a sklearn.neighbors.kd_tree.BinaryTree… is this normal behaviour ? Or am I doing something wrong ?

Many thanks,

A

Example code:

import numpy as np
from sklearn.neighbors import KernelDensity
kde = KernelDensity()
data = np.reshape([1., 2., 3.], (-1, 1))
kde.fit(data)

here kde estimator can be used: print(kde.score_samples(np.reshape([1.1, 2.1], (-1, 1)))) array([-1.4151691 , -1.22543484]) print(kde.tree_) <sklearn.neighbors.kd_tree.KDTree object at 0x564e7375d158>

whereas after reloading:

from sklearn.externals import joblib
joblib.dump(kde, './test_kde.pkl')
kde = joblib.load('./test_kde.pkl')
kde.score_samples(np.reshape([1.1, 2.1], (-1, 1)))

/home/asors/miniconda3/envs/mrpt/lib/python3.6/site-packages/sklearn/neighbors/kde.py:176: RuntimeWarning: divide by zero encountered in log log_density -= np.log(N) /home/asors/miniconda3/envs/mrpt/lib/python3.6/site-packages/sklearn/neighbors/kde.py:176: RuntimeWarning: invalid value encountered in subtract log_density -= np.log(N) array([nan, nan])

print(kde.tree_) <sklearn.neighbors.kd_tree.BinaryTree object at 0x564e73763258>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
NicolasHugcommented, Aug 7, 2018

I’ll try to investigate what’s going on

0reactions
NicolasHugcommented, Apr 22, 2019

Please open another issue with the whole minimal reproducible example

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Which parameters must be saved by a Kernel Density ...
I trained a kde with sklearn and now I want to save the parameters. Which parameters are needed to fully describe a kde?...
Read more >
Kernel Density Estimation Save Options
This dialog specifies save structures for the Kernel Density Estimation menu. Select the boxes then type the names for the data structures ...
Read more >
Kernel Density Estimation in Python Using Scikit-Learn
This article is an introduction to kernel density estimation using Python's machine learning library scikit-learn .
Read more >
Kernel Density Estimation with Python using Sklearn - Medium
Kernel Density Estimation often referred to as KDE is a technique that lets you create a smooth curve given a set of data....
Read more >
Kernel density estimation (Excel) - YouTube
However, what about an empirical or data-driven probability density functions? Here, kernel density estimation (KDE) comes to the rescue.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found