Learning how to define a custom metric in sklearn so it can be used with KDTree which in turn for correlation function


In [40]:
from sklearn.neighbors import *
import numpy as np
import math as m
from scipy import integrate
from sklearn.neighbors import KNeighborsClassifier
#from cython_metric import mydist
import time
import pickle
#import cython

In [35]:
Ez = lambda x: 1/m.sqrt(0.3*(1+x)**3+0.7)

np.vectorize(Ez)
#Calculate comoving distance of a data point using the Redshift - This definition is based on the cosmology model we take. Here the distance for E-dS universe is considered. Also note that c/H0 ratio is cancelled in the equations and hence not taken.

def DC_LCDM(z):
  return integrate.quad(Ez, 0, z)[0]
DC_LCDM=np.vectorize(DC_LCDM)

In [36]:
DC_LCDM(X)


Out[36]:
array([[ 0.47775138,  0.59532496,  0.5171643 ],
       [ 0.47483029,  0.38131355,  0.54777268],
       [ 0.3924032 ,  0.70797459,  0.75056921],
       [ 0.34879706,  0.64560646,  0.4628769 ],
       [ 0.49194619,  0.72823905,  0.06988467],
       [ 0.08539227,  0.02012603,  0.67154754],
       [ 0.63685917,  0.69472203,  0.75920732],
       [ 0.65036887,  0.41121132,  0.63839375],
       [ 0.11505747,  0.54358073,  0.13860998],
       [ 0.73948935,  0.45757248,  0.37410762],
       [ 0.24818023,  0.63431682,  0.40703905],
       [ 0.49223193,  0.01871005,  0.5278078 ],
       [ 0.52385421,  0.52730788,  0.73894905],
       [ 0.57267225,  0.329083  ,  0.39196296],
       [ 0.58346191,  0.05939948,  0.56230409],
       [ 0.56497927,  0.20008081,  0.12509766],
       [ 0.29206254,  0.33256452,  0.49352468],
       [ 0.39320702,  0.7648015 ,  0.09965626],
       [ 0.19872361,  0.15528878,  0.55281572],
       [ 0.23829562,  0.41498272,  0.23047233],
       [ 0.15312406,  0.10757698,  0.55506049],
       [ 0.13377886,  0.18760165,  0.33670733],
       [ 0.66423639,  0.09494029,  0.67487927],
       [ 0.09398217,  0.75796503,  0.41680581],
       [ 0.75813869,  0.51866012,  0.61138938],
       [ 0.03883947,  0.26406675,  0.11687321],
       [ 0.27557111,  0.11548583,  0.29423355],
       [ 0.37378708,  0.06320975,  0.57995247],
       [ 0.49088664,  0.24890956,  0.45862791],
       [ 0.09191897,  0.49773178,  0.73043118]])

In [49]:
DC_LCDM([6.0])


Out[49]:
array([ 1.92561349])

In [ ]:


In [37]:
def LCDMmetric(p1,p2):
    z1=p1[0]
    z2=p2[0]
    ra1=p1[1]*m.pi/180.0
    ra2=p2[1]*m.pi/180.0
    dec1=p1[2]*m.pi/180.0
    dec2=p2[2]*m.pi/180.0
    costheta=m.sin(dec1)*m.sin(dec2)+m.cos(dec1)*m.cos(dec2)*m.cos(ra1-ra2)
    s1=DC_LCDM(z1)
    s2=DC_LCDM(z2)
    return np.sqrt(s1**2+s2**2-2.0*s1*s2*costheta)

In [45]:
np.random.seed(0)
X = np.random.random((30,3))
r = np.linspace(0, 1, 10)
tree = BallTree(X,metric='pyfunc',func=LCDMmetric)                
s = pickle.dumps(tree)                     
treedump = pickle.loads(s) 
treedump.two_point_correlation(X,r)


/Users/rohin/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:11: RuntimeWarning: invalid value encountered in sqrt
Out[45]:
array([ 19, 263, 461, 621, 764, 851, 894, 900, 900, 900])

In [43]:
help(tree.two_point_correlation)


Help on built-in function two_point_correlation:

two_point_correlation(...)
    Compute the two-point correlation function
    
    Parameters
    ----------
    X : array_like
        An array of points to query.  Last dimension should match dimension
        of training data.
    r : array_like
        A one-dimensional array of distances
    dualtree : boolean (default = False)
        If true, use a dualtree algorithm.  Otherwise, use a single-tree
        algorithm.  Dual tree algorithms can have better scaling for
        large N.
    
    Returns
    -------
    counts : ndarray
        counts[i] contains the number of pairs of points with distance
        less than or equal to r[i]
    
    Examples
    --------
    Compute the two-point autocorrelation function of X:
    
    >>> import numpy as np
    >>> np.random.seed(0)
    >>> X = np.random.random((30, 3))
    >>> r = np.linspace(0, 1, 5)
    >>> tree = BinaryTree(X)     # doctest: +SKIP
    >>> tree.two_point_correlation(X, r)
    array([ 30,  62, 278, 580, 820])


In [ ]:


In [ ]:


In [ ]:


In [ ]:
s = pickle.dumps(tree)                     
tree_copy = pickle.loads(s)

In [16]:
x=[1,2,3]
y=[2,3,4]

In [ ]:
Z=(rdat[j]['Z']+hmqdat1[0]['Z'])/2.0
        RA1=hmqdat1[0]['RA']*math.pi/180.0
        RA2=rdat[j]['RA']*math.pi/180.0
        DEC1=hmqdat1[0]['DEC']*math.pi/180.0
        DEC2=rdat[j]['DEC']*math.pi/180.0
        deltatheta=abs(math.acos(math.sin(DEC1)*math.sin(DEC2)+math.cos(DEC1)*math.cos(DEC2)*math.cos(RA1-RA2)))

In [ ]:


In [20]:
def dist(x,y):
    x=np.array(x)
    y=np.array(y)
    return np.sqrt(np.sum(x**2+y**2-2*x*y))

In [21]:
dist(x,y)


Out[21]:
1.7320508075688772

In [19]:
x**2


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-c87530a60b58> in <module>()
----> 1 x**2

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

In [6]:
X = [[0, 1, 2],[3, 4, 5]]

In [ ]:


In [7]:
NearestNeighbors(X,metric='pyfunc', func=dist)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-df6e9a525089> in <module>()
----> 1 NearestNeighbors(X,metric='pyfunc', func=dist)

/Users/rohin/anaconda/lib/python2.7/site-packages/sklearn/neighbors/unsupervised.pyc in __init__(self, n_neighbors, radius, algorithm, leaf_size, metric, p, metric_params, n_jobs, **kwargs)
    121                           algorithm=algorithm,
    122                           leaf_size=leaf_size, metric=metric, p=p,
--> 123                           metric_params=metric_params, n_jobs=n_jobs, **kwargs)

TypeError: _init_params() got an unexpected keyword argument 'func'

In [ ]:


In [2]:
dist = DistanceMetric.get_metric('euclidean')
X = [[0, 1, 2],[3, 4, 5]]

dist.pairwise(X)


Out[2]:
array([[ 0.        ,  5.19615242],
       [ 5.19615242,  0.        ]])

In [3]:
dist.dist_to_rdist(3)


Out[3]:
9

In [26]:
def myDistance(x,y):
    return np.sqrt(x**2+y**2)

In [25]:
distc=DistanceMetric.get_metric("pyfunc",func=dist)

In [28]:
distc.pairwise(X)


