Skip to content

Instantly share code, notes, and snippets.

@thuwarakeshm
Last active July 27, 2021 13:23
Show Gist options
  • Save thuwarakeshm/4573c6e9da997687826c9355c7579482 to your computer and use it in GitHub Desktop.
Save thuwarakeshm/4573c6e9da997687826c9355c7579482 to your computer and use it in GitHub Desktop.
Introduction to Typer
@app.command()
def elbow(file_path: str, max_clusters: int = 10):
"""
This command will run K-Means algorithm many times and print the inertia values to the console.
"""
...
@app.command()
def elbow(file_path: str, max_clusters: int = 10):
errors = [] # Create an empty list to collect inertias
df = pd.read_csv(file_path)
# Loop through some desirable number of clusters
# The KMeans algorithm's fit method returns a property called inertia_ that has the information we need
with typer.progressbar(range(2, max_clusters)) as progress:
for k in progress:
time.sleep(1)
errors.append(
{
"inertia": KMeans(n_clusters=k, random_state=0)
.fit(df[["Age", "Income", "Debt"]])
.inertia_,
"num_clusters": k,
}
)
# for convenience convert the list to a pandas dataframe
df_inertia = pd.DataFrame(errors)
typer.echo(df_inertia)
@app.command()
def elbow(file_path: str, max_clusters:int=10):
errors = [] # Create an empty list to collect inertias
df = pd.read_csv(file_path)
# Loop through some desirable number of clusters
# The KMeans algorithm's fit method returns a property called inertia_ that has the information we need
for k in range(2, max_clusters):
time.sleep(1)
errors.append(
{
"inertia": KMeans(n_clusters=k, random_state=0)
.fit(df[["Age", "Income", "Debt"]])
.inertia_,
"num_clusters": k,
}
)
# for convenience convert the list to a pandas dataframe
df_inertia = pd.DataFrame(errors)
typer.echo(df_inertia)
import typer
def say_hello():
typer.secho(f"Hello World!", fg=typer.colors.WHITE, bg=typer.colors.BLUE)
if __name__ == "__main__":
typer.run(say_hello)
import typer
import pandas as pd # Pandas for reading data
from sklearn.cluster import KMeans # KMeans Clustering itself
from joblib import dump, load # Required to persist the trained model
app = typer.Typer()
@app.command()
def train(file_path: str):
typer.secho(f"Training K-Means with file {file_path}")
# Code for training K-Means clustering
df = pd.read_csv(file_path)
kmeans = KMeans(n_clusters=2, random_state=0).fit(df[["Age", "Income"]])
typer.secho("CLUSTER CENTERS")
typer.echo(kmeans.cluster_centers_)
dump(
kmeans, "model.joblib"
) # This line is to store the model to use in predictions later.
@app.command()
def predict():
typer.secho("Will be implemented soon")
pass
if __name__ == "__main__":
app()
import typer
app = typer.Typer()
@app.command()
def say_hello():
typer.secho(f"Hello World!", fg=typer.colors.WHITE, bg=typer.colors.BLUE)
@app.command()
def say_hello_in_red():
typer.secho(f"Hello World!", fg=typer.colors.WHITE, bg=typer.colors.RED)
if __name__ == "__main__":
app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment