2.3 Aiml Rishit
2.3 Aiml Rishit
Experiment: 2.3
Aim: To determine the optimal number of clusters using K means algorithm and k nearest
neighbour.
K means clustering
# Visualize the
clusters
plt.figure(figsize=(
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
8, 6))
for cluster_label in range(n_clusters):
cluster_data = data[data['Cluster'] == cluster_label]
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import
make_blobsfrom sklearn.cluster
import KMeans
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_irisfrom sklearn.cluster import KMeans
# Initialize the K-means clustering algorithm with a specified number of clusters (e.g., 3)
n_clusters = 3
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
# Scatter plot each data point colored by cluster plt.scatter(X[:, 0], X[:, 1], c=labels,
cmap='viridis')
K Nearest Neighbour
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import
make_classification from
sklearn.model_selection import train_test_split
from sklearn.neighbors import
KNeighborsClassifierfrom sklearn.metrics
import accuracy_score
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import
make_classification from
sklearn.model_selection import train_test_split
from sklearn.neighbors import
KNeighborsClassifierfrom sklearn.metrics
import accuracy_score
Learning Outcomes:
Learn about cluster.
Learn about K means clustering
Learn about K neighbor clustering