Out[28]:
array([[ 0.        ,  0.29473397,  0.41689499,  0.19662693,  0.57216693,
         0.86543108,  0.46672837,  0.398299  ,  0.63410319,  0.47902444,
         0.32524478,  0.69683463,  0.3604571 ,  0.41432719,  0.67470055,
         0.70298838,  0.4231624 ,  0.58094365,  0.65182397,  0.52695324,
         0.72155933,  0.70167269,  0.71513988,  0.53962867,  0.46254427,
         0.82434437,  0.70760869,  0.67082562,  0.45712036,  0.57699367],
       [ 0.29473397,  0.        ,  0.57586803,  0.41860234,  0.76350759,
         0.63809564,  0.60361979,  0.29019522,  0.69376753,  0.47216408,
         0.48733131,  0.40653272,  0.36137426,  0.25785655,  0.39477652,
         0.57319718,  0.24894356,  0.7911861 ,  0.42635362,  0.49801823,
         0.49717437,  0.54202361,  0.46878297,  0.73376823,  0.47756304,
         0.74291578,  0.51222961,  0.38532667,  0.20139922,  0.55394837],
       [ 0.41689499,  0.57586803,  0.        ,  0.44940452,  0.90274337,
         0.94847268,  0.34159159,  0.59112128,  0.91558599,  0.83389329,
         0.54893013,  0.94813259,  0.32616912,  0.78758869,  0.92045718,
         1.10244751,  0.66976712,  0.86701686,  0.82603234,  0.85573657,
         0.88468304,  0.96274798,  0.89123976,  0.60730787,  0.6506849 ,
         1.11400043,  1.01710785,  0.87123611,  0.77650891,  0.46799626],
       [ 0.19662693,  0.41860234,  0.44940452,  0.        ,  0.51150232,
         0.88049546,  0.6034734 ,  0.5875395 ,  0.49193536,  0.63313412,
         0.14046915,  0.79970368,  0.50491365,  0.53317948,  0.80796993,
         0.76185019,  0.43534796,  0.47319593,  0.66582697,  0.45139367,
         0.72860478,  0.66332565,  0.87719186,  0.34687487,  0.65666357,
         0.73793129,  0.7106551 ,  0.7463756 ,  0.55732267,  0.53915897],
       [ 0.57216693,  0.76350759,  0.90274337,  0.51150232,  0.        ,
         1.27710576,  0.93324293,  0.87874855,  0.53771137,  0.65033634,
         0.5131562 ,  1.05880581,  0.92673589,  0.68363322,  1.05856269,
         0.72485022,  0.79290106,  0.14716665,  1.02564343,  0.58316109,
         1.0837421 ,  0.89714081,  1.1569515 ,  0.61920756,  0.84643872,
         0.8338374 ,  0.88653952,  1.07327824,  0.800232  ,  1.04098047],
       [ 0.86543108,  0.63809564,  0.94847268,  0.88049546,  1.27710576,
         0.        ,  1.10498141,  0.83929158,  0.92741024,  1.07781983,
         0.86124819,  0.527138  ,  0.80250155,  0.7907367 ,  0.63389304,
         0.93371798,  0.48884899,  1.26277375,  0.25875381,  0.75669069,
         0.21063599,  0.4989078 ,  0.73789945,  1.02320604,  1.06862013,
         0.7607878 ,  0.56412812,  0.35859114,  0.62105822,  0.56411562],
       [ 0.46672837,  0.60361979,  0.34159159,  0.6034734 ,  0.93324293,
         1.10498141,  0.        ,  0.4545101 ,  1.08906121,  0.68336753,
         0.73887243,  0.94808847,  0.3046978 ,  0.75047393,  0.87148739,
         1.08104027,  0.79828995,  0.9474642 ,  0.96555471,  0.98868459,
         1.03165458,  1.11132525,  0.78677519,  0.85825468,  0.40871472,
         1.27584208,  1.11049857,  0.92936331,  0.78593021,  0.74636395],
       [ 0.398299  ,  0.29019522,  0.59112128,  0.5875395 ,  0.87874855,
         0.83929158,  0.4545101 ,  0.        ,  0.94944076,  0.3983422 ,
         0.69916895,  0.52511165,  0.29291477,  0.37703685,  0.42924863,
         0.71003766,  0.53646373,  0.93164382,  0.67436683,  0.76511511,
         0.74063597,  0.82258235,  0.36951956,  0.92561653,  0.23194689,
         1.02250602,  0.76449654,  0.56015289,  0.39840635,  0.72977178],
       [ 0.63410319,  0.69376753,  0.91558599,  0.49193536,  0.53771137,
         0.92741024,  1.08906121,  0.94944076,  0.        ,  0.87776842,
         0.37051325,  0.90188203,  0.94075486,  0.6945953 ,  0.97245196,
         0.69986913,  0.54530462,  0.47511598,  0.70507301,  0.24204497,
         0.73838988,  0.49773286,  1.12735288,  0.46858151,  1.04562838,
         0.36649881,  0.57773202,  0.84891453,  0.69684362,  0.7889177 ],
       [ 0.47902444,  0.47216408,  0.83389329,  0.63313412,  0.65033634,
         1.07781983,  0.68336753,  0.3983422 ,  0.87776842,  0.        ,
         0.72661803,  0.66016581,  0.63212211,  0.30974865,  0.58110249,
         0.50373484,  0.66718982,  0.75596279,  0.85336687,  0.71419005,
         0.91925887,  0.87082016,  0.61226971,  0.96418776,  0.33657788,
         0.98170601,  0.76970287,  0.75365707,  0.46957117,  0.99574789],
       [ 0.32524478,  0.48733131,  0.54893013,  0.14046915,  0.5131562 ,
         0.86124819,  0.73887243,  0.69916895,  0.37051325,  0.72661803,
         0.        ,  0.83012949,  0.61909521,  0.58861941,  0.86123252,
         0.76805369,  0.42909637,  0.44893062,  0.6461955 ,  0.37385929,
         0.70137602,  0.59774097,  0.95598024,  0.26349453,  0.78490898,
         0.63651851,  0.67065328,  0.76320554,  0.5955302 ,  0.54064252],
       [ 0.69683463,  0.40653272,  0.94813259,  0.79970368,  1.05880581,
         0.527138  ,  0.94808847,  0.52511165,  0.90188203,  0.66016581,
         0.83012949,  0.        ,  0.68266554,  0.40194891,  0.1443008 ,
         0.53478043,  0.43038673,  1.10579538,  0.38839615,  0.66247668,
         0.42136228,  0.52790384,  0.34417309,  1.07815971,  0.724559  ,
         0.77282084,  0.41704075,  0.17727528,  0.26405256,  0.79542441],
       [ 0.3604571 ,  0.36137426,  0.32616912,  0.50491365,  0.92673589,
         0.80250155,  0.3046978 ,  0.29291477,  0.94075486,  0.63212211,
         0.61909521,  0.68266554,  0.        ,  0.57261762,  0.62766183,
         0.91249427,  0.54006862,  0.93623298,  0.67427789,  0.80029975,
         0.73792585,  0.85553501,  0.57013905,  0.78818071,  0.41825933,
         1.05740317,  0.8600111 ,  0.63863159,  0.54997594,  0.51997467],
       [ 0.41432719,  0.25785655,  0.78758869,  0.53317948,  0.68363322,
         0.7907367 ,  0.75047393,  0.37703685,  0.6945953 ,  0.30974865,
         0.58861941,  0.40194891,  0.57261762,  0.        ,  0.37762153,
         0.34247994,  0.38986356,  0.75289052,  0.55645952,  0.48181003,
         0.61929916,  0.57162235,  0.49895645,  0.85129263,  0.48838977,
         0.72058604,  0.46999665,  0.47336493,  0.17195021,  0.79672605],
       [ 0.67470055,  0.39477652,  0.92045718,  0.80796993,  1.05856269,
         0.63389304,  0.87148739,  0.42924863,  0.97245196,  0.58110249,
         0.86123252,  0.1443008 ,  0.62766183,  0.37762153,  0.        ,
         0.55906002,  0.49750186,  1.11690044,  0.49928493,  0.73533324,
         0.54109173,  0.64838592,  0.21419615,  1.11381156,  0.61626317,
         0.88421104,  0.53503971,  0.28455876,  0.28259277,  0.83626093],
       [ 0.70298838,  0.57319718,  1.10244751,  0.76185019,  0.72485022,
         0.93371798,  1.08104027,  0.71003766,  0.69986913,  0.50373484,
         0.76805369,  0.53478043,  0.91249427,  0.34247994,  0.55906002,
         0.        ,  0.58685856,  0.81230161,  0.70028443,  0.50300819,
         0.74159298,  0.58412514,  0.73358488,  1.01606222,  0.78855821,
         0.63564983,  0.42940845,  0.63615774,  0.41150818,  1.05204981],
       [ 0.4231624 ,  0.24894356,  0.66976712,  0.43534796,  0.79290106,
         0.48884899,  0.79828995,  0.53646373,  0.54530462,  0.66718982,
         0.42909637,  0.43038673,  0.54006862,  0.38986356,  0.49750186,
         0.58685856,  0.        ,  0.79027959,  0.24329783,  0.34715212,
         0.30996314,  0.31613074,  0.63116205,  0.65869423,  0.72394098,
         0.53418579,  0.35213694,  0.33831598,  0.27378675,  0.47228515],
       [ 0.58094365,  0.7911861 ,  0.86701686,  0.47319593,  0.14716665,
         1.26277375,  0.9474642 ,  0.93164382,  0.47511598,  0.75596279,
         0.44893062,  1.10579538,  0.93623298,  0.75289052,  1.11690044,
         0.81230161,  0.79027959,  0.        ,  1.02003914,  0.57198068,
         1.07531732,  0.88786456,  1.21743138,  0.50184716,  0.91801822,
         0.81097807,  0.90730865,  1.09699184,  0.84646472,  0.98652541],
       [ 0.65182397,  0.42635362,  0.82603234,  0.66582697,  1.02564343,
         0.25875381,  0.96555471,  0.67436683,  0.70507301,  0.85336687,
         0.6461955 ,  0.38839615,  0.67427789,  0.55645952,  0.49928493,
         0.70028443,  0.24329783,  1.02003914,  0.        ,  0.51187891,
         0.07138216,  0.29515346,  0.64263041,  0.84333435,  0.89095076,
         0.57232065,  0.34890827,  0.23059374,  0.39454187,  0.51128652],
       [ 0.52695324,  0.49801823,  0.85573657,  0.45139367,  0.58316109,
         0.75669069,  0.98868459,  0.76511511,  0.24204497,  0.71419005,
         0.37385929,  0.66247668,  0.80029975,  0.48181003,  0.73533324,
         0.50300819,  0.34715212,  0.57198068,  0.51187891,  0.        ,
         0.5524959 ,  0.31851814,  0.90048096,  0.57899785,  0.88739203,
         0.30813462,  0.3578557 ,  0.62321167,  0.4650531 ,  0.71166039],
       [ 0.72155933,  0.49717437,  0.88468304,  0.72860478,  1.0837421 ,
         0.21063599,  1.03165458,  0.74063597,  0.73838988,  0.91925887,
         0.70137602,  0.42136228,  0.73792585,  0.61929916,  0.54109173,
         0.74159298,  0.30996314,  1.07531732,  0.07138216,  0.5524959 ,
         0.        ,  0.30096523,  0.6866117 ,  0.8884132 ,  0.95925072,
         0.5757769 ,  0.36519014,  0.26195038,  0.45596479,  0.54359565],
       [ 0.70167269,  0.54202361,  0.96274798,  0.66332565,  0.89714081,
         0.4989078 ,  1.11132525,  0.82258235,  0.49773286,  0.87082016,
         0.59774097,  0.52790384,  0.85553501,  0.57162235,  0.64838592,
         0.58412514,  0.31613074,  0.88786456,  0.29515346,  0.31851814,
         0.30096523,  0.        ,  0.83444204,  0.78737832,  1.00358896,
         0.28107151,  0.18326644,  0.44561336,  0.46060195,  0.67831734],
       [ 0.71513988,  0.46878297,  0.89123976,  0.87719186,  1.1569515 ,
         0.73789945,  0.78677519,  0.36951956,  1.12735288,  0.61226971,
         0.95598024,  0.34417309,  0.57013905,  0.49895645,  0.21419615,
         0.73358488,  0.63116205,  1.21743138,  0.64263041,  0.90048096,
         0.6866117 ,  0.83444204,  0.        ,  1.19796543,  0.54019057,
         1.07743632,  0.73912016,  0.43321792,  0.43825815,  0.87535333],
       [ 0.53962867,  0.73376823,  0.60730787,  0.34687487,  0.61920756,
         1.02320604,  0.85825468,  0.92561653,  0.46858151,  0.96418776,
         0.26349453,  1.07815971,  0.78818071,  0.85129263,  1.11381156,
         1.01606222,  0.65869423,  0.50184716,  0.84333435,  0.57899785,
         0.8884132 ,  0.78737832,  1.19796543,  0.        ,  0.99342576,
         0.77834006,  0.89354425,  0.99178508,  0.85438542,  0.61041716],
       [ 0.46254427,  0.47756304,  0.6506849 ,  0.65666357,  0.84643872,
         1.06862013,  0.40871472,  0.23194689,  1.04562838,  0.33657788,
         0.78490898,  0.724559  ,  0.41825933,  0.48838977,  0.61626317,
         0.78855821,  0.72394098,  0.91801822,  0.89095076,  0.88739203,
         0.95925072,  1.00358896,  0.54019057,  0.99342576,  0.        ,
         1.16875853,  0.93650013,  0.78163156,  0.57456421,  0.90350413],
       [ 0.82434437,  0.74291578,  1.11400043,  0.73793129,  0.8338374 ,
         0.7607878 ,  1.27584208,  1.02250602,  0.36649881,  0.98170601,
         0.63651851,  0.77282084,  1.05740317,  0.72058604,  0.88421104,
         0.63564983,  0.53418579,  0.81097807,  0.57232065,  0.30813462,
         0.5757769 ,  0.28107151,  1.07743632,  0.77834006,  1.16875853,
         0.        ,  0.36340897,  0.71832631,  0.66401735,  0.86230555],
       [ 0.70760869,  0.51222961,  1.01710785,  0.7106551 ,  0.88653952,
         0.56412812,  1.11049857,  0.76449654,  0.57773202,  0.76970287,
         0.67065328,  0.41704075,  0.8600111 ,  0.46999665,  0.53503971,
         0.42940845,  0.35213694,  0.90730865,  0.34890827,  0.3578557 ,
         0.36519014,  0.18326644,  0.73912016,  0.89354425,  0.93650013,
         0.36340897,  0.        ,  0.39645171,  0.36985488,  0.78970712],
       [ 0.67082562,  0.38532667,  0.87123611,  0.7463756 ,  1.07327824,
         0.35859114,  0.92936331,  0.56015289,  0.84891453,  0.75365707,
         0.76320554,  0.17727528,  0.63863159,  0.47336493,  0.28455876,
         0.63615774,  0.33831598,  1.09699184,  0.23059374,  0.62321167,
         0.26195038,  0.44561336,  0.43321792,  0.99178508,  0.78163156,
         0.71832631,  0.39645171,  0.        ,  0.30387848,  0.64856022],
       [ 0.45712036,  0.20139922,  0.77650891,  0.55732267,  0.800232  ,
         0.62105822,  0.78593021,  0.39840635,  0.69684362,  0.46957117,
         0.5955302 ,  0.26405256,  0.54997594,  0.17195021,  0.28259277,
         0.41150818,  0.27378675,  0.84646472,  0.39454187,  0.4650531 ,
         0.45596479,  0.46060195,  0.43825815,  0.85438542,  0.57456421,
         0.66401735,  0.36985488,  0.30387848,  0.        ,  0.6962249 ],
       [ 0.57699367,  0.55394837,  0.46799626,  0.53915897,  1.04098047,
         0.56411562,  0.74636395,  0.72977178,  0.7889177 ,  0.99574789,
         0.54064252,  0.79542441,  0.51997467,  0.79672605,  0.83626093,
         1.05204981,  0.47228515,  0.98652541,  0.51128652,  0.71166039,
         0.54359565,  0.67831734,  0.87535333,  0.61041716,  0.90350413,
         0.86230555,  0.78970712,  0.64856022,  0.6962249 ,  0.        ]])

