In [7]:
from scipy import stats
import numpy as np
def measure(n):
    """Measurement model, return two coupled measurements.
    """
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2

In [8]:
m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()

In [9]:
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)

KDE without dataset saved in object

  • Less storage & memory needed

In [159]:
from scipy import stats
import numpy as np

# making kde
values = np.arange(10)
kde = stats.gaussian_kde(values)

In [160]:
np.median(kde.resample(100000))


Out[160]:
4.5073174299614047

In [161]:
def KDE_make_means(kde, size=10):
    func = lambda x : np.random.randint(0, x.n, size=x.d)
    kde.means = [kde.dataset[:, func(kde)] for i in xrange(size)]
    kde.means_n = len(kde.means)
    #kde.dataset = None   

#KDE_make_means(kde)

In [162]:
def KDE_save_means(kde, size):
    indices = np.random.randint(0, kde.n, size=size)
    kde.means = kde.dataset[:, indices]
    kde.means_size = size
    
KDE_save_means(kde, size=10)
kde.dataset = None

In [166]:
def KDE_resample(kde):
    norm = np.transpose(np.random.multivariate_normal(np.zeros((kde.d,), float), kde.covariance, 
                                                      size=kde.means_size))
    print(kde.means)
    return kde.means + norm

KDE_resample(kde)


[[3 6 1 8 4 2 1 7 7 3]]
Out[166]:
array([[  2.27665471,   6.45279986,   0.76167366,  11.36411559,
          5.66182709,  -0.20468706,   1.86112105,   8.3382336 ,
          6.72463729,   2.70542962]])

Notes:

  • cannot get rid of dataset attribute in order to free up memory


In [1]:
%load_ext rpy2.ipython

In [4]:
%%R
library(dplyr)
library(tidyr)
library(ggplot2)
library(vegan)

In [6]:
%%R
data(varespec)
varespec %>% head


   Callvulg Empenigr Rhodtome Vaccmyrt Vaccviti Pinusylv Descflex Betupube
18     0.55    11.13     0.00     0.00    17.80     0.07     0.00        0
15     0.67     0.17     0.00     0.35    12.13     0.12     0.00        0
24     0.10     1.55     0.00     0.00    13.47     0.25     0.00        0
27     0.00    15.13     2.42     5.92    15.97     0.00     3.70        0
23     0.00    12.68     0.00     0.00    23.73     0.03     0.00        0
19     0.00     8.92     0.00     2.42    10.28     0.12     0.02        0
   Vacculig Diphcomp Dicrsp Dicrfusc Dicrpoly Hylosple Pleuschr Polypili
18     1.60     2.07   0.00     1.62     0.00      0.0     4.67     0.02
15     0.00     0.00   0.33    10.92     0.02      0.0    37.75     0.02
24     0.00     0.00  23.43     0.00     1.68      0.0    32.92     0.00
27     1.12     0.00   0.00     3.63     0.00      6.7    58.07     0.00
23     0.00     0.00   0.00     3.42     0.02      0.0    19.42     0.02
19     0.00     0.00   0.00     0.32     0.02      0.0    21.03     0.02
   Polyjuni Polycomm Pohlnuta Ptilcili Barbhatc Cladarbu Cladrang Cladstel
18     0.13     0.00     0.13     0.12     0.00    21.73    21.47     3.50
15     0.23     0.00     0.03     0.02     0.00    12.05     8.13     0.18
24     0.23     0.00     0.32     0.03     0.00     3.58     5.52     0.07
27     0.00     0.13     0.02     0.08     0.08     1.42     7.63     2.55
23     2.12     0.00     0.17     1.80     0.02     9.08     9.22     0.05
19     1.58     0.18     0.07     0.27     0.02     7.23     4.95    22.08
   Cladunci Cladcocc Cladcorn Cladgrac Cladfimb Cladcris Cladchlo Cladbotr
18     0.30     0.18     0.23     0.25     0.25     0.23     0.00     0.00
15     2.65     0.13     0.18     0.23     0.25     1.23     0.00     0.00
24     8.93     0.00     0.20     0.48     0.00     0.07     0.10     0.02
27     0.15     0.00     0.38     0.12     0.10     0.03     0.00     0.02
23     0.73     0.08     1.42     0.50     0.17     1.78     0.05     0.05
19     0.25     0.10     0.25     0.18     0.10     0.12     0.05     0.02
   Cladamau Cladsp Cetreric Cetrisla Flavniva Nepharct Stersp Peltapht Icmaeric
18     0.08   0.02     0.02     0.00     0.12     0.02   0.62     0.02        0
15     0.00   0.00     0.15     0.03     0.00     0.00   0.85     0.00        0
24     0.00   0.00     0.78     0.12     0.00     0.00   0.03     0.00        0
27     0.00   0.02     0.00     0.00     0.00     0.00   0.00     0.07        0
23     0.00   0.00     0.00     0.00     0.02     0.00   1.58     0.33        0
19     0.00   0.00     0.00     0.00     0.02     0.00   0.28     0.00        0
   Cladcerv Claddefo Cladphyl
18        0     0.25        0
15        0     1.00        0
24        0     0.33        0
27        0     0.15        0
23        0     1.97        0
19        0     0.37        0

In [ ]: