Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code 1 : import numpy as np import pandas as pd from PIL import Image from sklearn.preprocessing import StandardScaler # Initialize dataframe df = pd

Code1:
import numpy as np
import pandas as pd
from PIL import Image
from sklearn.preprocessing import StandardScaler
# Initialize dataframe
df = pd.DataFrame(np.zeros((400,2576)))
count =0
for i in range(40):
for j in range(10):
image = Image.open("/Users/ORL_Faces/"+ str(i+1)+"_"+ str(j+1)+".png")
image_array = np.array(image)
image_array = image_array.reshape(1,-1)
df.iloc[count]= image_array
count +=1
# Add gender labels to the dataframe
df["Gender"]= np.ones((400,1))
# Modify certain entries to represent actual gender labels
df.iloc[0:10,2576]=0.0
df.iloc[70:80,2576]=0.0
df.iloc[90:100,2576]=0.0
df.iloc[310:320,2576]=0.0
from sklearn.cluster import KMeans
# Standardize image data
X = df.iloc[:,0:2576]
scaler = StandardScaler()
X = scaler.fit_transform(X)
# Apply KMeans clustering
kmeans_model = KMeans(n_clusters=2)
kmeans_model.fit(X)
# Add cluster labels to the dataframe
df["KMeans_Clusters"]= kmeans_model.labels_
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
import pandas as pd
# Assuming df is your dataframe and it has been loaded correctly
X = df.iloc[:,0:2576] # Adjust the slice as necessary to include the right columns
# Standardize image data for hierarchical clustering
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Apply Hierarchical clustering using Ward's method
hierarchical_cluster = AgglomerativeClustering(n_clusters=2, linkage='ward')
hierarchical_cluster.fit(X_scaled)
# Add cluster labels to the dataframe
df["Hierarchical_Clusters"]= hierarchical_cluster.labels_
# Calculate true and false labelings for K-Means clustering
true_label_kmeans = sum(df["KMeans_Clusters"]== df["Gender"])
false_label_kmeans = sum(df["KMeans_Clusters"]!= df["Gender"])
# Calculate true and false labelings for Hierarchical clustering
true_label_hierarchical = sum(df["Hierarchical_Clusters"]== df["Gender"])
false_label_hierarchical = sum(df["Hierarchical_Clusters"]!= df["Gender"])
import matplotlib.pyplot as plt
plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.scatter(true_label_kmeans, false_label_kmeans, color='blue')
plt.title('K-Means Clustering')
plt.xlabel('True Labels')
plt.ylabel('False Labels')
plt.subplot(1,2,2)
plt.scatter(true_label_hierarchical, false_label_hierarchical, color='red')
plt.title('Hierarchical Clustering')
plt.xlabel('True Labels')
plt.ylabel('False Labels')
plt.tight_layout()
plt.show()
print("K-Means Clustering Results:")
print("# of True Labeling:", true_label_kmeans)
print("# of False Labeling:", false_label_kmeans)
print("
Hierarchical Clustering Results:")
print("# of True Labeling:", true_label_hierarchical)
print("# of False Labeling:", false_label_hierarchical)
Code2:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces, load_digits
from sklearn.cluster import KMeans, AgglomerativeClustering
from sklearn.metrics import silhouette_score
try:
# Try to fetch the Olivetti faces dataset
data = fetch_olivetti_faces()
X = data.data
y = data.target
dataset_name = "Olivetti Faces"
except Exception as e:
print("Failed to fetch Olivetti Faces dataset:", e)
# Fallback to a different dataset, e.g., load_digits
data = load_digits()
X = data.data
y = data.target
dataset_name = "Digits"
# Apply k-means clustering
kmeans = KMeans(n_clusters=2, random_state=42)
kmeans_labels = kmeans.fit_predict(X)
# Apply hierarchical clustering
hierarchical = AgglomerativeClustering(n_clusters=2)
hierarchical_labels = hierarchical.fit_predict(X)
# Evaluate clustering performance using silhouette score
kmeans_score = silhouette_score(X, kmeans_labels)
hierarchical_score = silhouette_score(X, hierarchical_labels)
# Print the silhouette scores
print(f"Silhouette Score for K-Means on {dataset_name}:", kmeans_score)
print(f"Silhouette Score for Hierarchical Clustering on {dataset_name}:", hierarchical_score)
# Visualize the clustering results
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.scatter(X[:,0], X[:,1], c=kmeans_labels, cmap='viridis', s=10)
plt.title(f"K-Means Clustering on {dataset_name}")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.subplot(1,2,2)
plt.scatter(X[:,0], X[:,1], c=hierarchical_labels, cmap='viridis', s=10)
plt.title(f"Hierarchical Clustering on {dataset_name}")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.tight_layout()
plt.show()
Please use the dataset of Code1(the link) for Code2, combine the 2 codes together, and give me the complete code. Thank you!:)

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

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions