8. Demonstrate and analyse the results of classification based on KNN Algorithm.
import os
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
import seaborn as sns
# Set the environment variable to avoid the memory leak warning
os.environ['OMP_NUM_THREADS'] = '1'
# Sample dataset
data = {
'Feature1': [1.0, 1.1, 0.9, 5.0, 5.1, 4.9, 9.0, 9.1, 8.9],
'Feature2': [2.0, 2.1, 1.9, 6.0, 6.1, 5.9, 10.0, 10.1, 9.9]
}
# Create DataFrame
df = pd.DataFrame(data)
# Convert data to numpy array
X = df.values
# K-means Clustering
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10) # Set n_init explicitly to suppress the warning
kmeans_labels = kmeans.fit_predict(X)
# EM Algorithm (Gaussian Mixture Model)
gmm = GaussianMixture(n_components=3, random_state=42) # Assuming 3 clusters
gmm_labels = gmm.fit_predict(X)
# Calculate silhouette scores
kmeans_silhouette = silhouette_score(X, kmeans_labels)
gmm_silhouette = silhouette_score(X, gmm_labels)
print(f"K-means Silhouette Score: {kmeans_silhouette}")
print(f"EM Silhouette Score: {gmm_silhouette}")
# Visualize the clustering results
plt.figure(figsize=(14, 6))
# K-means clustering result
plt.subplot(1, 2, 1)
sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=kmeans_labels, palette='viridis')
plt.title('K-means Clustering')
# EM clustering result
plt.subplot(1, 2, 2)
sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=gmm_labels, palette='viridis')
plt.title('EM Clustering (Gaussian Mixture Model)')
plt.show()
OUTPUT:
Accuracy: 100.00%
Correct Predictions:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]),)
Wrong Predictions:
(array([], dtype=int64),)
Confusion Matrix:
[[19 0 0]
[ 0 13 0]
[ 0 0 13]]
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 19
versicolor 1.00 1.00 1.00 13
virginica 1.00 1.00 1.00 13
accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45
Hello sir
I think this program is repated and its wrong . This program is based on kNN algorithm but you have given based on Kmeans one. Just please cross verify it and please upload the correct program for this