K-Means Clustering Using Boston Housing Dataset
Introduction
K-Means Clustering is an unsupervised machine-learning
algorithm that divides data into a predefined number of
groups called clusters.
In this example, the Boston Housing Dataset is used to
demonstrate K-Means clustering. The 13 numerical housing
features are standardized before applying the algorithm.
Important: MEDV is not used for clustering
because it is the target variable in the traditional
Boston Housing dataset.
1
Load Dataset
Load the Boston Housing CSV file using Pandas.
2
Select Features
Select the 13 housing-related numerical features.
3
Standardize Data
StandardScaler converts the features to a comparable
scale before calculating distances.
4
Find Suitable K
Use the Elbow Method to examine different numbers
of clusters.
5
Apply K-Means
Create the K-Means model and fit it to the standardized
data.
6
Assign Clusters
Each housing record receives a cluster label.
7
Analyze Clusters
Calculate the average feature values for every cluster.
8
Visualize
PCA is used to display the high-dimensional clusters
in two dimensions.
Python Program
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
# -----------------------------------------
# 1. LOAD DATASET
# -----------------------------------------
df = pd.read_csv("boston-housing.csv")
print("Dataset Shape:")
print(df.shape)
# -----------------------------------------
# 2. SELECT FEATURES
# -----------------------------------------
features = [
"CRIM",
"ZN",
"INDUS",
"CHAS",
"NOX",
"RM",
"AGE",
"DIS",
"RAD",
"TAX",
"PTRATIO",
"B",
"LSTAT"
]
X = df[features]
# -----------------------------------------
# 3. STANDARDIZE FEATURES
# -----------------------------------------
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# -----------------------------------------
# 4. ELBOW METHOD
# -----------------------------------------
inertia = []
for k in range(2, 11):
model = KMeans(
n_clusters=k,
random_state=42,
n_init=10
)
model.fit(X_scaled)
inertia.append(model.inertia_)
plt.figure(figsize=(8, 5))
plt.plot(
range(2, 11),
inertia,
marker="o"
)
plt.xlabel("Number of Clusters (K)")
plt.ylabel("Inertia")
plt.title("Elbow Method")
plt.grid(True)
plt.show()
# -----------------------------------------
# 5. CREATE K-MEANS MODEL
# -----------------------------------------
kmeans = KMeans(
n_clusters=3,
random_state=42,
n_init=10
)
# -----------------------------------------
# 6. CREATE CLUSTERS
# -----------------------------------------
df["Cluster"] = kmeans.fit_predict(X_scaled)
# -----------------------------------------
# 7. CLUSTER COUNT
# -----------------------------------------
print("\nCluster Counts:")
print(
df["Cluster"]
.value_counts()
.sort_index()
)
# -----------------------------------------
# 8. CLUSTER SUMMARY
# -----------------------------------------
summary = df.groupby("Cluster")[features].mean()
print("\nCluster Summary:")
print(summary)
# -----------------------------------------
# 9. PCA
# -----------------------------------------
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
# -----------------------------------------
# 10. VISUALIZE CLUSTERS
# -----------------------------------------
plt.figure(figsize=(9, 6))
plt.scatter(
X_pca[:, 0],
X_pca[:, 1],
c=df["Cluster"],
cmap="viridis",
s=45,
alpha=0.8
)
plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")
plt.title(
"Boston Housing K-Means Clusters"
)
plt.colorbar(
label="Cluster"
)
plt.grid(True)
plt.show()
# -----------------------------------------
# 11. SAVE RESULT
# -----------------------------------------
df.to_csv(
"boston_housing_kmeans_clusters.csv",
index=False
)
print("\nClustered dataset saved successfully.")
Sample Output
Dataset Shape:
(506, 14)
Cluster Counts:
Cluster
0 151
1 182
2 173
Name: count, dtype: int64
Cluster Summary:
CRIM ZN INDUS CHAS NOX RM
Cluster
0 0.2451 11.24 6.21 0.06 0.47 6.35
1 3.4827 3.84 14.91 0.08 0.62 6.01
2 0.7215 8.62 9.73 0.07 0.54 6.72
AGE DIS RAD TAX PTRATIO
Cluster
0 52.41 4.71 4.82 282.41 17.82
1 78.35 2.94 9.21 449.73 18.91
2 63.27 3.87 5.74 329.18 17.24
B LSTAT
Cluster
0 385.24 8.12
1 355.71 16.84
2 389.63 7.91
Final Output:
Clustered dataset saved successfully.
How to Understand the Output
Cluster Counts shows how many housing
records were assigned to each cluster.
Cluster Summary displays the mean value of
each selected feature within each cluster. This allows you
to compare the characteristics of the groups.
PCA Visualization reduces the 13-dimensional
feature space to two dimensions so that the cluster
assignments can be displayed on a scatter plot.
The cluster labels such as 0, 1 and 2 are simply identifiers
assigned by the algorithm. They do not inherently indicate
a ranking or quality level.
No comments:
Post a Comment