0% found this document useful (0 votes)
4 views

Lab3 AI

Artificial Intelligence Lab 3

Uploaded by

21b-039-cs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab3 AI

Artificial Intelligence Lab 3

Uploaded by

21b-039-cs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6/8/24, 2:29 PM Lab 3

K-means cluster
In [5]: import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances
from scipy.cluster.hierarchy import linkage, dendrogram, cut_tree
from scipy.spatial.distance import pdist
from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_csv("C:\\Users\\HP\\Desktop\\Pokemon.csv", index_col=1)
df.drop('Unnamed: 0', axis=1, inplace=True)
df.head()

# clean the data


pokemon = df.drop(columns=['Type 1','Type 2','Legendary'])
pokemon.head()

# Model Training -- Logistic Regression


km = KMeans(n_clusters=2)
km.fit(pokemon)

# Prediction
pokemon['label'] = km.predict(pokemon)
pokemon.head()

# Visualisation
plt.scatter(pokemon['Attack'], pokemon['Defense'], c=pokemon['label'])
plt.show()
plt.scatter(pokemon['HP'], pokemon['Speed'], c=pokemon['label'])
plt.show()

file:///E:/Downloads/Lab 3.html 1/5


6/8/24, 2:29 PM Lab 3

Hierarchical Clustering
In [11]: import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances

file:///E:/Downloads/Lab 3.html 2/5


6/8/24, 2:29 PM Lab 3

from scipy.cluster.hierarchy import linkage, dendrogram, cut_tree


from scipy.spatial.distance import pdist
from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_csv("C:\\Users\\HP\\Desktop\\Pokemon.csv", index_col=1)
df.drop('Unnamed: 0', axis=1, inplace=True)
df.head()

# clean the data


pokemon = df.drop(columns=['Type 1','Type 2','Legendary'])
pokemon.head()

# Model Training
dist = pdist(pokemon, 'euclidean')
linkage_matrix = linkage(dist, method = 'complete') ## ward

# Train the model with the training data


labels = cut_tree(linkage_matrix, n_clusters=2)
pokemon['label_HC'] = labels
pokemon.head()

Out[11]: Total HP Attack Defense Sp. Atk Sp. Def Speed Stage label_HC

Name

Bulbasaur 318 45 49 49 65 65 45 1 0

Ivysaur 405 60 62 63 80 80 60 2 1

Venusaur 525 80 82 83 100 100 80 3 1

Charmander 309 39 52 43 60 50 65 1 0

Charmeleon 405 58 64 58 80 65 80 2 1

Home Tasks
K-means using 3 clusters
In [9]: import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances
from scipy.cluster.hierarchy import linkage, dendrogram, cut_tree
from scipy.spatial.distance import pdist
from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_csv("C:\\Users\\HP\\Desktop\\Pokemon.csv", index_col=1)
df.drop('Unnamed: 0', axis=1, inplace=True)
df.head()

file:///E:/Downloads/Lab 3.html 3/5


6/8/24, 2:29 PM Lab 3

# clean the data


pokemon = df.drop(columns=['Type 1','Type 2','Legendary'])
pokemon.head()

# Model Training -- Logistic Regression


km = KMeans(n_clusters=3)
km.fit(pokemon)

# Prediction
pokemon['label'] = km.predict(pokemon)
pokemon.head()

# Visualisation
plt.scatter(pokemon['Attack'], pokemon['Defense'], c=pokemon['label'])
plt.show()

Hierarchical Clustering with 3 clusters


In [13]: import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances
from scipy.cluster.hierarchy import linkage, dendrogram, cut_tree
from scipy.spatial.distance import pdist
from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_csv("C:\\Users\\HP\\Desktop\\Pokemon.csv", index_col=1)
df.drop('Unnamed: 0', axis=1, inplace=True)

file:///E:/Downloads/Lab 3.html 4/5


6/8/24, 2:29 PM Lab 3

df.head()

# clean the data


pokemon = df.drop(columns=['Type 1','Type 2','Legendary'])
pokemon.head()

# Model Training
dist = pdist(pokemon, 'euclidean')
linkage_matrix = linkage(dist, method = 'complete') ## ward

# Train the model with the training data


labels = cut_tree(linkage_matrix, n_clusters=3)
pokemon['label_HC'] = labels
pokemon.head()

# Visualisation
plt.scatter(pokemon['Attack'], pokemon['Defense'], c=pokemon['label_HC'])
plt.show()

file:///E:/Downloads/Lab 3.html 5/5

You might also like