Thursday, January 16, 2020

machine learning Last session

In [1]:
import pandas as pd
In [3]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
In [6]:
data=pd.read_csv('data.csv',index_col=0)
data.head()
Out[6]:
xycluster
0-8.482852-5.6033492
1-7.751632-8.4053342
2-10.967098-9.0327822
3-11.999447-7.6067342
4-1.73681010.4780151
In [7]:
data['cluster'].value_counts()
Out[7]:
1    67
0    67
2    66
Name: cluster, dtype: int64
In [8]:
plt.scatter(data['x'],data['y'],c=data['cluster'])
Out[8]:
<matplotlib.collections.PathCollection at 0x2793648d748>
In [9]:
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
In [14]:
X=data[['x','y']]
y=data['cluster']
In [15]:
scaler=StandardScaler()
X=scaler.fit_transform(x
                      )
In [16]:
X
Out[16]:
array([[-1.01200363, -0.60606415],
       [-0.86550679, -1.04265203],
       [-1.5097118 , -1.14041707],
       [-1.71653856, -0.91821912],
       [ 0.33953731,  1.89963378],
       [ 0.38183922,  1.1890842 ],
       [ 0.24668446, -0.74232845],
       [-1.44313178, -0.76182643],
       [ 0.32130834, -0.75849023],
       [ 0.83110494,  1.27684537],
       [ 0.31775088, -0.39338956],
       [ 1.20365218, -0.4290876 ],
       [-0.07305683,  1.51966781],
       [ 0.63827173,  0.98134203],
       [ 1.52205697, -0.42280098],
       [ 0.17043691,  1.06578799],
       [ 1.5381542 , -0.52935538],
       [ 0.27161133, -0.71651135],
       [-1.35289151, -0.93641563],
       [ 0.79308339, -0.35212834],
       [ 0.14633873,  2.31330628],
       [ 1.14228061, -0.30354302],
       [ 1.12503491, -0.04177872],
       [-1.17977302, -1.09578279],
       [-2.01238779, -0.86540408],
       [ 0.19152374,  1.57228952],
       [ 0.83261025,  1.80799546],
       [ 0.66508288, -0.6110459 ],
       [-1.12827817, -0.15250042],
       [ 0.52241815, -0.65585313],
       [ 1.08583214,  0.93547726],
       [-0.97693538, -0.88741691],
       [-1.33595694, -0.63086238],
       [ 0.19953335,  1.28083486],
       [-0.94950185, -0.56065403],
       [ 0.66982823, -0.13484351],
       [ 0.771665  , -0.5499059 ],
       [ 0.2774257 ,  1.22860396],
       [-0.73472586, -1.2187408 ],
       [-0.63187335, -1.00655651],
       [-0.69416438, -1.0000677 ],
       [ 0.83271023, -0.6731856 ],
       [-1.23344655, -1.15789171],
       [ 0.65096239, -0.19455043],
       [-1.39325586, -1.1523753 ],
       [-1.49484828, -0.92704231],
       [-1.45695253, -0.70502951],
       [ 0.5305949 ,  0.81280245],
       [ 0.83400406, -0.42253729],
       [ 0.48904363,  1.24633059],
       [ 1.23175068, -0.82532629],
       [ 0.69624505, -0.30159923],
       [ 0.39391193, -0.396964  ],
       [ 0.79094876,  1.60143791],
       [ 0.15899074,  1.26432118],
       [ 0.39413305,  1.76704755],
       [ 0.86405762, -0.59555499],
       [-2.03584663, -0.69245567],
       [ 0.59854512,  1.21885094],
       [ 0.46524738,  1.14933569],
       [ 1.13205009, -0.79021258],
       [-0.43326051,  1.24378251],
       [ 1.18403178, -0.2105801 ],
       [ 1.104018  , -0.45293091],
       [ 0.90917625, -0.41741704],
       [ 0.24940112,  1.47054385],
       [-1.34378511, -0.96019197],
       [-1.15864684, -0.53793509],
       [-1.38106913, -1.24446575],
       [ 1.32888112, -0.15656749],
       [-1.97542172, -0.86113934],
       [ 0.55709844,  0.94043404],
       [-1.16375964, -1.04708045],
       [-0.92116118, -0.60408404],
       [ 0.19744613, -0.320971  ],
       [-1.17058201, -0.94932292],
       [-1.30394578, -0.6337949 ],
       [-1.3764337 , -0.98303292],
       [ 0.29643725, -0.16983389],
       [-1.48660911, -1.07632091],
       [ 0.88544854,  0.85471602],
       [ 0.9006815 , -0.4157147 ],
       [ 0.2206326 ,  1.52350226],
       [ 1.13326512,  1.24514921],
       [ 1.59514484, -0.57810326],
       [ 0.15529192, -0.46145428],
       [ 1.07078756, -0.73342709],
       [-1.84390846, -0.81992791],
       [ 0.90990583,  1.40916629],
       [ 0.3576306 , -0.73136536],
       [ 1.29042424, -0.89780574],
       [-1.36084266, -0.77007109],
       [-1.0236826 , -1.24868072],
       [ 0.7946996 , -1.12120567],
       [ 0.79884711,  1.66676256],
       [ 0.99941321,  1.13889426],
       [-1.36837982, -1.15198567],
       [-0.3156759 , -1.46559894],
       [ 0.70861868, -0.75627388],
       [-0.21082427, -0.41430294],
       [ 0.32589705,  1.68264012],
       [ 0.66619897,  1.21855463],
       [ 0.49361345,  0.86887702],
       [ 0.5068162 , -0.32337893],
       [ 0.74177357, -0.61694863],
       [-1.2885399 , -0.96008709],
       [ 1.02551131,  1.48533141],
       [ 0.43201879,  1.6076944 ],
       [ 1.08249536, -0.21716763],
       [-0.15265406,  0.0228466 ],
       [ 0.97586982, -0.63467294],
       [-1.10238107, -0.75993911],
       [ 0.24621536, -0.32010977],
       [ 0.53736804,  1.0345916 ],
       [ 1.21425236,  1.07790672],
       [-0.00967353,  1.27374229],
       [ 0.53207638,  1.10912864],
       [-2.00277598, -1.07059812],
       [ 1.33013807, -0.90971954],
       [-1.25463745, -1.18101253],
       [ 0.53775645,  1.29669311],
       [ 0.27952731, -0.01116382],
       [ 0.32201814,  1.62673893],
       [ 0.4329941 , -0.57783171],
       [ 0.57365299,  1.3368478 ],
       [ 0.65082523,  1.46367753],
       [ 0.65445327,  1.46194122],
       [ 0.30583921,  0.15038392],
       [ 0.58866226, -0.19466067],
       [ 0.77058657, -0.65992081],
       [-1.73473402, -0.85951427],
       [ 1.21530606, -0.27942699],
       [ 0.37146484,  1.57464784],
       [ 0.13101932,  1.17557222],
       [ 0.55048135, -0.073215  ],
       [ 0.65268228, -0.76005108],
       [-0.12524786,  1.16172152],
       [ 0.9416221 , -0.8284188 ],
       [ 0.72962605, -0.78012446],
       [-0.7595301 , -1.02429527],
       [ 0.42537401,  2.05365336],
       [ 0.41686597,  1.17237934],
       [-1.36156287, -0.58182547],
       [-1.63215702, -0.80053828],
       [ 0.16868097,  1.78349137],
       [-1.25198849, -1.21104408],
       [ 0.45461854,  1.24370166],
       [-1.99356038, -0.90548851],
       [ 0.84572607,  1.2164603 ],
       [ 1.11029307, -0.23099879],
       [-0.56892205, -0.70265972],
       [ 0.93402653,  1.29095874],
       [ 0.94118216, -0.72375673],
       [ 0.23509561,  0.96041989],
       [ 0.60301693,  1.08704203],
       [-1.1171891 , -1.21457231],
       [ 1.21399433,  1.29509665],
       [-1.25249385, -0.47819655],
       [-0.986743  , -0.98144191],
       [-1.7860204 , -1.39231882],
       [ 0.55320627,  1.41715403],
       [-2.0662249 , -0.69934998],
       [-1.29343007, -0.77757608],
       [ 0.27276956,  0.9418664 ],
       [ 0.5434236 , -0.41841969],
       [ 1.44526084,  0.0087187 ],
       [-0.34167386,  1.31912378],
       [-1.77472219, -0.84542156],
       [ 0.8071665 ,  1.92415722],
       [-1.41813112, -0.95059979],
       [ 0.26971005,  1.01953102],
       [ 0.90067843, -0.28122456],
       [ 0.65661936,  0.89004119],
       [ 0.42245075, -0.4677546 ],
       [-1.75542164, -0.6517002 ],
       [ 0.57522939,  1.23645334],
       [ 0.4217509 ,  1.70837732],
       [-1.23985549, -0.31419264],
       [-1.50342086, -0.14237115],
       [ 0.77945616,  1.27706047],
       [ 0.9279614 , -0.16082871],
       [-1.39989229, -0.77754158],
       [ 0.94719192, -0.41984737],
       [-2.06564945, -0.83849631],
       [-0.71883787, -0.62819834],
       [-1.17765783, -1.01506935],
       [ 0.70341832, -0.73729614],
       [-1.26359243, -0.85907062],
       [-1.08899436, -0.88801288],
       [ 1.132047  , -0.55152432],
       [ 0.52620981,  1.43321291],
       [ 0.74377108, -0.4080321 ],
       [ 0.77533462, -0.82445019],
       [ 1.43342856, -0.2291756 ],
       [ 0.72870678,  1.06563353],
       [-1.07957503, -1.20998434],
       [ 0.37227198, -0.76930174],
       [ 0.16267793,  1.55547825],
       [ 0.15970539,  1.31058228],
       [-1.7069593 , -0.84258607]])
In [19]:
data[['x','y']]=X
In [20]:
Kmeans=KMeans(n_clusters=2,random_state=42)
Kmeans.fit(X)
Out[20]:
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
       n_clusters=2, n_init=10, n_jobs=None, precompute_distances='auto',
       random_state=42, tol=0.0001, verbose=0)
center=Kmeans.clustercenters center
In [21]:
center=Kmeans.cluster_centers_ 
center
Out[21]:
array([[ 0.63701655,  0.42500526],
       [-1.32303438, -0.88270323]])
In [22]:
label=Kmeans.labels_
label
Out[22]:
array([1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
       0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0,
       1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
       1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
       0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
       0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
       0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0,
       0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0,
       0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,
       0, 1])
In [23]:
plt.scatter(data['x'],data['y'],c=label)
Out[23]:
<matplotlib.collections.PathCollection at 0x27938ef1d68>
In [30]:
SEE=[]
index=range(1,10)
for i in index:
    kmeans=KMeans(n_clusters=i,random_state=42)
    kmeans.fit(X)
    SEE.append(kmeans.inertia_)
    print(kmeans.inertia_)
400.0000000000001
156.41033617954912
44.05704845329278
36.85585101890133
30.98979911778676
25.240846029379913
22.70858764162133
19.8502123581499
17.33673390955418
In [31]:
plt.plot(index,SEE)
plt.xlabel('K')
plt.ylabel('SEE')
Out[31]:
Text(0, 0.5, 'SEE')
In [ ]:
 

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...