{ "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", "XXX = np.array(fires_thefts.x)\n", "YYY = 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(XXX, YYY, bw_manual, 'gauss')\n", " Y_pred_epanechnikov = KernelRegression.ker_reg(XXX, YYY, bw_manual, 'epanechnikov')\n", "\n", " fig = px.scatter(x=XXX,y=YYY)\n", " fig.add_trace(go.Scatter(x=XXX, y=np.array(Y_pred_gauss), name='Gauss', mode='lines'))\n", " fig.add_trace(go.Scatter(x=XXX, y=np.array(Y_pred_epanechnikov), name='Epanechnikov', mode='lines'))\n", " fig.show()\n", " \n", " # kernel regression\n", " kernel_x = np.arange(min(XXX)-5,max(XXX)+5, 0.1)\n", "\n", " ## Plotting gaussian for all input x points \n", " kernel_fns = {'kernel_x': kernel_x}\n", " for input_x in XXX: \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(XXX)-5,max(XXX)+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": "8ad0c3d9509b4e43a2eba34955b93ca9", "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": "honey-slide", "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.000000000011
128.6K(2.2)0.00000000009
228.6K(2.2)0.000000000014
328.6K(2.5)0.000000000022
428.6K(3.4)0.000000000017
528.6K(3.6)0.000000000015
628.6K(4.0)0.000000000016
728.6K(4.8)0.000000000019
828.6K(5.0)0.000000000032
928.6K(5.4)0.000000000027
1028.6K(5.6)0.000000000023
1128.6K(5.7)0.000000000011
1228.6K(6.2)0.000000000029
1328.6K(6.9)0.000000000018
1428.6K(7.2)0.000000000029
1528.6K(7.3)0.000000000031
1628.6K(7.7)0.000000000037
1728.6K(8.6)0.000000000153
1828.6K(9.0)0.000000000239
1928.6K(9.5)0.000000000644
2028.6K(10.5)0.000000004842
2128.6K(10.5)0.00000000483632.501314
2228.6K(10.7)0.000000007243
2328.6K(10.8)0.000000008834
2428.6K(11.0)0.000000013175
2528.6K(11.3)0.000000023334
2628.6K(11.9)0.000000072546
2728.6K(12.2)0.000000125946
2828.6K(15.1)0.000015564325
2928.6K(15.1)0.000015564330
3028.6K(16.5)0.000113988040
3128.6K(17.4)0.000365436732
3228.6K(18.4)0.001199886332
3328.6K(18.5)0.001343132522
3428.6K(21.6)0.025533838331
3528.6K(21.8)0.02976513974
3628.6K(23.3)0.081587105929
3728.6K(28.6)0.388474211727
3828.6K(29.1)0.383116032334
3928.6K(34.1)0.072361271668
4028.6K(36.2)0.015694932741
4128.6K(39.7)0.0004136342147
\n", "
" ], "text/plain": [ " Input_x kernel_x weigth Y Y=w0*x0+w1*x1+...+w41*x41\n", "0 28.6 K(2.0) 0.0000000000 11 \n", "1 28.6 K(2.2) 0.0000000000 9 \n", "2 28.6 K(2.2) 0.0000000000 14 \n", "3 28.6 K(2.5) 0.0000000000 22 \n", "4 28.6 K(3.4) 0.0000000000 17 \n", "5 28.6 K(3.6) 0.0000000000 15 \n", "6 28.6 K(4.0) 0.0000000000 16 \n", "7 28.6 K(4.8) 0.0000000000 19 \n", "8 28.6 K(5.0) 0.0000000000 32 \n", "9 28.6 K(5.4) 0.0000000000 27 \n", "10 28.6 K(5.6) 0.0000000000 23 \n", "11 28.6 K(5.7) 0.0000000000 11 \n", "12 28.6 K(6.2) 0.0000000000 29 \n", "13 28.6 K(6.9) 0.0000000000 18 \n", "14 28.6 K(7.2) 0.0000000000 29 \n", "15 28.6 K(7.3) 0.0000000000 31 \n", "16 28.6 K(7.7) 0.0000000000 37 \n", "17 28.6 K(8.6) 0.0000000001 53 \n", "18 28.6 K(9.0) 0.0000000002 39 \n", "19 28.6 K(9.5) 0.0000000006 44 \n", "20 28.6 K(10.5) 0.0000000048 42 \n", "21 28.6 K(10.5) 0.0000000048 36 32.501314\n", "22 28.6 K(10.7) 0.0000000072 43 \n", "23 28.6 K(10.8) 0.0000000088 34 \n", "24 28.6 K(11.0) 0.0000000131 75 \n", "25 28.6 K(11.3) 0.0000000233 34 \n", "26 28.6 K(11.9) 0.0000000725 46 \n", "27 28.6 K(12.2) 0.0000001259 46 \n", "28 28.6 K(15.1) 0.0000155643 25 \n", "29 28.6 K(15.1) 0.0000155643 30 \n", "30 28.6 K(16.5) 0.0001139880 40 \n", "31 28.6 K(17.4) 0.0003654367 32 \n", "32 28.6 K(18.4) 0.0011998863 32 \n", "33 28.6 K(18.5) 0.0013431325 22 \n", "34 28.6 K(21.6) 0.0255338383 31 \n", "35 28.6 K(21.8) 0.0297651397 4 \n", "36 28.6 K(23.3) 0.0815871059 29 \n", "37 28.6 K(28.6) 0.3884742117 27 \n", "38 28.6 K(29.1) 0.3831160323 34 \n", "39 28.6 K(34.1) 0.0723612716 68 \n", "40 28.6 K(36.2) 0.0156949327 41 \n", "41 28.6 K(39.7) 0.0004136342 147 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bw_manual = 3\n", "input_x = XXX[37]\n", "kernel_x = XXX\n", "weigths = KernelRegression.weights(bw_manual, input_x, XXX)\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': YYY,\n", " 'Y=w0*x0+w1*x1+...+w41*x41': ''\n", " }\n", "single_pt_KE = pd.DataFrame(data=data)\n", "single_pt_KE.at[len(YYY)//2, 'Y=w0*x0+w1*x1+...+w41*x41'] = sum([weigths[index]*YYY[index] for index, x in enumerate(kernel_x)])\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": [ 14.751401193274273, 15.219520375649841, 15.219520375649841, 15.878806426931906, 17.784868576841106, 18.18774341351661, 18.916196793808734, 21.07323582790648, 21.871331828442436, 22.937007874015748, 23.316386010362695, 23.688936627282494, 25.6672802078822, 28.78433802816901, 29.931684198358568, 30.295529026217228, 32.10645400593472, 38.3988432341246, 40.15605058591484, 42.03150654537387, 44.3720690838385, 44.3720690838385, 44.4729745437638, 44.47289086235102, 44.4707212568436, 44.46195176808697, 44.654411764705884, 44.488491048593346, 31.44035446489434, 31.44035446489434, 31.00720115218435, 30.8670792079208, 30.50924155513066, 30.375568551007152, 20.441628583298716, 20.6195872116552, 21.83943275388838, 30.450704225352116, 30.54929577464789, 58.88079470198677, 50.11920529801324, 147 ] }, { "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": [ 19.137984519548482, 19.40543709032291, 19.40543709032291, 19.829698200807226, 21.284115166145433, 21.64730546698855, 22.420316212358166, 24.159473244853405, 24.634808242260878, 25.63257510006903, 26.15394962830102, 26.419941622941327, 27.79788902849715, 29.829942361150017, 30.721064388796165, 31.01887668991366, 32.20615719154409, 34.769592374147884, 35.815269646075095, 37.0025966201741, 38.86763989397508, 38.86763989397508, 39.14648791770562, 39.27318088936524, 39.500505936629835, 39.77474712258941, 40.07439432455564, 40.096487557591715, 36.313125540313415, 36.313125540313415, 33.15130563928581, 31.23311794255793, 29.304839596033744, 29.12308253546981, 24.65597136249645, 24.48399388326629, 23.927970432737887, 32.50131410576155, 33.89644599813969, 58.19228341145836, 71.87937300351786, 106.71772581859165 ] }, { "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.622069242587393, 19.884760443572176, 19.884760443572176, 20.278797245049347, 21.46090764948087, 21.723598850465653, 22.248981252435218, 23.29974605637435, 23.562437257359132, 24.087819659328698, 24.350510860313477, 24.481856460805872, 25.138584463267826, 26.05800366671457, 26.45204046819174, 26.58338606868413, 27.108768470653697, 28.29087887508522, 28.816261277054785, 29.47298927951674, 30.786445284440653, 30.786445284440653, 31.049136485425436, 31.18048208591783, 31.44317328690261, 31.837210088379784, 32.62528369133413, 33.0193204928113, 36.82834290709066, 36.82834290709066, 38.66718131398413, 39.84929171841566, 41.162747723339564, 41.29409332383196, 45.36580693909609, 45.62849814008088, 47.59868214746675, 54.55999897356349, 55.21672697602544, 61.78400700064501, 64.54226461098523, 69.13936062821892 ] } ], "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(XXX.reshape(-1, 1), YYY.reshape(-1, 1))\n", "Y_pred_linear = reg.predict(XXX.reshape(-1, 1))\n", "\n", "# kernel regression\n", "Y_pred_gauss = KernelRegression.ker_reg(XXX, YYY, bw_manual, 'gauss')\n", "Y_pred_epanechnikov = KernelRegression.ker_reg(XXX, YYY, bw_manual, 'epanechnikov')\n", "\n", "fig = px.scatter(x=XXX,y=YYY)\n", "fig.add_trace(go.Scatter(x=XXX, y=np.array(Y_pred_gauss), name='Gauss', mode='lines'))\n", "fig.add_trace(go.Scatter(x=XXX, y=np.array(Y_pred_epanechnikov), name='Epanechnikov', mode='lines'))\n", "fig.add_trace(go.Scatter(x=XXX, 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.5" } }, "nbformat": 4, "nbformat_minor": 5 }