Compute2
Compute2
Compute2
import numpy as np
import pandas as pd
import os
x = pd.read_csv('data.csv')
x.dropna()
print(x)
2nd task: perform K-means Clustering for K=3,5,7 and also Fuzzy C means. Capture the Clusters
generated with Both K Means & C means.
import pandas as pd
from sklearn.cluster import KMeans
z = pd.read_csv('data.csv')
X = z.iloc[:, 1:59].values
scale = StandardScaler()
X = scale.fit_transform(X)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
k_values = [3, 5, 7]
fuzzy_cmeans_c = [3, 5, 7]
for k in k_values:
kmeans = KMeans(n_clusters=k, random_state=42)
y_kmeans = kmeans.fit_predict(X_pca)
plt.figure(figsize=(6, 4))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y_kmeans, cmap='viridis')
plt.title('K-means clustering (K = ' + str(k) + ')')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
for c in fuzzy_cmeans_c:
fcm = FCM(n_clusters=10, m=c)
fcm.fit(X_pca)
y_fcm = fcm.predict(X_pca)
plt.figure(figsize=(6, 4))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y_fcm, cmap='viridis')
plt.title('Fuzzy C means clustering (c = ' + str(c) + ')')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
OUTPUT
3rd task: perform Bottom-up Clustering (Agglomerative clustering). Capture the Clusters generated
at a different level, and also prepare dendrograms.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram, linkage
Data = pd.read_csv('data.csv')
# Observations:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.decomposition import PCA
data = pd.read_csv('data.csv')
pc = PCA(n_components=2)
reduced_features = pc.fit_transform(mfs)
# Observations:
# DBSCAN clustering algorithm forms clusters based on the density of the data
points.
# The resulting clusters are not well