3.3 Density- Based Spatial Clustering of Application with Noise
3.3 Density- Based Spatial Clustering of Application with Noise
3.3.1 Data Set Moons
3.3.1 Data Set Moons
In[]:=
session=StartExternalSession["Python"]
Out[]=
ExternalSessionObject
Test
In[]:=
5+6
Out[]=
11
In[]:=
from sklearn.datasets import make_moons
X, y =make_moons(n_samples=200,noise=0.05,random_state=0)
X, y =make_moons(n_samples=200,noise=0.05,random_state=0)
In[]:=
X
Out[]=
NumericArray
In[]:=
Normal[%]
Out[]=
{{0.816805,0.521645},{1.6186,-0.379829},{-0.0212695,0.273728},{-1.02181,-0.0754398},{1.76655,-0.170699},{1.88203,-0.0423845},{0.974816,0.209994},{0.887988,-0.489367},{0.898652,0.366378},{1.11639,-0.534604},{-0.3638,0.827902},{0.247024,-0.238567},{1.81659,-0.130884},{1.21639,-0.406858},{-0.82367,0.642352},{0.980656,0.208508},{0.540972,0.886168},{0.348031,-0.301014},{0.351815,0.888278},{-0.777086,0.822539},{1.92591,0.0121415},{0.860967,-0.476532},{0.199907,0.996724},{1.28953,-0.370711},{-0.278476,1.02538},{0.241879,-0.0762781},{1.84989,-0.0977367},{1.88407,0.0449402},{0.165822,-0.0861313},{0.138614,0.89639},{0.89087,0.522659},{-0.228066,0.840919},{0.982792,-0.464578},{0.0423775,0.194579},{0.764226,0.672233},{1.91109,0.211783},{0.436084,-0.230072},{0.961869,0.0992343},{-0.843367,0.524143},{-0.0412247,0.357219},{0.555077,-0.424933},{-0.438829,0.859404},{0.653265,0.712354},{0.102748,0.0672141},{1.54868,-0.340122},{-0.373184,0.955064},{1.01707,0.1921},{-0.719237,0.654767},{0.161358,-0.10772},{0.86434,-0.459457},{-0.697175,0.801337},{0.327912,-0.19619},{1.98047,0.0384868},{-0.904798,0.0572394},{1.04515,-0.500203},{0.753421,0.65688},{0.549686,0.736357},{1.24038,-0.475779},{0.249189,0.942462},{-0.207561,0.992906},{0.351364,-0.290654},{-1.01629,0.162902},{1.78137,-0.124493},{0.874238,0.530653},{1.09998,-0.467338},{-1.07023,0.236545},{-0.158699,1.01497},{1.46569,-0.380898},{0.0302521,0.977921},{-0.936594,0.456749},{0.660383,-0.465762},{-0.991447,0.406621},{0.463398,-0.466054},{-0.132006,0.524472},{0.81567,-0.428216},{-0.948209,0.377171},{0.053002,0.185974},{0.926486,0.40989},{0.6069,0.782793},{0.729614,-0.372153},{1.9796,0.124254},{-0.020539,0.976016},{0.638184,-0.499168},{2.00639,0.445976},{0.0231554,0.240357},{-0.358839,1.02717},{0.954147,0.0417743},{-0.339215,0.963089},{0.599505,-0.397749},{1.9902,0.3936},{0.331257,0.936578},{0.994604,0.350634},{1.98845,0.262836},{-0.674737,0.764197},{2.00751,0.365117},{1.78298,-0.114904},{1.73617,-0.227816},{0.406462,-0.254229},{-1.02505,0.243374},{0.064143,0.0775979},{1.30092,-0.580898},{1.97426,0.308899},{0.0322839,1.07938},{1.03086,-0.0238908},{-0.900625,0.306536},{0.0806856,0.291314},{-0.988078,0.103976},{-0.473944,0.961432},{1.54652,-0.350085},{0.233325,0.89649},{-0.584817,0.80319},{0.0374878,1.02322},{-0.0194322,1.07001},{-0.853237,0.398969},{0.926355,0.376953},{1.43251,-0.50149},{0.606228,0.662295},{1.94402,0.136856},{0.579844,-0.398689},{0.743175,0.509983},{0.871167,0.541052},{-0.710457,0.572819},{-0.0308157,0.336446},{-0.0298505,0.995531},{-0.0631335,0.421942},{-0.792232,0.683542},{0.920984,0.0417105},{0.177944,0.0453689},{1.34935,-0.394165},{1.98387,0.508984},{1.00105,0.271585},{-0.542542,0.762576},{-0.996901,0.472264},{0.234085,-0.153817},{1.21437,-0.40862},{1.60102,-0.179407},{1.15844,-0.404086},{-1.00923,0.216136},{2.01866,0.503134},{0.888399,0.390171},{0.101709,-0.0120648},{-0.0124197,0.470649},{0.445665,0.94596},{-0.356934,0.983192},{-0.43845,0.883742},{1.01534,0.0668747},{0.231061,0.0115349},{1.35099,-0.445205},{0.254234,1.02055},{-0.00586456,0.249196},{0.475285,-0.370284},{1.68072,-0.347753},{0.845643,0.456296},{0.342188,0.906139},{0.587414,-0.350787},{-0.178183,0.966415},{1.25866,-0.474001},{0.335428,-0.180233},{0.526308,0.948761},{0.642405,0.777171},{0.157703,0.0470942},{1.11179,-0.506528},{0.603709,0.837599},{1.48247,-0.32722},{0.397934,-0.368766},{1.67241,-0.0932804},{0.475513,0.855473},{0.706051,-0.422419},{1.56419,-0.348606},{0.940129,-0.575089},{0.614003,0.838338},{-1.0714,0.0266932},{-0.91309,0.526264},{-0.748245,0.518237},{0.146882,0.0297201},{0.94362,-0.448294},{1.8449,0.406019},{-0.668273,0.690857},{-0.736242,0.599519},{0.601465,0.725517},{1.47438,-0.37541},{-0.8876,0.508645},{1.92892,0.182018},{1.78673,-0.274707},{1.9513,0.265745},{0.334717,0.980571},{-0.168847,0.892064},{0.77064,-0.517503},{-0.887005,0.366964},{-0.628865,0.790872},{-0.930068,0.387549},{0.424479,0.932688},{0.808614,0.535999},{0.940009,0.271114},{-0.0160918,0.373696},{-0.536334,0.860268},{1.88282,0.244356},{0.175752,-0.007231},{0.124236,1.0079},{1.62153,-0.223285}}
In[]:=
trainX=%;
In[]:=
y
Out[]=
NumericArray
In[]:=
Normal[%]
Out[]=
{0,1,1,0,1,1,0,1,0,1,0,1,1,1,0,0,0,1,0,0,1,1,0,1,0,1,1,1,1,0,0,0,1,1,0,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,1,1,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,0,0,1,1,0,0,1,0,1,1,1,1,0,1,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,1,1,0,0,0,1,0,1,0,0,1,1,1,0,0,0,1,1,1,1,0,1,0,1,1,0,0,0,0,1,1,0,1,1,1,0,0,1,0,1,1,0,0,1,1,0,1,1,1,0,1,1,1,0,0,0,0,1,1,1,0,0,0,1,0,1,1,1,0,0,1,0,0,0,0,0,0,1,0,1,1,0,1}
In[]:=
clusters=%;
In[]:=
total=MapThread[{#1,#2}&,{trainX,clusters}];
In[]:=
clust1=Select[total,#[[2]]0&];
In[]:=
clust2=Select[total,#[[2]]1&];
In[]:=
pclust1=Map[#[[1]]&,clust1];
In[]:=
pclust2=Map[#[[1]]&,clust2];
In[]:=
p0=ListPlot[{pclust1,pclust2},PlotStyle{Pink,Green}]
Out[]=
In[]:=
c=FindClusters[trainX,2,Method->"KMeans"];
In[]:=
ListPlot[{c[[1]],c[[2]]}]
Out[]=
In[]:=
Needs["HierarchicalClustering`"]
In[]:=
DendrogramPlot[trainX,TruncateDendrogram10,HighlightLevel2]
Out[]=
In[]:=
XN=MapThread[(#1ToString[#2])&,{trainX,Range[Length[trainX]]}];
In[]:=
c=FindClusters[XN,2]
In[]:=
from sklearn.cluster import DBSCAN
db = DBSCAN(eps=0.2, min_samples=5,metric='euclidean')
db = DBSCAN(eps=0.2, min_samples=5,metric='euclidean')
In[]:=
y_db=db.fit_predict(X)
In[]:=
y_db
3.3.2 Segmentation of MRI of Brain
3.3.2 Segmentation of MRI of Brain