Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q: Apply k - means and hierarchical clustering to the ORL face dataset. Set k = 2 in k - means and select 2 clusters
Q: Apply kmeans and hierarchical clustering to the ORL face dataset. Set k in kmeans and select clusters in hierarchical clustering. Do the clustering results match the two genders? Following is the code that is need to be corrected:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
from PIL import Image
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
#hierarchical clustering
from sklearn.cluster import AgglomerativeClustering
# load data
df pdDataFramenpzeros
count
for i in range:
for j in range:
image Image.openUsersORLFaces stri strjpng
imagearray nparrayimage
imagearray imagearray.reshape
dfiloccount imagearray
count
dfGender npones
dfiloc:
dfiloc:
dfiloc:
dfiloc:
printdf
# kmeans
X dfiloc::
scaler StandardScaler
X scaler.fittransformX
X pdDataFrameX
warnings.filterwarningsignore
kmeansmodel KMeansnclusters
kmeansmodel.fitX
Xclusters kmeansmodel.labels
truelabel sumXclusters dfGender
falselabel sumXclusters dfGender
printTrue Labels:
False Labels: formattruelabel, falselabel
truelab
falselab
for i in range:
kmeansmodel KMeansnclusters
kmeansmodel.fitX
truelabel sumkmeansmodel.labels dfGender
falselabel sumkmeansmodel.labels dfGender
truelab.appendtruelabel
falselab.appendfalselabel
pltscattertruelab, falselab
pltxlabelTrue Labels"
pltylabelFalse Labels"
#hierarchical clustering
X dfiloc::
scaler StandardScaler
X scaler.fittransformX
X pdDataFrameX
hierarchicalcluster AgglomerativeClusteringnclusters affinity'euclidean', linkage'ward'
hierarchicalcluster.fitX
Xclusters hierarchicalcluster.labels
truelabel sumXclusters dfGender
falselabel sumXclusters dfGender
print# of True Labeling:
# of False Labeling: formattruelabel, falselabel
truelab
falselab
for i in range:
kmeansmodel KMeansnclusters
kmeansmodel.fitX
truelabel sumkmeansmodel.labels dfGender
falselabel sumkmeansmodel.labels dfGender
truelab.appendtruelabel
falselab.appendfalselabel
pltscattertruelab, falselab
pltxlabelTrue Labels"
pltylabelFalse Labels"
Could you please give me the complete Python code to solve the problemnot just the steps, but the code can get the result directly Thank you!:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started