implementation update
This commit is contained in:
parent
d4b25bbd0f
commit
a0c2abd8a6
19031
KernelRegression.ipynb
19031
KernelRegression.ipynb
File diff suppressed because one or more lines are too long
@ -1,4 +1,5 @@
|
||||
import numpy as np
|
||||
from numpy.core.fromnumeric import squeeze
|
||||
|
||||
def gauss_const(h):
|
||||
"""
|
||||
@ -14,6 +15,14 @@ def gauss_exp(ker_x, xi, h):
|
||||
den = h*h
|
||||
return num/den
|
||||
|
||||
def gauss_exp(ker_x, xi, h):
|
||||
"""
|
||||
Returns the gaussian function exponent term
|
||||
"""
|
||||
num = - 0.5*np.square((xi- ker_x))
|
||||
den = h*h
|
||||
return num/den
|
||||
|
||||
def kernel_function(h, ker_x, xi):
|
||||
"""
|
||||
Returns the gaussian function value. Combines the gauss_const and
|
||||
@ -31,7 +40,59 @@ def weights(bw_manual, input_x, all_input_values ):
|
||||
w_row.append(ki/ki_sum)
|
||||
return w_row
|
||||
|
||||
def single_y_pred(bw_manual, input_x, iks, igrek):
|
||||
|
||||
def single_y_pred_gauss(bw_manual, input_x, iks, igrek):
|
||||
w = weights(bw_manual, input_x, iks)
|
||||
y_single = np.sum(np.dot(igrek,w))
|
||||
return y_single
|
||||
|
||||
def epanechnikov_one(h, ker_x, xi):
|
||||
"""
|
||||
Returns the epanechnikov function value.
|
||||
"""
|
||||
value = 0.75*(1-np.square((xi-ker_x)/h))
|
||||
if (value < 0):
|
||||
value = 0
|
||||
return value
|
||||
|
||||
def epanechnikov_list(h, ker_x, xi):
|
||||
"""
|
||||
Returns the epanechnikov function value.
|
||||
"""
|
||||
value = 0.75*(1-np.square((xi-ker_x)/h))
|
||||
value = [0 if i < 0 else i for i in value]
|
||||
return value
|
||||
|
||||
|
||||
def weights_epanechnikov(bw_manual, input_x, all_input_values ):
|
||||
w_row = []
|
||||
for x_i in all_input_values:
|
||||
ki = epanechnikov_one(bw_manual, x_i, input_x)
|
||||
ki_sum = np.sum(epanechnikov_list(bw_manual, all_input_values, input_x))
|
||||
w_row.append(ki/ki_sum)
|
||||
return w_row
|
||||
|
||||
def single_y_pred_epanechnikov(bw_manual, input_x, x_values, y_values):
|
||||
w = weights_epanechnikov(bw_manual, input_x, x_values)
|
||||
y_single = np.sum(np.dot(y_values,w))
|
||||
return y_single
|
||||
|
||||
def ker_reg(x_values, y_values, bw = 1, ker_fun = 'gauss'):
|
||||
|
||||
ker_x = np.arange(0,40,0.1)
|
||||
|
||||
if (ker_fun == 'gauss'):
|
||||
Y_pred = []
|
||||
for input_x in x_values:
|
||||
w = []
|
||||
Y_single = single_y_pred_epanechnikov(bw, input_x, x_values, y_values)
|
||||
Y_pred.append(Y_single)
|
||||
elif (ker_fun == 'epanechnikov'):
|
||||
Y_pred = []
|
||||
for input_x in x_values:
|
||||
w = []
|
||||
Y_single = single_y_pred_gauss(bw, input_x, x_values, y_values)
|
||||
Y_pred.append(Y_single)
|
||||
else:
|
||||
return 0
|
||||
return Y_pred
|
Binary file not shown.
20
data6.tsv
Normal file
20
data6.tsv
Normal file
@ -0,0 +1,20 @@
|
||||
21.252 -555.640
|
||||
179.842 3840.141
|
||||
118.162 2274.989
|
||||
114.269 1146.575
|
||||
121.444 1840.589
|
||||
87.624 1663.894
|
||||
170.039 3504.537
|
||||
192.651 3708.239
|
||||
12.390 -358.240
|
||||
144.264 2444.162
|
||||
169.900 3348.941
|
||||
63.254 271.623
|
||||
72.439 900.423
|
||||
71.108 77.543
|
||||
179.476 3313.424
|
||||
169.084 2525.653
|
||||
99.073 734.413
|
||||
195.528 4067.410
|
||||
131.023 2182.147
|
||||
12.424 490.714
|
|
Loading…
Reference in New Issue
Block a user