Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Run the codes below and give a brief discussion of the results: A) import pandas as pd import numpy as np from sklearn.cluster import AgglomerativeClustering

Run the codes below and give a brief discussion of the results:

A)

import pandas as pd import numpy as np from sklearn.cluster import AgglomerativeClustering import matplotlib.pyplot as plt

# Load dataset url = "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data" df = pd.read_csv(url, header=None)

# Extract features and labels X = df.iloc[:, 2:].values y = df.iloc[:, 1].values

# Apply hierarchical clustering with different linkage criteria fig, axs = plt.subplots(2, 2, figsize=(12, 12))

# Complete linkage agg_clustering = AgglomerativeClustering(n_clusters=2, linkage='complete') y_pred = agg_clustering.fit_predict(X) axs[0, 0].scatter(X[y_pred == 0, 0], X[y_pred == 0, 1], s=50, color='red', label='Cluster 1') axs[0, 0].scatter(X[y_pred == 1, 0], X[y_pred == 1, 1], s=50, color='blue', label='Cluster 2') axs[0, 0].set_title('Complete Linkage') axs[0, 0].legend()

# Single linkage agg_clustering = AgglomerativeClustering(n_clusters=2, linkage='single') y_pred = agg_clustering.fit_predict(X) axs[0, 1].scatter(X[y_pred == 0, 0], X[y_pred == 0, 1], s=50, color='red', label='Cluster 1') axs[0, 1].scatter(X[y_pred == 1, 0], X[y_pred == 1, 1], s=50, color='blue', label='Cluster 2') axs[0, 1].set_title('Single Linkage') axs[0, 1].legend()

# Average linkage agg_clustering = AgglomerativeClustering(n_clusters=2, linkage='average') y_pred = agg_clustering.fit_predict(X) axs[1, 0].scatter(X[y_pred == 0, 0], X[y_pred == 0, 1], s=50, color='red', label='Cluster 1') axs[1, 0].scatter(X[y_pred == 1, 0], X[y_pred == 1, 1], s=50, color='blue', label='Cluster 2') axs[1, 0].set_title('Average Linkage') axs[1, 0].legend()

# Ward linkage agg_clustering = AgglomerativeClustering(n_clusters=2, linkage='ward') y_pred = agg_clustering.fit_predict(X) axs[1, 1].scatter(X[y_pred == 0, 0], X[y_pred == 0, 1], s=50, color='red', label='Cluster 1') axs[1, 1].scatter(X[y_pred == 1, 0], X[y_pred == 1, 1], s=50, color='blue', label='Cluster 2') axs[1, 1].set_title('Ward Linkage') axs[1, 1].legend()

plt.show()

B)

import pandas as pd import numpy as np from sklearn.cluster import KMeans from sklearn.metrics import silhouette_score from sklearn.preprocessing import StandardScaler import matplotlib.pyplot as plt

# Load dataset url = "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data" df = pd.read_csv(url, header=None)

# Extract features and labels X = df.iloc[:, 2:].values y = df.iloc[:, 1].values

# Standardize the features scaler = StandardScaler() X_std = scaler.fit_transform(X)

# Parameter optimization using Silhouette score silhouette_scores = [] for n_clusters in range(2, 11): kmeans = KMeans(n_clusters=n_clusters, init='k-means++', random_state=42) cluster_labels = kmeans.fit_predict(X_std) silhouette_avg = silhouette_score(X_std, cluster_labels) silhouette_scores.append(silhouette_avg)

# Plot Silhouette score vs. number of clusters plt.plot(range(2, 11), silhouette_scores) plt.xlabel('Number of Clusters') plt.ylabel('Silhouette Score') plt.show()

# Apply k-Means clustering with optimal parameters kmeans = KMeans(n_clusters=2, init='k-means++', random_state=42) y_pred = kmeans.fit_predict(X_std)

# Evaluate performance accuracy = np.mean(y_pred == y) print('Accuracy:', accuracy)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions