0% found this document useful (0 votes)
5 views2 pages

Program 9

The document contains a Python script that performs K-Means clustering on the Wisconsin Breast Cancer dataset. It standardizes the features, applies K-Means with two clusters, and uses PCA for dimensionality reduction to visualize the results. A scatter plot is generated to display the clustering outcomes in a two-dimensional space.

Uploaded by

prathibhard3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Program 9

The document contains a Python script that performs K-Means clustering on the Wisconsin Breast Cancer dataset. It standardizes the features, applies K-Means with two clusters, and uses PCA for dimensionality reduction to visualize the results. A scatter plot is generated to display the clustering outcomes in a two-dimensional space.

Uploaded by

prathibhard3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

5/21/25, 8:36 AM Untitled3.

ipynb - Colab

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

# 1. Load the Wisconsin Breast Cancer dataset


data = load_breast_cancer()
X = data.data
y = data.target
feature_names = data.feature_names
target_names = data.target_names

# 2. Standardize the features


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 3. Apply K-Means clustering


kmeans = KMeans(n_clusters=2, random_state=42)
kmeans.fit(X_scaled)
cluster_labels = kmeans.labels_

# 4. Reduce dimensions for visualization (PCA 2D)


pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# 5. Plot clustering results


plt.figure(figsize=(10, 6))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=cluster_labels, cmap='viridis', edgecolor='k', s=100)
plt.title("K-Means Clustering on Wisconsin Breast Cancer Dataset (PCA Projection)")
plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")
plt.grid(True)
plt.colorbar(label='Cluster Label')
plt.show()

https://colab.research.google.com/drive/11aop6X8rTQXtPe51CPHghl8h9p9KWWKE#printMode=true 1/2
5/21/25, 8:36 AM Untitled3.ipynb - Colab

https://colab.research.google.com/drive/11aop6X8rTQXtPe51CPHghl8h9p9KWWKE#printMode=true 2/2

You might also like