Challenge

Unsupervised Learning - Kmeans Week 11 Challenge

Question 1: How does similarity is calculated if data is categorical in nature?

Answer: The Similarity Measure is a way of measuring how data samples are related or close to each other.

There are Several ways to find the similarity in the categorical in nature. They are mentioned below as,

1. Euclidean.

2. Manhattan.

3.Chebychev.

4. Minkowski.

5. Cosine.

6. Pearson.

7. Mahalanobis.

8. SED.

9. Jaccard.

10. Levenshtein.

11. Sorenson-Dice.

12. Jensen-Shannon.

13. Canberra.

14. Hamming.

15. Spearman.

16.Chi-Square.(X^2=(Observed frequency-expected frequency)^2/Expected frequency)

so by any of the above method we will find the similarity for the categorical data.

Question 2: Implement K-means for “Car dataset” and come up with the business insights?

Answer:

from os import execl
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
df=pd.read_csv("auto-mpg_kaggle.csv")
print(df)
print(df.describe())

def minmax(col):
    for i in range(0,len(col)):
        new_value=(float(col[i])-float(min(col)))/(float(max(col))-float(min(col)))
        ex.append(new_value)

ex=[]
minmax(df.cylinders)
df["cylinders_minmax"]=ex

ex=[]
minmax(df.displacement)
df["displacement_minmax"]=ex

ex=[]
minmax(df.horsepower)
df["horsepower_minmax"]=ex

ex=[]
minmax(df.weight)
df["weight_minmax"]=ex

ex=[]
minmax(df.acceleration)
df["acceleration_minmax"]=ex

ex=[]
minmax(df.mpg)
df["mpg_minmax"]=ex
print(df)

km = KMeans(n_clusters=2)
print(km)
Y_new=km.fit_predict(df[["cylinders_minmax","displacement_minmax","horsepower_minmax","weight_minmax","acceleration_minmax","mpg_minmax",]])
print(Y_new)
df["cluster_minmax"]=Y_new
print(df)
df0=df[df.cluster_minmax==0]
df1=df[df.cluster_minmax==1]

plt.scatter(df0.acceleration_minmax,df0["mpg_minmax"],color="red")
plt.scatter(df1.acceleration_minmax,df1["mpg_minmax"],color="blue")

print(km.cluster_centers_)
print(km.algorithm)
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="*")
plt.legend()
plt.show()

k_range=range(1,10)
sse=[]
for k in k_range:
    km=KMeans(n_clusters=k)
    km.fit(df[["cylinders_minmax","displacement_minmax","horsepower_minmax","weight_minmax","acceleration_minmax","mpg_minmax"]])
    sse.append(km.inertia_)
print(sse)
plt.plot(k_range,sse)
plt.show()

Result:
      mpg  cylinders  displacement  horsepower  weight  acceleration  \
0    18.0          8         307.0         130    3504          12.0   
1    15.0          8         350.0         165    3693          11.5   
2    18.0          8         318.0         150    3436          11.0   
3    16.0          8         304.0         150    3433          12.0   
4    17.0          8         302.0         140    3449          10.5   
..    ...        ...           ...         ...     ...           ...   
393  27.0          4         140.0          86    2790          15.6   
394  44.0          4          97.0          52    2130          24.6   
395  32.0          4         135.0          84    2295          11.6   
396  28.0          4         120.0          79    2625          18.6   
397  31.0          4         119.0          82    2720          19.4   

     model year  origin                   car name  
0            70       1  chevrolet chevelle malibu  
1            70       1          buick skylark 320  
2            70       1         plymouth satellite  
3            70       1              amc rebel sst  
4            70       1                ford torino  
..          ...     ...                        ...  
393          82       1            ford mustang gl  
394          82       2                  vw pickup  
395          82       1              dodge rampage  
396          82       1                ford ranger  
397          82       1                 chevy s-10  

[398 rows x 9 columns]
              mpg   cylinders  displacement  horsepower       weight  \
count  398.000000  398.000000    398.000000  398.000000   398.000000   
mean    23.514573    5.454774    193.425879  104.185930  2970.424623   
std      7.815984    1.701004    104.269838   38.292729   846.841774   
min      9.000000    3.000000     68.000000   46.000000  1613.000000   
25%     17.500000    4.000000    104.250000   75.000000  2223.750000   
50%     23.000000    4.000000    148.500000   93.500000  2803.500000   
75%     29.000000    8.000000    262.000000  125.000000  3608.000000   
max     46.600000    8.000000    455.000000  230.000000  5140.000000   

       acceleration  model year      origin  
count    398.000000  398.000000  398.000000  
mean      15.568090   76.010050    1.572864  
std        2.757689    3.697627    0.802055  
min        8.000000   70.000000    1.000000  
25%       13.825000   73.000000    1.000000  
50%       15.500000   76.000000    1.000000  
75%       17.175000   79.000000    2.000000  
max       24.800000   82.000000    3.000000  
No handles with labels found to put in legend.
      mpg  cylinders  displacement  horsepower  weight  acceleration  \
0    18.0          8         307.0         130    3504          12.0   
1    15.0          8         350.0         165    3693          11.5   
2    18.0          8         318.0         150    3436          11.0   
3    16.0          8         304.0         150    3433          12.0   
4    17.0          8         302.0         140    3449          10.5   
..    ...        ...           ...         ...     ...           ...   
393  27.0          4         140.0          86    2790          15.6   
394  44.0          4          97.0          52    2130          24.6   
395  32.0          4         135.0          84    2295          11.6   
396  28.0          4         120.0          79    2625          18.6   
397  31.0          4         119.0          82    2720          19.4   

     model year  origin                   car name  cylinders_minmax  \
0            70       1  chevrolet chevelle malibu               1.0   
1            70       1          buick skylark 320               1.0   
2            70       1         plymouth satellite               1.0   
3            70       1              amc rebel sst               1.0   
4            70       1                ford torino               1.0   
..          ...     ...                        ...               ...   
393          82       1            ford mustang gl               0.2   
394          82       2                  vw pickup               0.2   
395          82       1              dodge rampage               0.2   
396          82       1                ford ranger               0.2   
397          82       1                 chevy s-10               0.2   

     displacement_minmax  horsepower_minmax  weight_minmax  \
0               0.617571           0.456522       0.536150   
1               0.728682           0.646739       0.589736   
2               0.645995           0.565217       0.516870   
3               0.609819           0.565217       0.516019   
4               0.604651           0.510870       0.520556   
..                   ...                ...            ...   
393             0.186047           0.217391       0.333711   
394             0.074935           0.032609       0.146583   
395             0.173127           0.206522       0.193365   
396             0.134367           0.179348       0.286929   
397             0.131783           0.195652       0.313864   

     acceleration_minmax  mpg_minmax  
0               0.238095    0.239362  
1               0.208333    0.159574  
2               0.178571    0.239362  
3               0.238095    0.186170  
4               0.148810    0.212766  
..                   ...         ...  
393             0.452381    0.478723  
394             0.988095    0.930851  
395             0.214286    0.611702  
396             0.630952    0.505319  
397             0.678571    0.585106  

[398 rows x 15 columns]
KMeans(n_clusters=2)
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1
 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0
 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0 1
 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 1 1 1
 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0
 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
      mpg  cylinders  displacement  horsepower  weight  acceleration  \
0    18.0          8         307.0         130    3504          12.0   
1    15.0          8         350.0         165    3693          11.5   
2    18.0          8         318.0         150    3436          11.0   
3    16.0          8         304.0         150    3433          12.0   
4    17.0          8         302.0         140    3449          10.5   
..    ...        ...           ...         ...     ...           ...   
393  27.0          4         140.0          86    2790          15.6   
394  44.0          4          97.0          52    2130          24.6   
395  32.0          4         135.0          84    2295          11.6   
396  28.0          4         120.0          79    2625          18.6   
397  31.0          4         119.0          82    2720          19.4   

     model year  origin                   car name  cylinders_minmax  \
0            70       1  chevrolet chevelle malibu               1.0   
1            70       1          buick skylark 320               1.0   
2            70       1         plymouth satellite               1.0   
3            70       1              amc rebel sst               1.0   
4            70       1                ford torino               1.0   
..          ...     ...                        ...               ...   
393          82       1            ford mustang gl               0.2   
394          82       2                  vw pickup               0.2   
395          82       1              dodge rampage               0.2   
396          82       1                ford ranger               0.2   
397          82       1                 chevy s-10               0.2   

     displacement_minmax  horsepower_minmax  weight_minmax  \
0               0.617571           0.456522       0.536150   
1               0.728682           0.646739       0.589736   
2               0.645995           0.565217       0.516870   
3               0.609819           0.565217       0.516019   
4               0.604651           0.510870       0.520556   
..                   ...                ...            ...   
393             0.186047           0.217391       0.333711   
394             0.074935           0.032609       0.146583   
395             0.173127           0.206522       0.193365   
396             0.134367           0.179348       0.286929   
397             0.131783           0.195652       0.313864   

     acceleration_minmax  mpg_minmax  cluster_minmax  
0               0.238095    0.239362               1  
1               0.208333    0.159574               1  
2               0.178571    0.239362               1  
3               0.238095    0.186170               1  
4               0.148810    0.212766               1  
..                   ...         ...             ...  
393             0.452381    0.478723               0  
394             0.988095    0.930851               0  
395             0.214286    0.611702               0  
396             0.630952    0.505319               0  
397             0.678571    0.585106               0  

[398 rows x 16 columns]
[[0.2460251  0.13048014 0.19142259 0.21970739 0.50206714 0.51599083]
 [0.8591195  0.61513334 0.50382827 0.63312488 0.372941   0.19066974]]
auto

[142.69975011779007, 47.04873541164898, 25.14455169702185, 20.31536442327276, 17.400459716185562, 15.600400938593374, 14.290559840638814, 13.106351473577453, 12.281248425880117]

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.