Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
K = st.slider('Number of nearest neighbors (K)', min_value=1, max_value=10, value=5, step=1)
|
| 6 |
+
|
| 7 |
+
from sklearn.neighbors import KNeighborsClassifier as KNN
|
| 8 |
+
from sklearn.model_selection import cross_val_score
|
| 9 |
+
|
| 10 |
+
from sklearn.datasets import make_blobs
|
| 11 |
+
X, y = make_blobs(n_samples=1000, centers=3, n_features=2, cluster_std=6, random_state=42)
|
| 12 |
+
ntrain = 100
|
| 13 |
+
|
| 14 |
+
x_train = X[:ntrain]
|
| 15 |
+
y_train = y[:ntrain]
|
| 16 |
+
x_test = X[ntrain:]
|
| 17 |
+
y_test = y[train:]
|
| 18 |
+
|
| 19 |
+
knn = KNN(n_neighbors=K)
|
| 20 |
+
knn.fit(x_train, y_train)
|
| 21 |
+
plt.figure()
|
| 22 |
+
y_predicted = knn.predict(xy)
|
| 23 |
+
#plt.pcolormesh(y_predicted.reshape(200, 200), cmap='jet')
|
| 24 |
+
plt.pcolormesh(xx, yy, y_predicted.reshape(200, 200), cmap='jet', alpha=0.2)
|
| 25 |
+
for i in range(len(y_unique)):
|
| 26 |
+
plt.scatter(x_train[y_train == y_unique[i], 0],
|
| 27 |
+
x_train[y_train == y_unique[i], 1],
|
| 28 |
+
marker=markers[i],
|
| 29 |
+
c=colors[i])
|
| 30 |
+
|
| 31 |
+
st_col = st.columns(1)[0]
|
| 32 |
+
with st_col:
|
| 33 |
+
st.pyplot(plt)
|
| 34 |
+
|
| 35 |
+
hide_streamlit_style = """
|
| 36 |
+
<style>
|
| 37 |
+
#MainMenu {visibility: hidden;}
|
| 38 |
+
footer {visibility: hidden;}
|
| 39 |
+
subheader {alignment: center;}
|
| 40 |
+
</style>
|
| 41 |
+
"""
|
| 42 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|