In [ ]:


In [41]:
class LCDMDistance(DistanceMetric):
    """LCDM Distance metric
       D(x, y) = \sqrt{ x^2+y^2-2xycos(theta) }
    """
    def __init__(self):
        return None
    
    def dist(x,y):
        return np.sqrt(x**2+y**2-2*x*y)
    
    def pairwise(x):
        return dist(x,x)

    def rdist_to_dist(self, rdist):
        return np.sqrt(rdist)

    def dist_to_rdist(self, dist):
        return dist ** 2

In [48]:
distlcdm=LCDMDistance.pairwise(X)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-1260f425983b> in <module>()
----> 1 distlcdm=LCDMDistance.pairwise(X)

TypeError: unbound method pairwise() must be called with LCDMDistance instance as first argument (got list instance instead)

In [47]:
distlcdm.pairwise(X)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-8b2bafa960d3> in <module>()
----> 1 distlcdm.pairwise(X)

AttributeError: 'function' object has no attribute 'pairwise'

In [60]:


In [12]:
x=[1,2,3]
x=np.array(x)

In [51]:
KDTree.valid_metrics


Out[51]:
['chebyshev',
 'euclidean',
 'cityblock',
 'manhattan',
 'infinity',
 'minkowski',
 'p',
 'l2',
 'l1']

