Kernel Density Estimation
Kernel Density Estimation
f(x) 추정
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
import seaborn as sns
## 데이터가 들어오면 해당 데이터 근처 lambda 범위만큼 탐색한다.
def kernel(x,y,x0,lamb,type=['quad','tri']):
dist = np.where(abs(x-x0)>1,lamb,abs(x-x0))/lamb
quad = (3/4*(1-dist**2)); tri=((1-abs(dist)**3)**3)
message = 'You have to choose type'
dens = {type=='quad':quad, type=='tri':tri}.get(True, message)
hat = (dens*y).sum(axis=1)/(dens).sum(axis=1)
return(hat)
dataset = load_boston()
x = dataset['data'].transpose()[-1]
y = dataset['target']
x0 = np.reshape(np.linspace(1,35,100),(-1,1)) #min(x)근처로 주자
y_hat = kernel(x=x,y=y,x0=x0,lamb=3, type='quad')
fig, ax = plt.subplots()
sns.lineplot(x=x,y=y)
sns.lineplot(x=x0.reshape(1,-1)[0],y=y_hat)
def kde(x,x0,lamb,type=['quad','tri'])
dist = np.where(abs(x-x0)>1,lamb,abs(x-x0))/lamb
quad = (3/4*(1-dist**2)); tri=((1-abs(dist)**3)**3)
message = 'You have to choose type'
dens = {type=='quad':quad, type=='tri':tri}.get(True, message)
hat = (dens).sum(axis=1) / len(x)*lamb
return(hat)
fig, ax = plt.subplots()
sns.lineplot(x=x,y=y)
sns.lineplot(x=x0.reshape(1,-1)[0],y=y_hat)
Last updated