Spaces:
Runtime error
Runtime error
ankitajain
commited on
Commit
•
fbe9715
1
Parent(s):
6c10112
commit
Browse files
app.py
CHANGED
@@ -9,21 +9,21 @@ st.subheader("K nearest neighbor (KNN) Regressor")
|
|
9 |
|
10 |
st_col = st.columns(1)[0]
|
11 |
|
12 |
-
K = st.slider(
|
|
|
|
|
13 |
|
14 |
|
|
|
15 |
|
16 |
-
|
17 |
-
X, y = make_regression(n_samples=100, n_features=1, noise=0.1,random_state=42)
|
18 |
-
|
19 |
-
ntrain = 100
|
20 |
|
21 |
x_train = X[:ntrain]
|
22 |
y_train = y[:ntrain]
|
23 |
x_test = X[ntrain:]
|
24 |
y_test = y[ntrain:]
|
25 |
|
26 |
-
knn =
|
27 |
knn.fit(x_train, y_train)
|
28 |
plt.figure()
|
29 |
|
@@ -33,21 +33,23 @@ xx, yy = np.meshgrid(x, y)
|
|
33 |
xy = np.c_[xx.ravel(), yy.ravel()]
|
34 |
|
35 |
y_predicted = knn.predict(xy)
|
36 |
-
#plt.pcolormesh(y_predicted.reshape(200, 200), cmap='jet')
|
37 |
-
plt.pcolormesh(xx, yy, y_predicted.reshape(200, 200), cmap=
|
38 |
y_unique = np.unique(y_train)
|
39 |
-
markers =
|
40 |
-
colors =
|
41 |
for i in range(len(y_unique)):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
|
48 |
with st_col:
|
49 |
-
|
50 |
-
|
51 |
hide_streamlit_style = """
|
52 |
<style>
|
53 |
#MainMenu {visibility: hidden;}
|
@@ -55,10 +57,12 @@ hide_streamlit_style = """
|
|
55 |
subheader {alignment: center;}
|
56 |
</style>
|
57 |
"""
|
58 |
-
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
59 |
|
60 |
-
st.markdown(
|
|
|
61 |
There are several points to note on the effect of K on the quality of model fit:
|
62 |
* Models with extremely small values of K learn the local patterns and do not generalize well thus they have a high variance or overfitting effect.
|
63 |
* Models with extremely high values of K suffer from averaging effect over the entire space and thus do not do well even on the train points. This is known as a high bias or underfitting effect.
|
64 |
-
"""
|
|
|
|
9 |
|
10 |
st_col = st.columns(1)[0]
|
11 |
|
12 |
+
K = st.slider(
|
13 |
+
"Number of nearest neighbors (K)", min_value=1, max_value=10, value=5, step=1
|
14 |
+
)
|
15 |
|
16 |
|
17 |
+
X, y = make_regression(n_samples=1000, n_features=1, noise=0.1, random_state=42)
|
18 |
|
19 |
+
ntrain = 700
|
|
|
|
|
|
|
20 |
|
21 |
x_train = X[:ntrain]
|
22 |
y_train = y[:ntrain]
|
23 |
x_test = X[ntrain:]
|
24 |
y_test = y[ntrain:]
|
25 |
|
26 |
+
knn = KNeighborsRegressor(n_neighbors=K)
|
27 |
knn.fit(x_train, y_train)
|
28 |
plt.figure()
|
29 |
|
|
|
33 |
xy = np.c_[xx.ravel(), yy.ravel()]
|
34 |
|
35 |
y_predicted = knn.predict(xy)
|
36 |
+
# plt.pcolormesh(y_predicted.reshape(200, 200), cmap='jet')
|
37 |
+
plt.pcolormesh(xx, yy, y_predicted.reshape(200, 200), cmap="jet", alpha=0.2)
|
38 |
y_unique = np.unique(y_train)
|
39 |
+
markers = "*x+"
|
40 |
+
colors = "bgr"
|
41 |
for i in range(len(y_unique)):
|
42 |
+
plt.scatter(
|
43 |
+
x_train[y_train == y_unique[i], 0],
|
44 |
+
x_train[y_train == y_unique[i], 1],
|
45 |
+
marker=markers[i],
|
46 |
+
c=colors[i],
|
47 |
+
)
|
48 |
+
|
49 |
|
50 |
with st_col:
|
51 |
+
st.pyplot(plt)
|
52 |
+
|
53 |
hide_streamlit_style = """
|
54 |
<style>
|
55 |
#MainMenu {visibility: hidden;}
|
|
|
57 |
subheader {alignment: center;}
|
58 |
</style>
|
59 |
"""
|
60 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
61 |
|
62 |
+
st.markdown(
|
63 |
+
"""
|
64 |
There are several points to note on the effect of K on the quality of model fit:
|
65 |
* Models with extremely small values of K learn the local patterns and do not generalize well thus they have a high variance or overfitting effect.
|
66 |
* Models with extremely high values of K suffer from averaging effect over the entire space and thus do not do well even on the train points. This is known as a high bias or underfitting effect.
|
67 |
+
"""
|
68 |
+
)
|