{ "cells": [ { "cell_type": "markdown", "id": "50d1198e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "

Regresja jądrowa

\n", "\n", "####
Karolina Oparczyk, Tomasz Grzybowski, Jan Nowak
\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." ] }, { "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", "\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": [ "Do implementacji regresji jądrowej można użyć wielu różnych jąder. Przykłady użyte w projekcie:\n", "* jądro Gaussa\n", "\\begin{equation}\n", "K(x) = \\frac1{h\\sqrt{2\\pi}}e^{-\\frac12(\\frac{x - x_i}h)^2}\n", "\\end{equation}" ] }, { "cell_type": "code", "execution_count": 1, "id": "italic-credits", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "hovertemplate": "x=%{x}
y=%{y}", "legendgroup": "", "line": { "color": "#636efa", "dash": "solid" }, "mode": "lines", "name": "", "orientation": "v", "showlegend": false, "type": "scatter", "x": [ -4, -3.9, -3.8, -3.6999999999999997, -3.5999999999999996, -3.4999999999999996, -3.3999999999999995, -3.2999999999999994, -3.1999999999999993, -3.099999999999999, -2.999999999999999, -2.899999999999999, -2.799999999999999, -2.699999999999999, -2.5999999999999988, -2.4999999999999987, -2.3999999999999986, -2.2999999999999985, -2.1999999999999984, -2.0999999999999983, -1.9999999999999982, -1.8999999999999981, -1.799999999999998, -1.699999999999998, -1.5999999999999979, -1.4999999999999978, -1.3999999999999977, -1.2999999999999976, -1.1999999999999975, -1.0999999999999974, -0.9999999999999973, -0.8999999999999972, -0.7999999999999972, -0.6999999999999971, -0.599999999999997, -0.4999999999999969, -0.3999999999999968, -0.2999999999999967, -0.19999999999999662, -0.09999999999999654, 3.552713678800501e-15, 0.1000000000000032, 0.20000000000000373, 0.30000000000000426, 0.4000000000000039, 0.5000000000000036, 0.6000000000000041, 0.7000000000000046, 0.8000000000000043, 0.9000000000000039, 1.0000000000000044, 1.100000000000005, 1.2000000000000046, 1.3000000000000043, 1.4000000000000048, 1.5000000000000053, 1.600000000000005, 1.7000000000000046, 1.8000000000000052, 1.9000000000000057, 2.0000000000000053, 2.100000000000005, 2.2000000000000055, 2.300000000000006, 2.4000000000000057, 2.5000000000000053, 2.600000000000006, 2.7000000000000064, 2.800000000000006, 2.9000000000000057, 3.000000000000006, 3.1000000000000068, 3.2000000000000064, 3.300000000000006, 3.4000000000000066, 3.500000000000007, 3.6000000000000068, 3.7000000000000064, 3.800000000000007, 3.9000000000000075 ], "xaxis": "x", "y": [ 0.00013383022576488537, 0.00019865547139277272, 0.00029194692579146027, 0.0004247802705507522, 0.000611901930113773, 0.0008726826950457617, 0.001232219168473021, 0.0017225689390536843, 0.0023840882014648486, 0.0032668190561999273, 0.004431848411938019, 0.005952532419775869, 0.00791545158297999, 0.010420934814422628, 0.013582969233685661, 0.017528300493568592, 0.02239453029484297, 0.028327037741601276, 0.03547459284623157, 0.04398359598042735, 0.05399096651318825, 0.06561581477467683, 0.07895015830089443, 0.09404907737688725, 0.11092083467945592, 0.12951759566589216, 0.14972746563574535, 0.1713685920478079, 0.19418605498321354, 0.21785217703255116, 0.24197072451914403, 0.2660852498987555, 0.28969155276148345, 0.31225393336676194, 0.3332246028918003, 0.3520653267643001, 0.36827014030332383, 0.38138781546052447, 0.39104269397545616, 0.3969525474770119, 0.3989422804014327, 0.39695254747701164, 0.3910426939754556, 0.3813878154605236, 0.3682701403033227, 0.35206532676429886, 0.33322460289179884, 0.3122539333667603, 0.2896915527614818, 0.2660852498987539, 0.2419707245191423, 0.21785217703254933, 0.19418605498321187, 0.1713685920478064, 0.14972746563574385, 0.1295175956658907, 0.11092083467945468, 0.0940490773768862, 0.07895015830089343, 0.06561581477467589, 0.05399096651318749, 0.04398359598042675, 0.035474592846231015, 0.028327037741600784, 0.022394530294842594, 0.017528300493568304, 0.013582969233685408, 0.010420934814422415, 0.007915451582979832, 0.005952532419775758, 0.004431848411937925, 0.003266819056199852, 0.0023840882014647936, 0.001722568939053646, 0.0012322191684729913, 0.0008726826950457385, 0.0006119019301137573, 0.0004247802705507417, 0.0002919469257914525, 0.00019865547139276686 ], "yaxis": "y" } ], "layout": { "legend": { "tracegroupgap": 0 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Funkcja jądrowa Gaussa" }, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "title": { "text": "x" } }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "title": { "text": "y" } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import KernelRegression\n", "import plotly.express as px\n", "import numpy as np\n", "kernel_x = np.arange(-4,4,0.1)\n", "col = KernelRegression.kernel_function(1, kernel_x, 0)\n", "px.line(x=kernel_x, y=col, title='Funkcja jądrowa Gaussa')" ] }, { "cell_type": "markdown", "id": "f46e92e5", "metadata": {}, "source": [ "* jądro Epanechnikova\n", "\\begin{equation}\n", "K(x) = (\\frac34)(1-(\\frac{x - x_i}h)^2) \\text{ dla } {|x|\\leq1}\n", "\\end{equation}" ] }, { "cell_type": "code", "execution_count": 2, "id": "victorian-equality", "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "hovertemplate": "x=%{x}
y=%{y}", "legendgroup": "", "line": { "color": "#636efa", "dash": "solid" }, "mode": "lines", "name": "", "orientation": "v", "showlegend": false, "type": "scatter", "x": [ -2, -1.9, -1.7999999999999998, -1.6999999999999997, -1.5999999999999996, -1.4999999999999996, -1.3999999999999995, -1.2999999999999994, -1.1999999999999993, -1.0999999999999992, -0.9999999999999991, -0.899999999999999, -0.7999999999999989, -0.6999999999999988, -0.5999999999999988, -0.49999999999999867, -0.3999999999999986, -0.2999999999999985, -0.1999999999999984, -0.09999999999999831, 1.7763568394002505e-15, 0.10000000000000187, 0.20000000000000195, 0.30000000000000204, 0.40000000000000213, 0.5000000000000022, 0.6000000000000023, 0.7000000000000024, 0.8000000000000025, 0.9000000000000026, 1.0000000000000027, 1.1000000000000028, 1.2000000000000028, 1.300000000000003, 1.400000000000003, 1.500000000000003, 1.6000000000000032, 1.7000000000000033, 1.8000000000000034, 1.9000000000000035 ], "xaxis": "x", "y": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.3322676295501878e-15, 0.1425000000000013, 0.27000000000000124, 0.3825000000000012, 0.4800000000000011, 0.562500000000001, 0.6300000000000009, 0.6825000000000007, 0.7200000000000004, 0.7425000000000003, 0.75, 0.7424999999999997, 0.7199999999999994, 0.6824999999999991, 0.6299999999999988, 0.5624999999999983, 0.47999999999999793, 0.3824999999999975, 0.269999999999997, 0.14249999999999655, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "yaxis": "y" } ], "layout": { "legend": { "tracegroupgap": 0 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Funkcja jądrowa Epanechnikova" }, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "title": { "text": "x" } }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "title": { "text": "y" } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "kernel_x = np.arange(-2,2,0.1)\n", "col = KernelRegression.epanechnikov_list(1, kernel_x, 0)\n", "px.line(x=kernel_x, y=col, title='Funkcja jądrowa Epanechnikova')" ] }, { "cell_type": "markdown", "id": "6d60bbc1", "metadata": {}, "source": [ "Istotne znaczenie ma nie tylko dobór jądra, ale również parametru wygładzania, czyli szerokości okna. W zależności od niego, punkty są grupowane i dla każdej grupy wyliczana jest wartość funkcji. Jeśli okno będzie zbyt szerokie, funkcja będzie bardziej przypominała prostą (under-fitting). Natomiast jeśli będzie zbyt wąskie, funkcja będzie za bardzo \"skakać\" (over-fitting).\n", "\n", "Wyliczenie wartości funkcji polega na wzięciu średniej ważonej z $y_{i}$\n", " dla takich $x_{i}$, które znajdują się blisko x, dla którego wyznaczamy wartość. Wagi przy $y_{i}$ dla x sumują się do 1 i są wyższe, kiedy $x_{i}$ jest bliżej x oraz niższe w przeciwnym przypadku.\n", "

\n", "
\\begin{equation}w_i= \\frac{K_i}{\\sum\\limits_{i=1}^n K_i} \\end{equation}\n", "

\n", "\\begin{equation} Y=w_1*x_1+w_2*x_2+ \\text{...} +w_n*x_n \\end{equation}
\n", "" ] }, { "cell_type": "code", "execution_count": 3, "id": "4ae1bce9", "metadata": {}, "outputs": [], "source": [ "import ipywidgets as widgets\n", "import plotly.graph_objs as go\n", "import pandas as pd \n", "\n", "fires_thefts = pd.read_csv('fires_thefts.csv', names=['x','y'])\n", "fires_thefts=fires_thefts.sort_values('x')\n", "X = np.array(fires_thefts.x)\n", "Y = np.array(fires_thefts.y)" ] }, { "cell_type": "code", "execution_count": 4, "id": "loved-clinton", "metadata": {}, "outputs": [], "source": [ "\n", "dropdown_bw = widgets.Dropdown(options=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], value=1, description='Szerokość okna')\n", "\n", "def interactive_kernel(bw_manual): \n", " Y_pred_gauss = KernelRegression.ker_reg(X, Y, bw_manual, 'gauss')\n", " Y_pred_epanechnikov = KernelRegression.ker_reg(X, Y, bw_manual, 'epanechnikov')\n", "\n", " fig = px.scatter(x=X,y=Y)\n", " fig.add_trace(go.Scatter(x=X, y=np.array(Y_pred_gauss), name='Gauss', mode='lines'))\n", " fig.add_trace(go.Scatter(x=X, y=np.array(Y_pred_epanechnikov), name='Epanechnikov', mode='lines'))\n", " fig.show()\n", " \n", " # kernel regression\n", " kernel_x = np.arange(min(X)-5,max(X)+5, 0.1)\n", "\n", " ## Plotting gaussian for all input x points \n", " kernel_fns = {'kernel_x': kernel_x}\n", " for input_x in X: \n", " input_string= 'x_value_{}'.format(np.round(input_x,2)) \n", " kernel_fns[input_string] = KernelRegression.kernel_function(bw_manual, kernel_x, input_x)\n", "\n", " kernels_df = pd.DataFrame(data=kernel_fns)\n", " y_all = kernels_df.drop(columns='kernel_x')\n", " fig = px.line(kernels_df, x='kernel_x', y=y_all.columns, title='Gaussian for all input points', range_x=[min(X)-5,max(X)+5]) \n", " fig.show()" ] }, { "cell_type": "code", "execution_count": 5, "id": "injured-english", "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "722bc244f3d24c5ba1875e43ed5be82c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='Szerokość okna', options=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), value=1)…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "widgets.interact(interactive_kernel, bw_manual=dropdown_bw)" ] }, { "cell_type": "code", "execution_count": 6, "id": "demographic-clearing", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Input_xkernel_xweigthYY=w0*x0+w1*x1+...+w41*x41
028.6K(2.0)0.000000150611
128.6K(2.2)0.00000018629
228.6K(2.2)0.000000186214
328.6K(2.5)0.000000255122
428.6K(3.4)0.000000642317
528.6K(3.6)0.000000785115
628.6K(4.0)0.000001167516
728.6K(4.8)0.000002532719
828.6K(5.0)0.000003061532
928.6K(5.4)0.000004451827
1028.6K(5.6)0.000005355423
1128.6K(5.7)0.000005870311
1228.6K(6.2)0.000009234129
1328.6K(6.9)0.000017120918
1428.6K(7.2)0.000022173629
1528.6K(7.3)0.000024150431
1628.6K(7.7)0.000033848737
1728.6K(8.6)0.000070675653
1828.6K(9.0)0.000097018439
1928.6K(9.5)0.000142865144
2028.6K(10.5)0.000300636142
2128.6K(10.5)0.00030063613632.501314
2228.6K(10.7)0.000347199943
2328.6K(10.8)0.000372896434
2428.6K(11.0)0.000429619875
2528.6K(11.3)0.000529694634
2628.6K(11.9)0.000796558646
2728.6K(12.2)0.000971557746
2828.6K(15.1)0.005503286225
2928.6K(15.1)0.005503286230
3028.6K(16.5)0.011270012240
3128.6K(17.4)0.017142236932
3228.6K(18.4)0.026299381332
3328.6K(18.5)0.027389107922
3428.6K(21.6)0.079070939131
3528.6K(21.8)0.08355836864
3628.6K(23.3)0.120126507129
3728.6K(28.6)0.210681057327
3828.6K(29.1)0.209630281234
3928.6K(34.1)0.115047537668
4028.6K(36.2)0.066363381041
4128.6K(39.7)0.0179240863147
\n", "
" ], "text/plain": [ " Input_x kernel_x weigth Y Y=w0*x0+w1*x1+...+w41*x41\n", "0 28.6 K(2.0) 0.0000001506 11 \n", "1 28.6 K(2.2) 0.0000001862 9 \n", "2 28.6 K(2.2) 0.0000001862 14 \n", "3 28.6 K(2.5) 0.0000002551 22 \n", "4 28.6 K(3.4) 0.0000006423 17 \n", "5 28.6 K(3.6) 0.0000007851 15 \n", "6 28.6 K(4.0) 0.0000011675 16 \n", "7 28.6 K(4.8) 0.0000025327 19 \n", "8 28.6 K(5.0) 0.0000030615 32 \n", "9 28.6 K(5.4) 0.0000044518 27 \n", "10 28.6 K(5.6) 0.0000053554 23 \n", "11 28.6 K(5.7) 0.0000058703 11 \n", "12 28.6 K(6.2) 0.0000092341 29 \n", "13 28.6 K(6.9) 0.0000171209 18 \n", "14 28.6 K(7.2) 0.0000221736 29 \n", "15 28.6 K(7.3) 0.0000241504 31 \n", "16 28.6 K(7.7) 0.0000338487 37 \n", "17 28.6 K(8.6) 0.0000706756 53 \n", "18 28.6 K(9.0) 0.0000970184 39 \n", "19 28.6 K(9.5) 0.0001428651 44 \n", "20 28.6 K(10.5) 0.0003006361 42 \n", "21 28.6 K(10.5) 0.0003006361 36 32.501314\n", "22 28.6 K(10.7) 0.0003471999 43 \n", "23 28.6 K(10.8) 0.0003728964 34 \n", "24 28.6 K(11.0) 0.0004296198 75 \n", "25 28.6 K(11.3) 0.0005296946 34 \n", "26 28.6 K(11.9) 0.0007965586 46 \n", "27 28.6 K(12.2) 0.0009715577 46 \n", "28 28.6 K(15.1) 0.0055032862 25 \n", "29 28.6 K(15.1) 0.0055032862 30 \n", "30 28.6 K(16.5) 0.0112700122 40 \n", "31 28.6 K(17.4) 0.0171422369 32 \n", "32 28.6 K(18.4) 0.0262993813 32 \n", "33 28.6 K(18.5) 0.0273891079 22 \n", "34 28.6 K(21.6) 0.0790709391 31 \n", "35 28.6 K(21.8) 0.0835583686 4 \n", "36 28.6 K(23.3) 0.1201265071 29 \n", "37 28.6 K(28.6) 0.2106810573 27 \n", "38 28.6 K(29.1) 0.2096302812 34 \n", "39 28.6 K(34.1) 0.1150475376 68 \n", "40 28.6 K(36.2) 0.0663633810 41 \n", "41 28.6 K(39.7) 0.0179240863 147 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bw_manual = 5\n", "input_x = X[37]\n", "kernel_x = X\n", "weigths = KernelRegression.weights(bw_manual, input_x, X)\n", "# Dataframe for a single observation point x_i. In the code x_i comes from new_x\n", "data = {'Input_x': [input_x for x in kernel_x],\n", " 'kernel_x': kernel_x,\n", " 'weigth': weigths,\n", " 'Y': Y,\n", " 'Y=w0*x0+w1*x1+...+w41*x41': ''\n", " }\n", "single_pt_KE = pd.DataFrame(data=data)\n", "single_pt_KE.at[len(Y)//2, 'Y=w0*x0+w1*x1+...+w41*x41'] = KernelRegression.single_y_pred_gauss(3, X[37], X, Y)\n", "single_pt_KE['weigth'] = single_pt_KE['weigth'].map('{:,.10f}'.format)\n", "single_pt_KE['kernel_x'] = single_pt_KE['kernel_x'].map('K({})'.format)\n", "single_pt_KE" ] }, { "cell_type": "code", "execution_count": 7, "id": "0406c2c4", "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "hovertemplate": "x=%{x}
y=%{y}", "legendgroup": "", "marker": { "color": "#636efa", "symbol": "circle" }, "mode": "markers", "name": "", "orientation": "v", "showlegend": false, "type": "scatter", "x": [ 2, 2.2, 2.2, 2.5, 3.4, 3.6, 4, 4.8, 5, 5.4, 5.6, 5.7, 6.2, 6.9, 7.2, 7.3, 7.7, 8.6, 9, 9.5, 10.5, 10.5, 10.7, 10.8, 11, 11.3, 11.9, 12.2, 15.1, 15.1, 16.5, 17.4, 18.4, 18.5, 21.6, 21.8, 23.3, 28.6, 29.1, 34.1, 36.2, 39.7 ], "xaxis": "x", "y": [ 11, 9, 14, 22, 17, 15, 16, 19, 32, 27, 23, 11, 29, 18, 29, 31, 37, 53, 39, 44, 42, 36, 43, 34, 75, 34, 46, 46, 25, 30, 40, 32, 32, 22, 31, 4, 29, 27, 34, 68, 41, 147 ], "yaxis": "y" }, { "mode": "lines", "name": "Gauss", "type": "scatter", "x": [ 2, 2.2, 2.2, 2.5, 3.4, 3.6, 4, 4.8, 5, 5.4, 5.6, 5.7, 6.2, 6.9, 7.2, 7.3, 7.7, 8.6, 9, 9.5, 10.5, 10.5, 10.7, 10.8, 11, 11.3, 11.9, 12.2, 15.1, 15.1, 16.5, 17.4, 18.4, 18.5, 21.6, 21.8, 23.3, 28.6, 29.1, 34.1, 36.2, 39.7 ], "y": [ 17.555205563509922, 17.734843463682793, 17.734843463682793, 18.176366652242898, 19.580841388863796, 19.84035782372782, 20.661171088236117, 22.532186287192758, 23.01010695460826, 23.92441595297218, 24.445451332794832, 24.73853395760963, 26.68692680288166, 29.84251316046013, 31.222859934057883, 31.605842609245123, 33.05127958899505, 36.1359944873751, 37.28208616780045, 38.58499145748456, 41.10240606850776, 41.10240606850776, 41.45712040143889, 41.525714807709875, 41.67692927789663, 41.903083135696434, 42.300849506181365, 42.30323115161558, 36.02633653997792, 36.02633653997792, 31.510438483886222, 29.409303882195445, 28.074219516984098, 27.955394461859985, 23.38428545354991, 23.17743153918791, 21.648262601291027, 30.482412060301506, 30.517587939698494, 55.80587848212328, 73.69506342132328, 111.19867549668874 ] }, { "mode": "lines", "name": "Epanechnikov", "type": "scatter", "x": [ 2, 2.2, 2.2, 2.5, 3.4, 3.6, 4, 4.8, 5, 5.4, 5.6, 5.7, 6.2, 6.9, 7.2, 7.3, 7.7, 8.6, 9, 9.5, 10.5, 10.5, 10.7, 10.8, 11, 11.3, 11.9, 12.2, 15.1, 15.1, 16.5, 17.4, 18.4, 18.5, 21.6, 21.8, 23.3, 28.6, 29.1, 34.1, 36.2, 39.7 ], "y": [ 24.053734059882288, 24.276509858788508, 24.276509858788508, 24.61702998330566, 25.681680046599226, 25.926362984048545, 26.4235668871939, 27.44464655254659, 27.704350480246756, 28.227529069515292, 28.490508958799865, 28.62223196129558, 29.281772246191295, 30.20079719855705, 30.590043277413265, 30.71885452244941, 31.228179944929696, 32.32627562117898, 32.78557805593901, 33.32775548623167, 34.28029136722303, 34.28029136722303, 34.44612286429198, 34.52562056417806, 34.67753283086536, 34.886973548993915, 35.235024626327515, 35.37138690162655, 35.24918242577492, 35.24918242577492, 34.27913887569741, 33.41792501130564, 32.327340899676955, 32.21403007187681, 29.025906386148797, 28.879485913370026, 28.20705059999523, 35.21022942157131, 36.78097788065531, 58.843114179798405, 69.8487439590774, 88.04892296264353 ] }, { "mode": "lines", "name": "Linear", "type": "scatter", "x": [ 2, 2.2, 2.2, 2.5, 3.4, 3.6, 4, 4.8, 5, 5.4, 5.6, 5.7, 6.2, 6.9, 7.2, 7.3, 7.7, 8.6, 9, 9.5, 10.5, 10.5, 10.7, 10.8, 11, 11.3, 11.9, 12.2, 15.1, 15.1, 16.5, 17.4, 18.4, 18.5, 21.6, 21.8, 23.3, 28.6, 29.1, 34.1, 36.2, 39.7 ], "y": [ 19.622069242587386, 19.88476044357217, 19.88476044357217, 20.278797245049343, 21.460907649480866, 21.72359885046565, 22.248981252435215, 23.299746056374342, 23.56243725735913, 24.087819659328694, 24.350510860313474, 24.481856460805865, 25.138584463267826, 26.058003666714562, 26.452040468191736, 26.583386068684128, 27.108768470653693, 28.290878875085216, 28.81626127705478, 29.47298927951674, 30.786445284440653, 30.786445284440653, 31.049136485425436, 31.180482085917827, 31.443173286902606, 31.837210088379784, 32.62528369133413, 33.0193204928113, 36.82834290709066, 36.82834290709066, 38.66718131398413, 39.84929171841566, 41.162747723339564, 41.29409332383196, 45.365806939096096, 45.62849814008088, 47.59868214746675, 54.55999897356349, 55.21672697602545, 61.78400700064502, 64.54226461098523, 69.13936062821894 ] } ], "layout": { "legend": { "tracegroupgap": 0 }, "margin": { "t": 60 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "title": { "text": "x" } }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "title": { "text": "y" } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sklearn.linear_model import LinearRegression\n", "# linear regression\n", "reg = LinearRegression().fit(X.reshape(-1, 1), Y.reshape(-1, 1))\n", "Y_pred_linear = reg.predict(X.reshape(-1, 1))\n", "\n", "# kernel regression\n", "Y_pred_gauss = KernelRegression.ker_reg(X, Y, bw_manual, 'gauss')\n", "Y_pred_epanechnikov = KernelRegression.ker_reg(X, Y, bw_manual, 'epanechnikov')\n", "\n", "fig = px.scatter(x=X,y=Y)\n", "fig.add_trace(go.Scatter(x=X, y=np.array(Y_pred_gauss), name='Gauss', mode='lines'))\n", "fig.add_trace(go.Scatter(x=X, y=np.array(Y_pred_epanechnikov), name='Epanechnikov', mode='lines'))\n", "fig.add_trace(go.Scatter(x=X, y=np.array(Y_pred_linear.flatten().tolist()), name='Linear', mode='lines'))" ] } ], "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 }