lib with functions
This commit is contained in:
parent
654cb79a5d
commit
0f069f6061
23033
.ipynb_checkpoints/KernelRegression-checkpoint.ipynb
Normal file
23033
.ipynb_checkpoints/KernelRegression-checkpoint.ipynb
Normal file
File diff suppressed because one or more lines are too long
99
.ipynb_checkpoints/kernel_regression-checkpoint.ipynb
Normal file
99
.ipynb_checkpoints/kernel_regression-checkpoint.ipynb
Normal file
@ -0,0 +1,99 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "50d1198e",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"<h1><center>Regresja jądrowa</center></h1>\n",
|
||||
"\n",
|
||||
"#### <center>Karolina Oparczyk, Tomasz Grzybowski, Jan Nowak</center>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f792be04",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Regresja jądrowa używana jest jako funkcja wagi do opracowania modelu regresji nieparametrycznej. Nadaje ona niektórym elementom zbioru większą \"wagę\", która ma wpływ na ostateczny wynik. \n",
|
||||
"\n",
|
||||
"Można ją porównać do rysowania krzywej na wykresie punktowym tak, aby była jak najlepiej do nich dopasowana. Jest mniej wrażliwa na wartości odstające, niż na przykład regresja liniowa."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ce35888e",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Właściwości regresji jądrowej:\n",
|
||||
"* symetryczna - wartość maksymalna leży pośrodku krzywej\n",
|
||||
"<img src=\"files/symmetric.PNG\">\n",
|
||||
"* powierzchnia pod krzywą funkcji wynosi 1\n",
|
||||
"* wartość funkcji jądrowej nie jest ujemna"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "dbe6165c",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Przykłady:\n",
|
||||
"* jądro Gaussa\n",
|
||||
"\\begin{equation}\n",
|
||||
"K(x) = (2\\pi)^{-\\frac12} \\text{ exp } (-\\frac{{x}^{2}}2) \\text{ lg}(x)\n",
|
||||
"\\end{equation}\n",
|
||||
"<img src=\"files/gauss.png\">"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f46e92e5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"* jądro Epanechnikova\n",
|
||||
"\\begin{equation}\n",
|
||||
"K(x) = (\\frac34)(1-x^2)l_{|x|\\leq1}(x)\n",
|
||||
"\\end{equation}\n",
|
||||
"<img src=\"files/epanechnikov.png\">"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"celltoolbar": "Slideshow",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
File diff suppressed because one or more lines are too long
37
KernelRegression.py
Executable file
37
KernelRegression.py
Executable file
@ -0,0 +1,37 @@
|
||||
import numpy as np
|
||||
|
||||
def gauss_const(h):
|
||||
"""
|
||||
Returns the normalization constant for a gaussian
|
||||
"""
|
||||
return 1/(h*np.sqrt(np.pi*2))
|
||||
|
||||
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
|
||||
gauss_exp to get this result
|
||||
"""
|
||||
const = gauss_const(h)
|
||||
gauss_val = const*np.exp(gauss_exp(ker_x,xi,h))
|
||||
return gauss_val
|
||||
|
||||
def weights(bw_manual, input_x, all_input_values ):
|
||||
w_row = []
|
||||
for x_i in all_input_values:
|
||||
ki = kernel_function(bw_manual, x_i, input_x)
|
||||
ki_sum = np.sum(kernel_function(bw_manual, all_input_values, input_x))
|
||||
w_row.append(ki/ki_sum)
|
||||
return w_row
|
||||
|
||||
def single_y_pred(bw_manual, input_x, iks, igrek):
|
||||
w = weights(bw_manual, input_x, iks)
|
||||
y_single = np.sum(np.dot(igrek,w))
|
||||
return y_single
|
BIN
__pycache__/KernelRegression.cpython-38.pyc
Normal file
BIN
__pycache__/KernelRegression.cpython-38.pyc
Normal file
Binary file not shown.
@ -91,7 +91,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.2"
|
||||
"version": "3.8.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
Loading…
Reference in New Issue
Block a user