Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created September 10, 2021 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/e86d6c76fe6be90b09313c523578b279 to your computer and use it in GitHub Desktop.
Save amankharwal/e86d6c76fe6be90b09313c523578b279 to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.cluster import MiniBatchKMeans
data = pd.read_csv("https://raw.githubusercontent.com/ageron/handson-ml/master/datasets/housing/housing.csv")
data = data.loc[:, ["median_income", "latitude", "longitude"]]
kmeans = MiniBatchKMeans(n_clusters=6, random_state=0, batch_size=6)
data["Cluster"] = kmeans.fit_predict(data)
data["Cluster"] = data["Cluster"].astype("int")
print(data.head())
plt.style.use('seaborn-whitegrid')
plt.rc("figure", autolayout=True)
plt.rc("axes", labelweight='bold', labelsize='large', titleweight='bold', titlesize=14, titlepad=10)
sns.relplot(x='longitude', y='latitude', hue='Cluster', data=data, height=6)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment