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]:
In [49]:
DC_LCDM([6.0])
Out[49]:
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)
Out[45]:
In [43]:
help(tree.two_point_correlation)
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]:
In [19]:
x**2
In [6]:
X = [[0, 1, 2],[3, 4, 5]]
In [ ]:
In [7]:
NearestNeighbors(X,metric='pyfunc', func=dist)
In [ ]:
In [2]:
dist = DistanceMetric.get_metric('euclidean')
X = [[0, 1, 2],[3, 4, 5]]
dist.pairwise(X)
Out[2]:
In [3]:
dist.dist_to_rdist(3)
Out[3]:
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]:
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)
In [47]:
distlcdm.pairwise(X)
In [60]:
In [12]:
x=[1,2,3]
x=np.array(x)
In [51]:
KDTree.valid_metrics
Out[51]:
In [54]:
KDTree.data
Out[54]:
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)
In [58]:
BallTree.valid_metrics
Out[58]:
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]:
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]:
In [ ]: