Skip to content

Instantly share code, notes, and snippets.

@thuwarakeshm
Last active May 10, 2022 17:26
Show Gist options
  • Save thuwarakeshm/977281a4892869688be92d3a488aba1d to your computer and use it in GitHub Desktop.
Save thuwarakeshm/977281a4892869688be92d3a488aba1d to your computer and use it in GitHub Desktop.
pyscript
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PyScript Test</title>
<!-- Import PyScript -->
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<!-- Import Python modules -->
<py-env>
- pandas
- numpy
- matplotlib
</py-env>
</head>
<body>
<!-- All your other HTML -->
<!-- Python Script Here -->
<py-script>print("Hello World")</py-script>
<!-- Create a Python REPL -->
<py-repl></py-repl>
</body>
</html>
<html>
<head>
<title>K-Means</title>
<meta charset="utf-8">
<!-- Import PyScript -->
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- pandas
- scikit-learn
</py-env>
</head>
<body>
<div id="centroids" style="width: 100%; height: 100%"></div>
<py-script src="./script.py" output="centroids">
</py-script>
</body>
</html>
import pandas as pd
from pyodide.http import open_url
from sklearn.cluster import KMeans
# Read dataset from file using pandas
df = pd.read_csv(
open_url(
"https://raw.githubusercontent.com/thuwarakeshm/PracticalML-KMeans-Election/master/voters_demo_sample.csv"
)
)
# perform K-Means clustering to find 2 clusters considering only age and income of voters
kmeans = KMeans(n_clusters=2, random_state=0).fit(df[["Age", "Income"]])
print("Cluster Centroids")
for cluster in kmeans.cluster_centers_:
print(f"Cluster I : Age {cluster[0]} and Income {cluster[1]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment