In [1]:
%load_ext autoreload
%autoreload 2
In [1]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_context('paper',font_scale=1.4)
In [2]:
import sys
sys.path.insert(0, '../..')
In [4]:
import skccm.utilities as ut
In [22]:
X = np.array([ [0.1,0.2,.3,.4],
[.3,.3,.7,.7]])
In [23]:
ut.exp_weight(distances)
Out[23]:
In [14]:
top = np.exp(distances)
bot = np.exp(distances).sum(axis=1,keepdims=True)
In [17]:
top/bot
Out[17]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [24]:
import skccm as ccm
In [33]:
X1 = np.array([
[ 0.3, 0.6],
[ 0.2, 1.4],
[ 1.2, 0.2],
[ 0.9, 1.9]])
X2 = np.array([
[ 0.2, 0.3],
[ 0.7, 2.2],
[ 0.8, 1.4],
[ 1.7, 1.7]])
In [34]:
C = ccm.CCM()
In [35]:
C.fit(X1,X2)
In [37]:
sc1, sc2 = C.predict(X1,X2,[3,4])
In [40]:
sc1[0]
Out[40]:
In [38]:
y = X.sum(axis=1,keepdims=True)
In [39]:
print(X)
print(y)
In [40]:
R = edm.Regression()
In [41]:
R.fit(X,y)
In [42]:
R.dist_calc(X)
In [43]:
#X[1] to X[2] and X[3]
d12 = np.sqrt( (0.3 - 0.2)**2 + (0.6 - 1.4)**2 )
d13 = np.sqrt( (0.3 - 1.2)**2 + (0.6 - 0.2)**2 )
#X[2] to X[1] and X[3]
d21 = np.sqrt( (0.2 - 0.3)**2 + (1.4 - 0.6)**2 )
d23 = np.sqrt( (0.2 - 1.2)**2 + (1.4 - 0.2)**2 )
#X[2] to X[1] and X[3]
d21 = np.sqrt( (0.2 - 0.3)**2 + (1.4 - 0.6)**2 )
d23 = np.sqrt( (0.2 - 1.2)**2 + (1.4 - 0.2)**2 )
#X[3] to X[1] and X[2]
d31 = np.sqrt( (1.2 - 0.3)**2 + (0.2 - 0.6)**2 )
d32= np.sqrt( (1.2 - 0.2)**2 + (0.2 - 1.4)**2 )
print('X1:',d12, d13)
print('X2:',d21, d23)
print('X3:',d31, d32)
In [44]:
R.dist
Out[44]:
In [45]:
R.ind
Out[45]:
In [51]:
X
Out[51]:
In [46]:
y
Out[46]:
In [47]:
p = R.predict(X,[1,2,3])
In [49]:
p[0]
Out[49]:
In [52]:
p[1]
Out[52]:
In [55]:
#first sample
p1_test = np.empty((3,1))
p1_test[0] = (0.9 + 1.6)/2
p1_test[1] = (1.6 + 0.9)/2
p1_test[2] = (1.4 + 0.9)/2
p1_test
Out[55]:
In [57]:
p[2]
Out[57]:
In [61]:
np.array([np.mean(y)]*3).reshape(3,1)
Out[61]:
In [115]:
Xtr = np.array([
[ 0.3, 0.6],
[ 0.2, 1.4],
[ 1.2, 0.2]])
Xte = np.array([
[ 0.7, 1.6],
[ 1.3, 0.4],
])
ytr = Xtr.sum(axis=1,keepdims=True)
yte = Xte.sum(axis=1,keepdims=True)
In [143]:
yte
Out[143]:
In [122]:
R = edm.Regression(weights='distance')
In [123]:
R.fit(Xtr,ytr)
In [124]:
p = R.predict(Xte,[1,2,3])
In [125]:
print(R.ind)
print(R.dist)
In [126]:
print('ytr',ytr)
print('yte',yte)
print('Xtr',Xtr)
print('Xte',Xte)
In [127]:
p[0]
Out[127]:
In [129]:
R.dist
Out[129]:
In [130]:
R.ind
Out[130]:
In [132]:
ytr
Out[132]:
In [137]:
W = 1/R.dist[:,0:2]
W/= np.sum(W,axis=1,keepdims=True)
In [138]:
W
Out[138]:
In [146]:
np.sum(W*ytr[R.ind[:,0:2],0],axis=1).reshape(-1,1)
Out[146]:
In [128]:
p[1]
Out[128]:
In [216]:
p[2]
Out[216]:
In [230]:
Xtr = np.array([
[ 3, 6],
[ 2, 1],
[ 1, 3]])
Xte = np.array([
[ 3, 5],
[ 2, 2],
])
ytr = np.array([[9],
[3],
[4]])
In [231]:
R = edm.Classification()
In [232]:
R.fit(Xtr,ytr)
In [233]:
p = R.predict(Xte,[1,2,3])
In [234]:
R.dist
Out[234]:
In [235]:
R.ind
Out[235]:
In [236]:
np.array([[9.],
[3.]])
p[0]
Out[236]:
In [237]:
p[1]
Out[237]:
In [238]:
p[2]
Out[238]:
In [287]:
Xtr = np.array([
[ 3, 5],
[ 2, 1],
[ 1, 3]])
Xte = np.array([
[ 3, 5],
[ 2, 2],
])
ytr = np.array([[9],
[3],
[9]])
In [288]:
R = edm.Classification(weights='distance')
In [289]:
R.fit(Xtr,ytr)
In [290]:
p = R.predict(Xte,[1,2,3])
In [291]:
R.dist
Out[291]:
In [292]:
R.ind
Out[292]:
In [301]:
W = 1/(R.dist+.00001)
W/np.sum(W,axis=1,keepdims=True)
#W
Out[301]:
In [302]:
W
Out[302]:
In [303]:
W[1,1] + W[1,2]
Out[303]:
In [305]:
W[1,0]
Out[305]:
In [294]:
np.array([[9.],
[3.]])
p[0]
Out[294]:
In [295]:
p[1]
Out[295]:
In [296]:
p[2]
Out[296]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [306]:
ut.quick_mode_axis1(x).shape
Out[306]:
In [184]:
np.mean(stats.mode(x,axis=1)[0].ravel() == ut.quick_mode_axis1(x))
Out[184]:
In [189]:
stats.mode([5,5,5,7,7,4,4,4])
Out[189]:
In [188]:
ut.quick_mode_axis1(np.array([5,5,5,7,7,4,4,4]).reshape(1,-1))
Out[188]:
In [211]:
x = np.array([[5,5,7,7,7,4,4,4],
[4,4,3,3,3,5,5,5]])
In [212]:
sl = x[1,:]
sl
Out[212]:
In [213]:
loc = np.bincount(sl)[sl].argmax()
loc
Out[213]:
In [214]:
sl[loc]
Out[214]:
In [229]:
ut.quick_mode_axis1_keep_nearest_neigh(x)
Out[229]:
In [215]:
X = X.astype(int)
len_x = len(X)
mode = np.zeros(len_x)
for i in range(len_x):
mode[i] = np.bincount(X[i,:])[X[i,:]].argmax() #reorder before argmax
return mode