In [54]:
KDTree.data


Out[54]:
<attribute 'data' of 'sklearn.neighbors.kd_tree.BinaryTree' objects>

In [14]:
np.random.seed(0)
X = np.random.random((30, 3))
r = np.linspace(0, 1, 5)
tree = KDTree(X,metric="pyfunc",func=dist)                
tree.two_point_correlation(X, r)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-a8210530f8f5> in <module>()
      2 X = np.random.random((30, 3))
      3 r = np.linspace(0, 1, 5)
----> 4 tree = KDTree(X,metric="pyfunc",func=dist)
      5 tree.two_point_correlation(X, r)

sklearn/neighbors/binary_tree.pxi in sklearn.neighbors.kd_tree.BinaryTree.__init__ (sklearn/neighbors/kd_tree.c:9328)()

ValueError: metric PyFuncDistance is not valid for KDTree

In [58]:
BallTree.valid_metrics


Out[58]:
['chebyshev',
 'sokalmichener',
 'canberra',
 'haversine',
 'rogerstanimoto',
 'matching',
 'dice',
 'euclidean',
 'braycurtis',
 'russellrao',
 'cityblock',
 'manhattan',
 'infinity',
 'jaccard',
 'seuclidean',
 'sokalsneath',
 'kulsinski',
 'minkowski',
 'mahalanobis',
 'p',
 'l2',
 'hamming',
 'l1',
 'wminkowski',
 'pyfunc']

In [27]:
np.random.seed(0)
X = np.random.random((30,3))
r = np.linspace(0, 1, 5)
tree = BallTree(X,metric='pyfunc',func=dist)                
tree.two_point_correlation(X, r)


Out[27]:
array([ 30,  62, 278, 580, 820])

In [10]:
np.random.seed(0)
X = np.random.random((30, 3))
r = np.linspace(0, 1, 5)
tree = BallTree(X,metric='chebyshev')                
tree.two_point_correlation(X, r)


Out[10]:
array([ 30,  82, 394, 734, 900])

In [ ]: