Skip to content

Commit 38f92ca

Browse files
xhluluxhlulu
authored andcommitted
Placeholder Regression Section
1 parent 90ea1d4 commit 38f92ca

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

doc/python/ml-regression.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Regression
2+
3+
4+
### Visualizing kNN Regression
5+
6+
```python
7+
import numpy as np
8+
import plotly.express as px
9+
import plotly.graph_objects as go
10+
from sklearn.neighbors import KNeighborsRegressor
11+
12+
df = px.data.tips()
13+
X = df.total_bill.values.reshape(-1, 1)
14+
15+
knn_dist = KNeighborsRegressor(10, weights='distance')
16+
knn_uni = KNeighborsRegressor(10, weights='uniform')
17+
knn_dist.fit(X, df.tip)
18+
knn_uni.fit(X, df.tip)
19+
20+
x_range = np.linspace(X.min(), X.max(), 100)
21+
y_dist = knn_dist.predict(x_range.reshape(-1, 1))
22+
y_uni = knn_uni.predict(x_range.reshape(-1, 1))
23+
24+
fig = px.scatter(df, x='total_bill', y='tip', color='sex', opacity=0.65)
25+
fig.add_traces(go.Scatter(x=x_range, y=y_uni, name='Weights: Uniform'))
26+
fig.add_traces(go.Scatter(x=x_range, y=y_dist, name='Weights: Distance'))
27+
fig.show()
28+
```
29+
30+
### Reference
31+
32+
Learn more about `px` here:
33+
* https://plot.ly/python/plotly-express/
34+
35+
This tutorial was inspired by amazing examples from the official scikit-learn docs:
36+
* https://scikit-learn.org/stable/auto_examples/neighbors/plot_regression.html

0 commit comments

Comments
 (0)