Challenge

Project 2

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE
from sklearn.linear_model import LinearRegression
cardata = pd.read_csv('Cars_mileage.csv') 
print(cardata)
     HP        MPG  VOL          SP         WT
0    49  53.700681   89  104.185353  28.762059
1    55  50.013401   92  105.461264  30.466833
2    55  50.013401   92  105.461264  30.193597
3    70  45.696322   92  113.461264  30.632114
4    53  50.504232   92  104.461264  29.889149
..  ...        ...  ...         ...        ...
76  322  36.900000   50  169.598513  16.132947
77  238  19.197888  115  150.576579  37.923113
78  263  34.000000   50  151.598513  15.769625
79  295  19.833733  119  167.944460  39.423099
80  236  12.101263  107  139.840817  34.948615

[81 rows x 5 columns]
print(cardata.describe())
               HP        MPG         VOL          SP         WT
count   81.000000  81.000000   81.000000   81.000000  81.000000
mean   117.469136  34.422076   98.765432  121.540272  32.412577
std     57.113502   9.131445   22.301497   14.181432   7.492813
min     49.000000  12.101263   50.000000   99.564907  15.712859
25%     84.000000  27.856252   89.000000  113.829145  29.591768
50%    100.000000  35.152727  101.000000  118.208698  32.734518
75%    140.000000  39.531633  113.000000  126.404312  37.392524
max    322.000000  53.700681  160.000000  169.598513  52.997752
cardata.hist(figsize=(10,8))
plt.show()
km=KMeans(n_clusters=3)
print(km)
KMeans(n_clusters=3)
y=km.fit_predict(cardata[["HP","MPG","VOL",'SP',"WT"]])
print(y)
[4 4 4 4 4 4 4 5 5 0 4 5 0 4 4 4 4 5 4 0 0 4 0 0 0 5 0 0 4 0 0 0 0 0 0 0 0
 0 0 6 0 0 0 0 0 0 0 0 6 6 6 6 6 2 2 6 0 6 0 0 2 6 2 2 2 8 2 2 2 7 3 2 2 2
 2 2 3 7 3 1 7]
cardata["cluster"]=y
print(cardata)
     HP        MPG  VOL          SP         WT  cluster
0    49  53.700681   89  104.185353  28.762059        0
1    55  50.013401   92  105.461264  30.466833        0
2    55  50.013401   92  105.461264  30.193597        0
3    70  45.696322   92  113.461264  30.632114        0
4    53  50.504232   92  104.461264  29.889149        0
..  ...        ...  ...         ...        ...      ...
76  322  36.900000   50  169.598513  16.132947        1
77  238  19.197888  115  150.576579  37.923113        1
78  263  34.000000   50  151.598513  15.769625        1
79  295  19.833733  119  167.944460  39.423099        1
80  236  12.101263  107  139.840817  34.948615        1

[81 rows x 6 columns]
car0=cardata[cardata.cluster==0]
car1=cardata[cardata.cluster==1]
car2=cardata[cardata.cluster==2]
print(car0,car1,car2)
     HP        MPG  VOL          SP         WT  cluster
0    49  53.700681   89  104.185353  28.762059        0
1    55  50.013401   92  105.461264  30.466833        0
2    55  50.013401   92  105.461264  30.193597        0
3    70  45.696322   92  113.461264  30.632114        0
4    53  50.504232   92  104.461264  29.889149        0
5    70  45.696322   89  113.185353  29.591768        0
6    55  50.013401   92  105.461264  30.308480        0
7    62  46.716554   50  102.598513  15.847758        0
8    62  46.716554   50  102.598513  16.359484        0
9    80  42.299078   94  115.645204  30.920154        0
10   73  44.652834   89  111.185353  29.363341        0
11   92  39.354094   50  117.598513  15.753535        0
12   92  39.354094   99  122.105055  32.813592        0
13   73  44.652834   89  111.185353  29.378436        0
14   66  45.734893   89  108.185353  29.347279        0
15   73  44.652834   89  111.185353  29.604527        0
16   78  42.789909   91  114.369293  29.535784        0
17   92  39.354094   50  117.598513  16.194122        0
18   78  42.789909   91  114.369293  29.929394        0
19   90  38.901834  103  118.472936  33.516974        0
20   92  38.411003   99  119.105055  32.324650        0
21   74  42.828479  107  110.840817  34.908211        0
22   95  38.310606  101  120.288996  32.675828        0
23   81  40.474723   96  113.829145  31.837122        0
24   95  38.310606   89  119.185353  28.781728        0
25   92  38.411003   50  114.598513  16.043175        0
26   92  38.411003  117  120.760520  38.062823        0
27   92  38.411003   99  119.105055  32.835069        0
28   52  43.469434  104   99.564907  34.483207        0
29  103  35.404192  107  121.840817  35.549360        0
30   84  39.431235  114  113.484609  37.042350        0
31   84  39.431235  101  112.288996  33.234361        0
32  102  36.285456   97  119.921115  31.380041        0
33  102  36.285456  113  121.392639  37.573290        0
34   81  39.531633  101  111.288996  32.701644        0
35   90  37.958743   98  115.013085  31.911223        0
36   90  37.958743   88  114.093383  28.754000        0
37  102  34.070668   86  116.909442  27.879915        0
38  102  34.070668   86  116.909442  28.630502        0
40   95  35.152727  113  116.392639  37.392524        0
41   95  35.152727  106  115.748847  35.027176        0
42  102  34.070668   92  117.461264  30.527427        0
43   95  35.152727   88  114.093383  28.343976        0
44   93  35.643558  102  114.380966  33.078632        0
45  100  34.561499   99  117.105055  32.621916        0
46  100  34.561499  111  118.208698  36.498617        0
47   98  35.052330  103  116.472936  33.910056        0
49  115  29.629936  101  118.288996  33.458472        0
50  115  29.629936  101  118.288996  33.213954        0
51  115  29.629936  101  118.288996  33.436711        0
52  115  29.629936  124  120.404312  40.398164        0
56   96  31.113584   92  110.461264  30.147543        0
57  115  29.629936  101  118.288996  32.734518        0
58  100  30.131923   94  112.645204  30.615283        0
59  100  28.860225  115  115.576579  37.662874        0      HP        MPG  VOL          SP         WT  cluster
69  245  21.273708  112  158.300669  37.141733        1
70  280  19.678507   50  164.598513  15.823060        1
76  322  36.900000   50  169.598513  16.132947        1
77  238  19.197888  115  150.576579  37.923113        1
78  263  34.000000   50  151.598513  15.769625        1
79  295  19.833733  119  167.944460  39.423099        1
80  236  12.101263  107  139.840817  34.948615        1      HP        MPG  VOL          SP         WT  cluster
39  130  31.014131   92  128.461264  30.115434        2
48  130  31.014131   86  127.909442  28.070597        2
53  180  24.487367  113  143.392639  37.620695        2
54  160  26.852279  113  135.392639  37.254392        2
55  130  27.856252  124  126.404312  40.589068        2
60  145  27.354265  111  130.208698  36.888153        2
61  120  24.609132  116  117.668550  37.860411        2
62  140  23.515917  131  126.048103  43.390988        2
63  140  23.515917  123  125.312342  40.722831        2
64  150  23.605158  121  128.128401  40.159482        2
65  165  40.050000   50  126.598513  15.712859        2
66  165  23.103172  114  132.484609  37.979956        2
67  165  23.103172  127  133.680223  41.573975        2
68  165  23.103172  123  133.312342  40.472042        2
71  162  23.203569  135  133.415985  44.013139        2
72  162  23.203569  132  133.140074  43.353123        2
73  140  19.086341  160  124.715241  52.997752        2
74  140  19.086341  129  121.864163  42.618698        2
75  175  18.762837  129  132.864163  42.778219        2
plt.scatter(car1.HP,car1["MPG"],color="red")
plt.show()
plt.scatter(car1.HP,car1["VOL"],color="green")
plt.show()
plt.scatter(car1.HP,car1["SP"],color="purple")
plt.show()
plt.scatter(car1.HP,car1["WT"],color="red")
plt.show()
plt.scatter(car0.HP,car0["MPG"],color="red")
plt.scatter(car1.HP,car1["MPG"],color='blue')
plt.scatter(car2.HP,car2["MPG"],color="green")
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="")
plt.legend()
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
plt.scatter(car0.HP,car0["VOL"],color="red")
plt.scatter(car1.HP,car1["VOL"],color='blue')
plt.scatter(car2.HP,car2["VOL"],color="green")
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="")
plt.legend()
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
plt.scatter(car0.HP,car0["SP"],color="red")
plt.scatter(car1.HP,car1["SP"],color='blue')
plt.scatter(car2.HP,car2["SP"],color="green")
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="")
plt.legend()
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
plt.scatter(car0.HP,car0["WT"],color="red")
plt.scatter(car1.HP,car1["WT"],color='blue')
plt.scatter(car2.HP,car2["WT"],color="green")
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="")
plt.legend()
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
def minmax(col):
    for i in range(0,len(col)):
        value=(float(col[i]))-(float(min(col))) / (float(max(col))-float(min(col)))
        ex.append(value)
ex=[]
minmax(cardata.HP)
cardata["HP_new"]=ex
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new
0    49  53.700681   89  104.185353  28.762059   48.820513
1    55  50.013401   92  105.461264  30.466833   54.820513
2    55  50.013401   92  105.461264  30.193597   54.820513
3    70  45.696322   92  113.461264  30.632114   69.820513
4    53  50.504232   92  104.461264  29.889149   52.820513
..  ...        ...  ...         ...        ...         ...
76  322  36.900000   50  169.598513  16.132947  321.820513
77  238  19.197888  115  150.576579  37.923113  237.820513
78  263  34.000000   50  151.598513  15.769625  262.820513
79  295  19.833733  119  167.944460  39.423099  294.820513
80  236  12.101263  107  139.840817  34.948615  235.820513

[81 rows x 6 columns]
ex=[]
minmax(cardata["MPG"])
cardata["MPG_new"]=ex
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new    MPG_new
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332
..  ...        ...  ...         ...        ...         ...        ...
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363

[81 rows x 7 columns]
ex=[]
minmax(cardata["VOL"])
cardata["VOL_new"]=ex
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new    MPG_new  \
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782   
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501   
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501   
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423   
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332   
..  ...        ...  ...         ...        ...         ...        ...   
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100   
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988   
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100   
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833   
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363   

    cluster_new  cluster     VOL_new  
0             0        1   88.545455  
1             0        1   91.545455  
2             0        1   91.545455  
3             0        1   91.545455  
4             0        1   91.545455  
..          ...      ...         ...  
76            1        2   49.545455  
77            1        9  114.545455  
78            1        2   49.545455  
79            1        4  118.545455  
80            1        9  106.545455  

[81 rows x 10 columns]
ex=[]
minmax(cardata["SP"])
cardata["SP_new"]=ex
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new    MPG_new  \
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782   
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501   
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501   
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423   
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332   
..  ...        ...  ...         ...        ...         ...        ...   
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100   
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988   
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100   
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833   
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363   

    cluster_new  cluster     VOL_new      SP_new  
0             0        1   88.545455  102.763680  
1             0        1   91.545455  104.039590  
2             0        1   91.545455  104.039590  
3             0        1   91.545455  112.039590  
4             0        1   91.545455  103.039590  
..          ...      ...         ...         ...  
76            1        2   49.545455  168.176840  
77            1        9  114.545455  149.154906  
78            1        2   49.545455  150.176840  
79            1        4  118.545455  166.522787  
80            1        9  106.545455  138.419144  

[81 rows x 11 columns]
ex=[]
minmax(cardata["WT"])
cardata["WT_new"]=ex
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new    MPG_new  \
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782   
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501   
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501   
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423   
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332   
..  ...        ...  ...         ...        ...         ...        ...   
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100   
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988   
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100   
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833   
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363   

    cluster_new  cluster     VOL_new      SP_new     WT_new  
0             0        1   88.545455  102.763680  28.340632  
1             0        1   91.545455  104.039590  30.045406  
2             0        1   91.545455  104.039590  29.772170  
3             0        1   91.545455  112.039590  30.210687  
4             0        1   91.545455  103.039590  29.467722  
..          ...      ...         ...         ...        ...  
76            1        2   49.545455  168.176840  15.711521  
77            1        9  114.545455  149.154906  37.501686  
78            1        2   49.545455  150.176840  15.348198  
79            1        4  118.545455  166.522787  39.001672  
80            1        9  106.545455  138.419144  34.527188  

[81 rows x 12 columns]
km=KMeans(n_clusters=3)
print(km) 
KMeans(n_clusters=3)
new_value=km.fit_predict(cardata[["HP_new","MPG_new"]])
cardata["cluster_new"]=new_value
print(cardata) 
     HP        MPG  VOL          SP         WT      HP_new    MPG_new  \
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782   
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501   
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501   
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423   
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332   
..  ...        ...  ...         ...        ...         ...        ...   
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100   
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988   
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100   
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833   
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363   

    cluster_new  
0             0  
1             0  
2             0  
3             0  
4             0  
..          ...  
76            1  
77            1  
78            1  
79            1  
80            1  

[81 rows x 8 columns]
new_value=km.fit_predict(cardata[["HP_new","VOL_new"]])
cardata["cluster_new"]=new_value
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new    MPG_new  \
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782   
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501   
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501   
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423   
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332   
..  ...        ...  ...         ...        ...         ...        ...   
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100   
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988   
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100   
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833   
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363   

    cluster_new  cluster     VOL_new      SP_new     WT_new  
0             1        1   88.545455  102.763680  28.340632  
1             1        1   91.545455  104.039590  30.045406  
2             1        1   91.545455  104.039590  29.772170  
3             1        1   91.545455  112.039590  30.210687  
4             1        1   91.545455  103.039590  29.467722  
..          ...      ...         ...         ...        ...  
76            2        2   49.545455  168.176840  15.711521  
77            5        9  114.545455  149.154906  37.501686  
78            2        2   49.545455  150.176840  15.348198  
79            5        4  118.545455  166.522787  39.001672  
80            5        9  106.545455  138.419144  34.527188  

[81 rows x 12 columns]
new_value=km.fit_predict(cardata[["HP_new","SP_new"]])
cardata["cluster_new"]=new_value
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new    MPG_new  \
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782   
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501   
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501   
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423   
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332   
..  ...        ...  ...         ...        ...         ...        ...   
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100   
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988   
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100   
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833   
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363   

    cluster_new  cluster     VOL_new      SP_new     WT_new  
0             3        1   88.545455  102.763680  28.340632  
1             3        1   91.545455  104.039590  30.045406  
2             3        1   91.545455  104.039590  29.772170  
3             6        1   91.545455  112.039590  30.210687  
4             3        1   91.545455  103.039590  29.467722  
..          ...      ...         ...         ...        ...  
76            8        2   49.545455  168.176840  15.711521  
77            5        9  114.545455  149.154906  37.501686  
78            5        2   49.545455  150.176840  15.348198  
79            1        4  118.545455  166.522787  39.001672  
80            5        9  106.545455  138.419144  34.527188  

[81 rows x 12 columns]
new_value=km.fit_predict(cardata[["HP_new","WT_new"]])
cardata["cluster_new"]=new_value
print(cardata)
     HP        MPG  VOL          SP         WT      HP_new    MPG_new  \
0    49  53.700681   89  104.185353  28.762059   48.820513  53.409782   
1    55  50.013401   92  105.461264  30.466833   54.820513  49.722501   
2    55  50.013401   92  105.461264  30.193597   54.820513  49.722501   
3    70  45.696322   92  113.461264  30.632114   69.820513  45.405423   
4    53  50.504232   92  104.461264  29.889149   52.820513  50.213332   
..  ...        ...  ...         ...        ...         ...        ...   
76  322  36.900000   50  169.598513  16.132947  321.820513  36.609100   
77  238  19.197888  115  150.576579  37.923113  237.820513  18.906988   
78  263  34.000000   50  151.598513  15.769625  262.820513  33.709100   
79  295  19.833733  119  167.944460  39.423099  294.820513  19.542833   
80  236  12.101263  107  139.840817  34.948615  235.820513  11.810363   

    cluster_new  cluster     VOL_new      SP_new     WT_new  
0             7        1   88.545455  102.763680  28.340632  
1             7        1   91.545455  104.039590  30.045406  
2             7        1   91.545455  104.039590  29.772170  
3             3        1   91.545455  112.039590  30.210687  
4             7        1   91.545455  103.039590  29.467722  
..          ...      ...         ...         ...        ...  
76            5        2   49.545455  168.176840  15.711521  
77            0        9  114.545455  149.154906  37.501686  
78            4        2   49.545455  150.176840  15.348198  
79            5        4  118.545455  166.522787  39.001672  
80            0        9  106.545455  138.419144  34.527188  

[81 rows x 12 columns]
car0=cardata[cardata.cluster_new==0]
car1=cardata[cardata.cluster_new==1]
car2=cardata[cardata.cluster_new==2]
plt.scatter(car0.HP,car0["MPG_new"],color="red")
plt.scatter(car1.HP,car1["MPG_new"],color="blue")
plt.scatter(car2.HP,car2["MPG_new"],color="green")
print(km.cluster_centers_)
print(km.algorithm)
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="*")
plt.legend()
plt.show()
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
[[ 86.58414918  38.77594231]
 [268.24908425  22.99268566]
 [150.55735493  24.78945382]]
auto
car0=cardata[cardata.cluster_new==0]
car1=cardata[cardata.cluster_new==1]
car2=cardata[cardata.cluster_new==2]
plt.scatter(car0.HP,car0["VOL_new"],color="red")
plt.scatter(car1.HP,car1["VOL_new"],color="blue")
plt.scatter(car2.HP,car2["VOL_new"],color="green")
print(km.cluster_centers_)
print(km.algorithm)
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="*")
plt.legend()
plt.show()
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
[[239.48717949  36.24972679]
 [ 95.85622711  30.52521062]
 [142.32051282  42.37489064]
 [ 75.89194139  30.86617921]
 [271.32051282  15.37491598]
 [308.32051282  27.35659628]
 [120.37606838  34.00938738]
 [ 55.19551282  26.61739378]
 [166.37606838  37.44061735]]
auto
car0=cardata[cardata.cluster_new==0]
car1=cardata[cardata.cluster_new==1]
car2=cardata[cardata.cluster_new==2]
plt.scatter(car0.HP,car0["SP_new"],color="red")
plt.scatter(car1.HP,car1["SP_new"],color="blue")
plt.scatter(car2.HP,car2["SP_new"],color="green")
print(km.cluster_centers_)
print(km.algorithm)
plt.scatter(km.cluster_centers_[:,0],
            km.cluster_centers_[:,1],
            color="purple",marker="*")
plt.legend()
plt.show()
 
[[239.48717949  36.24972679]
 [ 95.85622711  30.52521062]
 [142.32051282  42.37489064]
 [ 75.89194139  30.86617921]
 [271.32051282  15.37491598]
 [308.32051282  27.35659628]
 [120.37606838  34.00938738]
 [ 55.19551282  26.61739378]
 [166.37606838  37.44061735]]
auto
car0=cardata[cardata.cluster_new==0]
car1=cardata[cardata.cluster_new==1]
car2=cardata[cardata.cluster_new==2]
plt.scatter(car0.HP,car0["MPG_new"],color="red")
plt.scatter(car1.HP,car1["MPG_new"],color="blue")
plt.scatter(car2.HP,car2["MPG_new"],color="green")
print(km.cluster_centers_)
print(km.algorithm)
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color="purple",marker="*")
plt.legend()
plt.show()
 
[[239.48717949  36.24972679]
 [ 95.85622711  30.52521062]
 [142.32051282  42.37489064]
 [ 75.89194139  30.86617921]
 [271.32051282  15.37491598]
 [308.32051282  27.35659628]
 [120.37606838  34.00938738]
 [ 55.19551282  26.61739378]
 [166.37606838  37.44061735]]
auto
#elbow curve
k_range =range(1,10)
sse=[]
for k in k_range:
    km=KMeans(n_clusters=k)
    km.fit(cardata[["HP_new","MPG_new"]])
    sse.append(km.inertia_)
print(sse)
plt.plot(k_range,sse)
plt.show()
C:\Users\Admin\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:1036: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.
  warnings.warn(
[267626.83547069365, 90696.74184835266, 31508.87696331378, 17453.76836647474, 12521.939792827963, 8081.473788216265, 6170.608026067637, 4464.0842722293455, 3857.5298496941414]
k_range =range(1,10)
sse=[]
for k in k_range:
    km=KMeans(n_clusters=k)
    km.fit(cardata[["HP_new","VOL_new"]])
    sse.append(km.inertia_)
print(sse)
plt.plot(k_range,sse)
plt.show()
C:\Users\Admin\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:1036: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.
  warnings.warn(
[300744.7160493827, 124912.80115830115, 58942.978835978836, 43343.27155483405, 34405.116792929286, 26527.852579365077, 21058.84841269841, 17726.16193036354, 14979.611418992885]
k_range =range(1,10)
sse=[]
for k in k_range:
    km=KMeans(n_clusters=k)
    km.fit(cardata[["HP_new","SP_new"]])
    sse.append(km.inertia_)
print(sse)
plt.plot(k_range,sse)
plt.show()
C:\Users\Admin\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:1036: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.
  warnings.warn(
[277045.21295974904, 91944.91154016042, 31371.985934869954, 17591.584497874916, 12171.721518338545, 7929.548012730878, 5739.266263613302, 3884.3621374702157, 3215.768868932697]
k_range =range(1,10)
sse=[]
for k in k_range:
    km=KMeans(n_clusters=k)
    km.fit(cardata[["HP_new","WT_new"]])
    sse.append(km.inertia_)
print(sse)
plt.plot(k_range,sse)
plt.show()
C:\Users\Admin\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:1036: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.
  warnings.warn(
[265447.5525686185, 90529.8448677072, 32036.741783879886, 19187.340728265353, 14180.230381758134, 10073.618221157674, 8146.488655655978, 6908.161187242607, 5060.312205399491]

final conclusion is a K initial value is 3 and the kMeans value is also 3. both the value is same

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.