{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import plotly.express as px\n",
"from statsmodels.nonparametric.kernel_regression import KernelReg\n",
"import plotly.graph_objs as go\n",
"import pandas as pd \n",
"import KernelRegression"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(1)# xwidth controls the range of x values.\n",
"xwidth = 20\n",
"x = np.arange(0,xwidth,1)# we want to add some noise to the x values so that dont sit at regular intervals\n",
"x_residuals = np.random.normal(scale=0.2, size=[x.shape[0]])# new_x is the range of x values we will be using all the way through\n",
"new_x = x + x_residuals# We generate residuals for y values since we want to show some variation in the data\n",
"num_points = x.shape[0]\n",
"residuals = np.random.normal(scale=2.0, size=[num_points])# We will be using fun_y to generate y values all the way through\n",
"fun_y = lambda x: -(x*x) + residuals"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.32486907, 0.87764872, 1.89436565, 2.78540628, 4.17308153,\n",
" 4.53969226, 6.34896235, 6.84775862, 8.06380782, 8.95012592,\n",
" 10.29242159, 10.58797186, 11.93551656, 12.92318913, 14.22675389,\n",
" 14.78002175, 15.96551436, 16.82442832, 18.00844275, 19.11656304])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_x"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(1)# xwidth controls the range of x values.\n",
"xwidth = 20\n",
"x = np.arange(0,xwidth,1)# we want to add some noise to the x values so that dont sit at regular intervals\n",
"x_residuals = np.random.normal(scale=0.2, size=[x.shape[0]])# new_x is the range of x values we will be using all the way through\n",
"new_x = x + x_residuals# We generate residuals for y values since we want to show some variation in the data\n",
"num_points = x.shape[0]\n",
"residuals = np.random.normal(scale=2.0, size=[num_points])# We will be using fun_y to generate y values all the way through\n",
"fun_y = lambda x: -(x*x) + residuals"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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}
\n", " | Input_x | \n", "kernel_x | \n", "gaussian_const | \n", "gaussian_exp | \n", "full_gaussian_value | \n", "bw | \n", "
---|---|---|---|---|---|---|
0 | \n", "0.324869 | \n", "-20.0 | \n", "0.199471 | \n", "-51.637538 | \n", "7.481390e-24 | \n", "2 | \n", "
1 | \n", "0.324869 | \n", "-19.9 | \n", "0.199471 | \n", "-51.130666 | \n", "1.241978e-23 | \n", "2 | \n", "
2 | \n", "0.324869 | \n", "-19.8 | \n", "0.199471 | \n", "-50.626294 | \n", "2.056647e-23 | \n", "2 | \n", "
3 | \n", "0.324869 | \n", "-19.7 | \n", "0.199471 | \n", "-50.124423 | \n", "3.397190e-23 | \n", "2 | \n", "
4 | \n", "0.324869 | \n", "-19.6 | \n", "0.199471 | \n", "-49.625051 | \n", "5.597502e-23 | \n", "2 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
395 | \n", "0.324869 | \n", "19.5 | \n", "0.199471 | \n", "-45.960706 | \n", "2.184737e-21 | \n", "2 | \n", "
396 | \n", "0.324869 | \n", "19.6 | \n", "0.199471 | \n", "-46.441334 | \n", "1.351030e-21 | \n", "2 | \n", "
397 | \n", "0.324869 | \n", "19.7 | \n", "0.199471 | \n", "-46.924462 | \n", "8.333837e-22 | \n", "2 | \n", "
398 | \n", "0.324869 | \n", "19.8 | \n", "0.199471 | \n", "-47.410091 | \n", "5.127898e-22 | \n", "2 | \n", "
399 | \n", "0.324869 | \n", "19.9 | \n", "0.199471 | \n", "-47.898219 | \n", "3.147371e-22 | \n", "2 | \n", "
400 rows × 6 columns
\n", "\n", " | Input_x | \n", "kernel_x | \n", "gaussian_const | \n", "gaussian_exp | \n", "full_gaussian_value | \n", "bw | \n", "
---|---|---|---|---|---|---|
0 | \n", "39.7 | \n", "-21 | \n", "0.398942 | \n", "-264.5 | \n", "5.370560e-116 | \n", "1 | \n", "
1 | \n", "39.7 | \n", "-20 | \n", "0.398942 | \n", "-242.0 | \n", "3.174282e-106 | \n", "1 | \n", "
2 | \n", "39.7 | \n", "-19 | \n", "0.398942 | \n", "-220.5 | \n", "6.902029e-97 | \n", "1 | \n", "
3 | \n", "39.7 | \n", "-18 | \n", "0.398942 | \n", "-200.0 | \n", "5.520948e-88 | \n", "1 | \n", "
4 | \n", "39.7 | \n", "-17 | \n", "0.398942 | \n", "-180.5 | \n", "1.624636e-79 | \n", "1 | \n", "
5 | \n", "39.7 | \n", "-16 | \n", "0.398942 | \n", "-162.0 | \n", "1.758750e-71 | \n", "1 | \n", "
6 | \n", "39.7 | \n", "-15 | \n", "0.398942 | \n", "-144.5 | \n", "7.004182e-64 | \n", "1 | \n", "
7 | \n", "39.7 | \n", "-14 | \n", "0.398942 | \n", "-128.0 | \n", "1.026163e-56 | \n", "1 | \n", "
8 | \n", "39.7 | \n", "-13 | \n", "0.398942 | \n", "-112.5 | \n", "5.530710e-50 | \n", "1 | \n", "
9 | \n", "39.7 | \n", "-12 | \n", "0.398942 | \n", "-98.0 | \n", "1.096607e-43 | \n", "1 | \n", "
10 | \n", "39.7 | \n", "-11 | \n", "0.398942 | \n", "-84.5 | \n", "7.998828e-38 | \n", "1 | \n", "
11 | \n", "39.7 | \n", "-10 | \n", "0.398942 | \n", "-72.0 | \n", "2.146384e-32 | \n", "1 | \n", "
12 | \n", "39.7 | \n", "-9 | \n", "0.398942 | \n", "-60.5 | \n", "2.118819e-27 | \n", "1 | \n", "
13 | \n", "39.7 | \n", "-8 | \n", "0.398942 | \n", "-50.0 | \n", "7.694599e-23 | \n", "1 | \n", "
14 | \n", "39.7 | \n", "-7 | \n", "0.398942 | \n", "-40.5 | \n", "1.027977e-18 | \n", "1 | \n", "
15 | \n", "39.7 | \n", "-6 | \n", "0.398942 | \n", "-32.0 | \n", "5.052271e-15 | \n", "1 | \n", "
16 | \n", "39.7 | \n", "-5 | \n", "0.398942 | \n", "-24.5 | \n", "9.134720e-12 | \n", "1 | \n", "
17 | \n", "39.7 | \n", "-4 | \n", "0.398942 | \n", "-18.0 | \n", "6.075883e-09 | \n", "1 | \n", "
18 | \n", "39.7 | \n", "-3 | \n", "0.398942 | \n", "-12.5 | \n", "1.486720e-06 | \n", "1 | \n", "
19 | \n", "39.7 | \n", "-2 | \n", "0.398942 | \n", "-8.0 | \n", "1.338302e-04 | \n", "1 | \n", "
20 | \n", "39.7 | \n", "-1 | \n", "0.398942 | \n", "-4.5 | \n", "4.431848e-03 | \n", "1 | \n", "
21 | \n", "39.7 | \n", "0 | \n", "0.398942 | \n", "-2.0 | \n", "5.399097e-02 | \n", "1 | \n", "
22 | \n", "39.7 | \n", "1 | \n", "0.398942 | \n", "-0.5 | \n", "2.419707e-01 | \n", "1 | \n", "
23 | \n", "39.7 | \n", "2 | \n", "0.398942 | \n", "-0.0 | \n", "3.989423e-01 | \n", "1 | \n", "
24 | \n", "39.7 | \n", "3 | \n", "0.398942 | \n", "-0.5 | \n", "2.419707e-01 | \n", "1 | \n", "
25 | \n", "39.7 | \n", "4 | \n", "0.398942 | \n", "-2.0 | \n", "5.399097e-02 | \n", "1 | \n", "
26 | \n", "39.7 | \n", "5 | \n", "0.398942 | \n", "-4.5 | \n", "4.431848e-03 | \n", "1 | \n", "
27 | \n", "39.7 | \n", "6 | \n", "0.398942 | \n", "-8.0 | \n", "1.338302e-04 | \n", "1 | \n", "
28 | \n", "39.7 | \n", "7 | \n", "0.398942 | \n", "-12.5 | \n", "1.486720e-06 | \n", "1 | \n", "
29 | \n", "39.7 | \n", "8 | \n", "0.398942 | \n", "-18.0 | \n", "6.075883e-09 | \n", "1 | \n", "
30 | \n", "39.7 | \n", "9 | \n", "0.398942 | \n", "-24.5 | \n", "9.134720e-12 | \n", "1 | \n", "
31 | \n", "39.7 | \n", "10 | \n", "0.398942 | \n", "-32.0 | \n", "5.052271e-15 | \n", "1 | \n", "
32 | \n", "39.7 | \n", "11 | \n", "0.398942 | \n", "-40.5 | \n", "1.027977e-18 | \n", "1 | \n", "
33 | \n", "39.7 | \n", "12 | \n", "0.398942 | \n", "-50.0 | \n", "7.694599e-23 | \n", "1 | \n", "
34 | \n", "39.7 | \n", "13 | \n", "0.398942 | \n", "-60.5 | \n", "2.118819e-27 | \n", "1 | \n", "
35 | \n", "39.7 | \n", "14 | \n", "0.398942 | \n", "-72.0 | \n", "2.146384e-32 | \n", "1 | \n", "
36 | \n", "39.7 | \n", "15 | \n", "0.398942 | \n", "-84.5 | \n", "7.998828e-38 | \n", "1 | \n", "
37 | \n", "39.7 | \n", "16 | \n", "0.398942 | \n", "-98.0 | \n", "1.096607e-43 | \n", "1 | \n", "
38 | \n", "39.7 | \n", "17 | \n", "0.398942 | \n", "-112.5 | \n", "5.530710e-50 | \n", "1 | \n", "
39 | \n", "39.7 | \n", "18 | \n", "0.398942 | \n", "-128.0 | \n", "1.026163e-56 | \n", "1 | \n", "
40 | \n", "39.7 | \n", "19 | \n", "0.398942 | \n", "-144.5 | \n", "7.004182e-64 | \n", "1 | \n", "
41 | \n", "39.7 | \n", "20 | \n", "0.398942 | \n", "-162.0 | \n", "1.758750e-71 | \n", "1 | \n", "