From c799b5aaa03481d057e7c154ec207ad6b030794b Mon Sep 17 00:00:00 2001 From: Benjamin Deonovic Date: Thu, 2 Apr 2015 14:41:33 -0500 Subject: [PATCH 1/2] Implementation of meanshift --- src/Clustering.jl | 4 ++ src/meanshift.jl | 153 ++++++++++++++++++++++++++++++++++++++++++++++ test/meanshift.jl | 42 +++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 src/meanshift.jl create mode 100644 test/meanshift.jl diff --git a/src/Clustering.jl b/src/Clustering.jl index 9085d023..097b6496 100644 --- a/src/Clustering.jl +++ b/src/Clustering.jl @@ -38,6 +38,9 @@ module Clustering # dbscan DbscanResult, dbscan, + # meanshift + meanshift, MeanShiftResult, modedetect, + # silhouette silhouettes, @@ -54,6 +57,7 @@ module Clustering include("kmedoids.jl") include("affprop.jl") include("dbscan.jl") + include("meanshift.jl") include("silhouette.jl") include("varinfo.jl") diff --git a/src/meanshift.jl b/src/meanshift.jl new file mode 100644 index 00000000..3464118b --- /dev/null +++ b/src/meanshift.jl @@ -0,0 +1,153 @@ + +type MeanShiftResult{T <: FloatingPoint} <: ClusteringResult + centers::Matrix{T} # cluster centers (d x k) + assignments::Vector{Int64} # assignments (n) + counts::Vector{Int64} # number of samples assigned to each cluster (k) + bandwidth::Vector{Float64} # bandwidth (by default, 10 percent of the data range) + scaled::Bool # whether data is scaled or not + scaledby::Vector{T} # scaling factors (d) +end + +#1-d profile +function g1{T <: FloatingPoint}(xi::Vector{T}, x::T, h::Float64) + (1/2)*exp(-1/2*((x-xi)/h).^2) +end +#multi-d profile +function gd{T <: FloatingPoint}(Xi::Matrix{T},x::Vector{T},h::Vector{Float64}) + d = size(x,1) + k = ones(T,size(Xi,1)) + for j in 1:d + @inbounds k = k .* g1(Xi[:,j],x[j],h[j]) + end + return k +end + +# mean shift base function +function ms{T <: FloatingPoint}(X::Matrix{T}, x::Vector{T}, h::Vector{Float64}) + d = size(X,2) + g = gd(X,x,h) + + result = zeros(T,d) + @inbounds @simd for j in 1:d + result[j] = sum( X[:,j].*g)/sum(g) + end + return result +end + +function ms{T <: Real}(X::Matrix{T}, x::Vector{T}, h::Float64) + d = size(x,1) + ms(X,x,Float64[h for j in 1:d]) +end + +function msrep{T <: FloatingPoint}(X::Matrix{T}, x::Vector{T}, h::Vector{Float64}; threshold::Float64 = eps(), iter::Int64 = 200) + s = 0 + thresholds = Float64[threshold for j in 1:iter] + th = zeros(iter) + m = x + n,d = size(X) + for j in 1:iter + m = ms(X,x,h) + th[j] = sqeuclidean(m, x)/sqeuclidean(x,zeros(d)) + if th[j] < threshold + s = j + break + end + x = m + end + m +end + +function msrep{T <: FloatingPoint}(X::Matrix{T}, x::Vector{T}, h::Float64; threshold::Float64 = eps(), iter::Int64 = 200) + d = size(X,2) + msrep(X,x,Float64[h for j in 1:d], threshold=threshold,iter=iter) +end + +#mean shift clustering +function meanshift{T <: FloatingPoint}( + X::Matrix{T}, + h::Vector{T}; + subset::Vector{Int64}=[1:size(X,1);], + threshold1::Float64 = 0.0001, + threshold2::Float64 = sqrt(eps()), + scaled::Bool = true, + iter::Int64=200) + + n,d = size(X) + + all(1 .<= subset .<= n) ? nothing : error("subset must be values in [1:n]") + + #range of data + s1 = ones(T,d) + if scaled + s1 = mapslices( (x) -> spannorm_dist(x,zeros(size(x,1))), X ,1)[:] + + #scale data to lie by its range + X = mapslices( (x) -> x./s1, X ,2) + end + + finals = zeros(T,d,n) + ncluster = 0 + + savecluster = zeros(T,d,0) + clusterlabel = zeros(Int64,n) + counts = Array(Int64,0) + + for i in subset + finals[:,i] = msrep(X,X[i,:][:],h,threshold=threshold2,iter=iter) + clusterdist = zeros(ncluster) + if ncluster >= 1 + for j in 1:ncluster + @inbounds clusterdist[j] = sqeuclidean(savecluster[:,j],finals[:,i])/sqeuclidean(savecluster[:,j],zeros(d)) + end + end + if ncluster == 0 || minimum(clusterdist) > threshold1 + ncluster += 1 + push!(counts,1) + savecluster = hcat(savecluster,finals[:,i]) + clusterlabel[i] = ncluster + else + clst = indmin(clusterdist) + clusterlabel[i] = clst + counts[clst] += 1 + end + end + MeanShiftResult(savecluster, clusterlabel, counts, h, scaled, s1) +end + +function meanshift{T <: FloatingPoint}( + X::Matrix{T}, + h::T; + subset::Vector{Int64}=[1:size(X,1);], + threshold1::Float64 = 0.0001, + threshold2::Float64=sqrt(eps()), + scaled::Bool = true, + iter::Int64=200) + + d = size(X,2) + meanshift(X,Float64[h for j in 1:d],subset=subset,threshold1=threshold1,threshold2=threshold2,scaled=scaled,iter=iter) +end + +function meanshift{T <: FloatingPoint}( + X::Matrix{T}; + subset::Vector{Int64}=[1:size(X,1);], + threshold1::Float64 = 0.0001, + threshold2::Float64=sqrt(eps()), + scaled::Bool = true, + iter::Int64=200) + + n,d = size(X) + + s1 = mapslices( (x) -> spannorm_dist(x,zeros(n)), X ,1)[:] + h = scaled ? Float64[0.1 for j in 1:d] : s1/10 + meanshift(X,h,subset=subset,threshold1=threshold1,threshold2=threshold2,scaled=scaled,iter=iter) +end + +function modedetect{T <: FloatingPoint}(x::Vector{T}; maxsamples::Int64=1000) + d = size(x,1) + subset = [1:d;] + if maxsamples < d + subset = sample(1:d,maxsamples,replace=false) + end + meanshift(hcat(x,x),subset=subset,scaled=false).centers'[:,1] +end + diff --git a/test/meanshift.jl b/test/meanshift.jl new file mode 100644 index 00000000..38abcd58 --- /dev/null +++ b/test/meanshift.jl @@ -0,0 +1,42 @@ +using Clustering +using Base.Test + +#= +using Distributions +using RDatasets + +d=MixtureModel( Normal[Normal(-2,1.0),Normal(5,1.0),Normal(10,1.0)], [0.2,0.3,0.5]) +x=rand(d,1000) + +dat = dataset("datasets","faithful") +faithful = array(hcat(dat[:,:Eruptions],dat[:,:Waiting])) + +dat = dataset("datasets","iris") +iris = hcat(dat[:,:SepalLength],dat[:,:SepalWidth],dat[:,:PetalLength],dat[:,:PetalWidth]) +=# + +srand(1234) + +x = [-2.9029142938652415,-2.2717353960346207,9.98070821831015,5.110096126322175,3.496570542648949,8.893270086474423,-1.2307217394654177,10.997317155657504,5.521272656468051,-3.541426383947246,4.684562753901671,-2.4084380321926506,4.458284280245628,5.514836166949132,9.950049768696639,4.4772278498840175,9.603789315311337,12.06353247264456,10.170777946172779,5.7993351658657595,10.19960913912461,5.818709875106646,4.683613221588931,10.07995136571587,9.4787709447846,9.089195233992333,11.306683767463253,5.1043290923122715,4.113462484909875,10.01155085481153,10.238284072727254,5.4293690788240685,-1.8206314916972315,10.727227387859264,-2.3316488258685966,9.360770603514055,-2.256064509386966,9.919860416609223,9.509990622782775,-1.1402799727876487,11.486660150515913,5.793146520537967,3.8267076135045284,4.937032586547668,9.912284124991398,9.179606688998946,7.381450352258975,9.282486530947677,10.823597455404567,-2.2057815126337648,4.507746594812058,10.176156218897512,-0.13999831228951742,4.801783395921873,-3.6715750451349987,4.650077540461158,10.428574767832242,9.595559139616828,-2.548548899769181,4.0529148950948874,8.595843241356356,-1.8722533722498986,5.722479828349236,10.084916221706953,5.249425791938789,10.62046393827771,5.739090105457539,11.973441511742559,8.37752209252133,3.4245709961451007,-1.7499355034571404,8.875704061719048,11.257336098741519,4.583881193806511,5.183202636363743,10.885631809227545,9.253152305906935,4.423105718834383,8.72451254275298,5.079202320884537,3.5756019922750477,-0.28150395344403334,-1.9337610402028351,9.961629292990274,9.3648997414345,4.742437301173768,10.196089315078975,-1.84130801453557,10.2474090512196,10.57340909763312,9.824857457016861,11.649887693802851,3.9133693258618694,4.016669923108883,5.287621780579473,-1.7875014312714084,9.955799617677464,10.233165598069837,-2.2612647356824156,9.1664934651544,10.073535212084982,9.670041600646906,3.8492847412255,9.75176463587086,3.754552306639063,9.615369874633245,4.888880463075803,9.618896641572102,-1.2684926333557587,4.68631127292892,4.53192330951416,-1.22861316082047,-1.982737678657977,7.566340921843622,9.089981640986663,9.118774672300173,-3.4752489489038814,6.178457695758667,11.103466001875182,6.448105952005066,-2.5481890582159057,10.855194389525664,10.25082709294611,9.976454383920654,-2.972171637079988,4.076934076995844,4.629646178510925,9.03180546659077,8.952064294476262,-2.1876173689483065,5.965147122759522,9.41260768347256,-1.4381574651403493,4.547726053846362,4.4849835836390195,-1.7837842625196623,2.1601875872914853,-2.179357688659204,4.629974974941222,9.360447124449106,4.294474564958372,3.40937897298829,7.431956787676102,-1.1681648912674458,5.77733525327017,9.369745260084054,10.476766913445562,-1.3256992065913118,9.78681962231385,-2.546418839745622,11.407810940823818,-2.8254692945257833,3.4586446735093923,11.850893286345649,9.441136501424504,11.478147833152192,-3.62477957937758,8.722904361144845,9.450911186743589,9.251733125397932,9.90209698579686,-1.5103288602636664,9.476634543070901,11.048549383689776,10.069366643077396,11.268734961129072,-1.9634727562187277,10.677353566807572,10.039428373282144,10.96811621327428,11.878118761239097,5.226960209653529,5.145355419843534,10.59211273928435,5.809245193446498,7.083642120079918,6.488524953487714,4.944945738010841,10.849792441981602,6.864681412873264,8.295151157754772,4.835159605675863,6.804251214576671,12.07408683623668,4.496216257360725,8.739826015356181,-2.3177929352502,-3.2950912451895125,3.1636038489052307,10.863610583759545,10.457144246321828,4.4795500904771846,10.297642724913514,5.89691589437983,10.12178748719461,6.804845351203177,8.21777900390575,9.63755492104692,11.062489809227074,9.505318601038185,4.7580269643728315,9.325973785481107,10.803949554157812,11.751569432774156,5.1430041877748485,9.026272557991213,5.56199504456894,8.814236077627706,-0.9719725845279057,5.301206764065788,10.777490879385622,7.866456697606473,5.121183823718458,11.527978081150206,8.81850246695919,6.223448256116544,5.744665567026424,9.573543769893734,9.053118056075954,5.131861286389623,9.83481253988603,8.524082895664126,10.800501500483675,-3.097512685130007,5.5840074234273205,5.325893061531313,9.449368416561033,6.933773393086056,5.0563022140779,9.045541466815212,-1.0263895083471621,10.243095909737038,3.8233206890167817,6.741113577200024,11.94872892673379,6.553293083342449,4.497407746964158,-3.686241154394687,-0.9428327265163685,9.314036576839808,-3.0844994254585063,-1.5031150413513397,10.80995040816209,9.12391878171329,-1.38570996620169,7.287197906293412,10.12786603478079,-1.0483604804191837,10.941369074061013,10.719938668730393,8.480087880506552,9.683358822475377,4.722136394045618,10.685662975748597,4.52467260519338,-3.0471299338481415,6.330606430744817,-2.7945838565335865,9.675715437107794,11.030273936953675,5.059206427992706,6.094478145840936,-2.1775918323519963,10.69834882437723,8.463681132938634,9.80095663870962,11.548956076918916,-1.718475968861293,-2.6242910320865724,9.075369675549174,10.651408951056773,9.272480874326664,-2.4468280639925926,9.652632229755152,11.601692188437239,-0.8894446022697828,11.07873970292413,-2.0589330410337476,9.94798866902162,-1.4149364296237708,4.251132609339663,9.870991841811856,-1.2231038407228267,11.140343173226093,5.865399157834332,6.154274303591206,4.337882100105277,8.839957013361312,4.757117724332189,3.0853341260163725,-1.3017346154633644,8.022537757276892,9.144690176338635,10.519409074509335,-1.0258042686806548,9.83810355721139,12.391414389033404,5.769802545692433,10.638493176498951,4.27869748548911,9.868999809469106,9.618501877899737,9.355906952440561,-1.5903909248862458,9.21716391690945,10.325119167432025,9.105133885562548,4.31049373782956,-1.2565962629597052,4.439801144914126,5.85887411441769,5.67660448974261,5.705740515586932,8.45161501888418,11.196749848403204,7.918752362075979,-1.7261319840911749,4.93405798367324,10.027085907495323,11.366664526731793,4.651511785574178,-2.1577382237289506,10.08795972216258,4.146152681112058,9.922128582433926,9.569324611432933,9.891400368758404,2.873324395533627,4.08569718313885,10.164821486670924,10.901357557001358,-2.432830536362042,9.476144919936694,5.181352137501046,9.792298946753007,-2.1885107120064937,9.382119484231467,-0.5928276029624964,-3.3497916698740324,-2.7034055591204953,-2.2987309617216996,4.178388252860136,-1.0851988073906753,-1.9772602151908965,3.2881965218574147,-1.660185055567868,-2.3538797767275557,3.7900648190345825,-3.0418910558981533,12.021044300135678,-3.732012874902036,4.993110920411281,2.5558043496583935,5.503272800866196,11.510866844817247,4.015663301721046,9.50913538406485,5.730696359228114,-2.3723137581795752,5.416710842894578,-1.1427533024873546,9.642114154316268,-2.3982682245061167,5.605962137421296,8.053234210228538,9.902685942648704,4.2369871869005715,-2.777334385797554,9.846441935612335,-0.9408190711190723,9.442865064829858,5.170146942484166,-2.7509222321334637,9.878020684665776,-1.6580948221380278,5.63331499060199,10.643282609686706,11.468889466399698,-2.406887989173902,5.6408447832269495,6.203879354788741,11.195507417175545,6.01759498820031,-2.164912548183509,3.338176813909853,3.7465244194064082,6.617775221363561,5.84579405256716,11.134539011128602,4.251491455298106,9.310404532451898,10.121749196323881,-1.3254259235836554,6.630128910095238,10.699979408263713,10.863897344545176,5.516618195725992,10.13260906637329,8.889833821166725,9.580181571634629,4.151574034548232,8.541317433754575,11.525547738459041,6.0329773686368195,10.173952824266465,5.3483331275423085,10.904487335473116,5.43133913441578,-1.1466170759759238,10.833454172537518,7.231889954720838,10.940458272919283,12.254013778359788,10.906171015224961,-1.2161363733268191,4.072434776507392,10.02365116981119,7.988617742367682,4.363613969009179,10.41110756498248,9.50355304893438,8.038015997091247,10.772640754999822,5.212636873348336,-2.3946741066579973,3.1212936399080577,6.494610058289369,-3.2263117761282256,12.712683353987078,9.824820618619562,11.929177123002864,9.395203171831547,10.149577275873918,10.043857151214288,8.459817754330329,9.5261731843288,4.15109056866373,-3.48209975783576,-0.6697828534342256,9.965697377235417,-3.5568295016132008,11.156050133223564,10.496163868718417,-2.8242537741060554,-2.4295768996398963,6.154861754272459,-2.0568415043969743,3.7312856270019212,-2.471767849525986,5.56615479906824,10.126306597636766,9.536171973580506,-1.6939568645511989,9.300274487642902,11.36907505294796,5.560700299958457,-2.7832811997961233,-1.671993524193167,9.279036282931553,9.374315455833472,9.439208154824177,11.018387308873665,-1.240093131838253,10.398719021367166,9.131048533595077,4.136777860260092,-4.179079119691539,9.114013382683028,9.593088247286442,10.36091630843489,3.9727757822455496,9.943074582023932,6.1459836296474695,5.3375852843838,5.754543431267568,10.20055937360612,2.8841588733511117,-3.500502340017581,-1.0667928602042278,5.425721593038799,9.69506179176838,9.077741973108594,12.244584073521862,7.628755431114124,9.329758417835627,10.795893767866156,9.740544523214469,9.952793958887561,9.341179623797421,10.315488521756066,-2.4559013251700934,4.506955866433962,8.289124480209548,11.052365927922818,6.121880131244109,9.702145239465256,-2.321671074165207,9.906846676685221,8.959550295462787,11.95535251080971,9.28591646922207,10.03757024343047,5.996483500074954,9.816732138639932,5.283903913783125,5.443716112169671,-2.125841756839822,-2.170479703563519,10.586955533934495,3.865123045376109,10.550391576647947,10.267174830667722,-3.6542654840080124,11.569526556454868,5.6379112358180725,-3.452842415868735,5.591645548942866,6.393689592411548,5.441144151887744,-2.0371808091612835,-2.284470391535052,-3.1331096793355897,4.037905480393459,8.53803046709823,6.525676315223196,10.0459259723607,-3.2739503521925695,8.540923206105386,8.252276807059246,2.828155178237595,-1.380252273420182,9.078646886599307,10.800926503424112,8.807232493402902,6.012722996841366,10.06089194882441,11.256379331838978,5.065593349644152,9.142364963175767,9.908619192130164,3.6208403818655936,4.254151179895864,-1.956078006409029,10.503009371726948,9.879187158959539,-2.720971247621655,9.634442593324186,7.632776956517075,8.263718996086508,8.298381862352173,8.598567573084976,-3.213367219108378,10.061135028393812,-1.5261408557314695,9.631594481761077,9.150138286757104,5.3444365412312465,4.882585821529331,10.211215010939721,11.082561087222127,-1.6667793034461387,-2.166453421110133,2.65638063235776,10.091338307409869,8.791725450345108,7.25203990439258,10.026222670917369,5.290910297935697,-1.9627759464049503,12.122788747495171,-3.0309241779719285,-0.6639892746822318,10.01069435520723,11.034331920023158,12.120728059850508,9.589689517984565,11.261854235177807,5.0436168911672645,9.662160358571322,10.785145534845961,5.357352821470051,9.68197086352931,4.825037730351957,9.744403221386193,-1.1223903736383816,6.04488276891953,10.125271792478157,-2.3865361765760964,10.842986954173174,8.298986094364594,11.982017113666359,4.552430530602498,10.583642925795408,11.643789794668574,4.523253325537184,-1.6582048514899765,8.654958615296149,-3.341646923813496,5.49749398182794,4.8821545234837735,10.292382162851514,9.82691682460154,4.414373086840388,11.071985764578972,8.763764615537932,5.369026083865705,9.934166427526298,11.851338755748449,9.873952002417493,8.639685160404966,-1.958646763381331,12.476086615182096,-2.7836539336084245,9.497405047786444,-4.527012823456501,8.543932541693751,9.44952222635094,4.233095181115094,5.248273954903292,-1.1802765948881033,-1.8299675175131171,11.060137064581877,3.838176656945657,8.81356029206489,4.768739007902124,9.593953245505645,9.58724392860292,10.57987208385352,5.479049766640571,6.8987213130091405,4.597516811928036,8.645903673760646,-2.176134253637488,9.623131861477498,-2.2243398239772354,9.911267243209975,11.770322430117796,5.022024166201491,5.624956728887817,11.369668181743005,4.387865603199891,10.76916008122514,5.71838743148757,8.387166173916896,6.445701877300734,8.659711785126536,-2.1635933017032207,-2.0267282287979818,10.105342160918275,9.90898446305042,-1.1523243257537454,-1.0920722739865283,10.046415589437494,5.1029776128459154,8.989666385551574,11.457881856235808,8.891736421425932,4.769274452075751,9.321135140846481,-2.0236847847341037,-1.3992326506106596,4.615829962778111,9.404411478627228,11.01621552329923,8.406902174271178,-2.211687566092343,5.638678213028461,10.158211149071299,3.0852220838276825,4.893762958699021,5.306337564609539,10.458788005988708,9.961377959533559,8.96587393054263,10.066742650744983,10.589977444016181,8.914232302575611,-3.1224230952814063,10.220014359311298,10.15988400397827,10.922809393147663,-0.37841698235038046,5.793494594560936,-3.247482628752951,11.804433306693262,6.438321253328375,11.113512267312077,10.029116791411603,5.172941155127493,-1.2045855833416066,-3.6195225557476447,9.460693024169244,-2.241340481485563,6.78093801275053,3.6017559859489165,10.653573085554374,6.216819410638736,5.172380802633599,4.86821506487033,9.121544959156923,9.981985761422521,12.026109668966434,9.676313462919584,7.316083880722651,4.057249123807118,10.171519168747741,9.663862421454297,9.994636463131817,5.722471559023911,4.790378759869048,10.850279653221536,9.957859811509406,10.230508993256484,10.150802403946916,-2.0216184775161525,10.211857619437795,-0.9979220815447065,11.016816447396948,-0.890348150489942,5.200496967517308,5.636145777810936,-1.5941192657230878,12.75271505490056,4.84266882156132,3.7689607851672524,5.019082981590599,9.441698441264801,9.253731910768957,5.503881788578316,9.992509562312552,10.037709305749535,-2.273850355795542,9.658039605723655,10.252259443828263,3.5034453955818714,-2.7841947283575488,9.179148481381207,11.221370814527111,12.016535736980476,9.435530260445363,11.252850731266838,9.538161617576,5.001027364511372,5.008335169860848,4.24423600986613,10.891497508647195,3.31555669309123,10.634524749711689,4.644915482020947,11.300527820627659,9.633151659946968,9.709918415090508,7.376995859705879,4.525872406247267,4.262378843956681,3.004181545703081,-2.9433786307960412,10.074119649333593,-3.618185543162925,10.256858132786475,6.698775730212194,9.918610311816618,8.320124779822333,10.652506237230972,-2.9830705027893227,4.709637095590947,9.90331644506007,6.985572262294638,10.527699887242047,11.759794406699395,9.277860611958422,6.018077444198325,-2.1206084588322636,9.855572244857692,4.9717911150449,9.78447051413844,5.814107227416974,11.072367929393351,-1.3889064974870664,6.664074598249668,-2.05744698095786,10.256490061483534,4.5170639145788005,3.9347705352663587,11.13424545948861,5.048158931902458,8.430827171455437,-0.6710624675032115,9.878509485862578,2.5262173111515676,-1.1834404457802088,9.268862039156579,10.24637277903993,-3.1307800291340087,10.212276236647707,10.432438284201071,10.213676089447688,10.443294086541307,9.401546211464893,5.031190284593338,-3.1573713956919685,6.285464199803592,10.095518147079172,5.472897300630138,9.291099511789612,9.972250655419586,10.609878719534132,-2.748187102758344,9.517870626604186,3.845348881056606,9.871106030298883,4.759345895112487,9.14555087664528,8.555966719125207,11.182493396745137,10.975973000110207,4.838381184259428,11.41559651442525,-1.6551005609902814,-2.558046518445782,11.059220243551847,4.017037665143366,10.21919781792215,10.044854687972688,7.884880486233457,11.21944615671242,9.710711691988621,-3.3683800611015764,-2.723930728465199,4.276748408972522,9.836310250649182,10.226142260087835,-2.4428701778131967,10.0140494364322,10.543837350250318,-4.1134426495778555,11.934304719401421,5.890938102016104,6.284502245123283,3.894655960028947,5.959470521074738,4.966362787017081,0.13412073428430782,9.732972999117557,8.982709080393517,-4.227668228101368,-1.7205056475133236,10.693657874276848,9.784931646849204,5.818470150609005,9.82899179873677,10.624426394170746,9.247349494161183,9.93031969333844,-1.369731793525577,-2.2069534288751176,3.486050970762886,4.669776295273101,12.091491090992315,-3.4674063892120994,10.763249976520274,9.535578446163484,5.4064977392832665,10.43686892165395,-1.7475724321453225,2.430568479055931,-2.7574178802813543,12.21576960006697,9.698140622612867,10.296412227631702,9.438381853419306,9.219060894413936,9.982594902562822,9.508501634770099,3.787158474255678,-3.7630436303646593,-1.7319683803143024,9.724064040898554,9.785370571349711,6.120828993392303,8.428826361861304,8.813889005870765,4.88374579561223,-3.2788563559950497,9.986901029337373,10.109343834227635,-2.8830344788472826,-2.2121236625020146,-3.424157067912084,12.278263695693962,10.854418437717746,11.47011448492524,5.935545445490502,10.677880739814107,5.240890385235274,12.144358392258715,-2.111289733771118,4.6531301258227105,-0.9242753446831256,10.097772203821126,8.447429269047507,8.962476996001541,10.645691388271828,4.204478866458271,6.170263083619032,6.141772579052443,4.290198842111958,9.657259329697514,5.208217910590152,10.60610369734477,6.833012866371655,5.221261721268788,6.7993210729969995,4.390372822035889,4.816158838025248,-1.938000822128271,9.307963309392123,11.518879417875667,-0.6622849105165802,3.9615655876278337,11.324682524521942,-1.4278090635262441,4.6382706317500615,10.3861266135167,0.4047821160355256,9.579665967833016,10.716325312400878,10.425523870001387,6.332871685156023,-3.008066702190005,10.274383054354484,4.947398452895163,10.273424131148454,12.09730268474927,4.6077465737536025,4.1689853229686,-3.387855949568472,5.178554949762984,5.220124062825756,8.793932179236124,10.23888524742659,8.48200969468698,9.693779288221657,10.480535556285234,3.877578161362689,6.0468333479553875,10.803953602709655,8.954217069839176,11.146366628256953,5.487574928067736,3.48443762221087,10.473367440606195,5.091673298755278,3.723114108370844,8.912533776187793,9.978703521833308,10.933285442103832,5.328736833134702,10.890694883851316,4.493429207850765,-2.24070416253143,5.957178194413243,5.584769096991644,9.075545733034946,9.26503215024277,4.780479855671216,9.124192248763112,-1.105993793186112,11.551759470076284,9.03358328147316,10.589545238855754,-2.502156223792624,6.469385952488672,11.384271079657292,5.120669568479721,9.475068052367545,9.883450506859191,-4.846973382300861,10.647236868711312,10.31692299571137,-2.1637787122957652,4.710425331142665,10.083818113183696,10.795418192938092,4.390152570363244,6.269912434316406,4.974139855539564,8.870231232775133,3.4993181745030153,5.080365808723284,9.75379993034182,3.9075548717617457,11.919186427171397,-3.0083045876822894,11.137804768446363,8.609478036571234,11.496891658344367,10.301698308971789,9.991321496634137,8.682713263002519] + + +@time m = modedetect(x) +@test isa(m, Vector{Float64}) +@test length(m) == 3 + + +faithful = [3.6 79.0;1.8 54.0;3.333 74.0;2.283 62.0;4.533 85.0;2.883 55.0;4.7 88.0;3.6 85.0;1.95 51.0;4.35 85.0;1.833 54.0;3.917 84.0;4.2 78.0;1.75 47.0;4.7 83.0;2.167 52.0;1.75 62.0;4.8 84.0;1.6 52.0;4.25 79.0;1.8 51.0;1.75 47.0;3.45 78.0;3.067 69.0;4.533 74.0;3.6 83.0;1.967 55.0;4.083 76.0;3.85 78.0;4.433 79.0;4.3 73.0;4.467 77.0;3.367 66.0;4.033 80.0;3.833 74.0;2.017 52.0;1.867 48.0;4.833 80.0;1.833 59.0;4.783 90.0;4.35 80.0;1.883 58.0;4.567 84.0;1.75 58.0;4.533 73.0;3.317 83.0;3.833 64.0;2.1 53.0;4.633 82.0;2.0 59.0;4.8 75.0;4.716 90.0;1.833 54.0;4.833 80.0;1.733 54.0;4.883 83.0;3.717 71.0;1.667 64.0;4.567 77.0;4.317 81.0;2.233 59.0;4.5 84.0;1.75 48.0;4.8 82.0;1.817 60.0;4.4 92.0;4.167 78.0;4.7 78.0;2.067 65.0;4.7 73.0;4.033 82.0;1.967 56.0;4.5 79.0;4.0 71.0;1.983 62.0;5.067 76.0;2.017 60.0;4.567 78.0;3.883 76.0;3.6 83.0;4.133 75.0;4.333 82.0;4.1 70.0;2.633 65.0;4.067 73.0;4.933 88.0;3.95 76.0;4.517 80.0;2.167 48.0;4.0 86.0;2.2 60.0;4.333 90.0;1.867 50.0;4.817 78.0;1.833 63.0;4.3 72.0;4.667 84.0;3.75 75.0;1.867 51.0;4.9 82.0;2.483 62.0;4.367 88.0;2.1 49.0;4.5 83.0;4.05 81.0;1.867 47.0;4.7 84.0;1.783 52.0;4.85 86.0;3.683 81.0;4.733 75.0;2.3 59.0;4.9 89.0;4.417 79.0;1.7 59.0;4.633 81.0;2.317 50.0;4.6 85.0;1.817 59.0;4.417 87.0;2.617 53.0;4.067 69.0;4.25 77.0;1.967 56.0;4.6 88.0;3.767 81.0;1.917 45.0;4.5 82.0;2.267 55.0;4.65 90.0;1.867 45.0;4.167 83.0;2.8 56.0;4.333 89.0;1.833 46.0;4.383 82.0;1.883 51.0;4.933 86.0;2.033 53.0;3.733 79.0;4.233 81.0;2.233 60.0;4.533 82.0;4.817 77.0;4.333 76.0;1.983 59.0;4.633 80.0;2.017 49.0;5.1 96.0;1.8 53.0;5.033 77.0;4.0 77.0;2.4 65.0;4.6 81.0;3.567 71.0;4.0 70.0;4.5 81.0;4.083 93.0;1.8 53.0;3.967 89.0;2.2 45.0;4.15 86.0;2.0 58.0;3.833 78.0;3.5 66.0;4.583 76.0;2.367 63.0;5.0 88.0;1.933 52.0;4.617 93.0;1.917 49.0;2.083 57.0;4.583 77.0;3.333 68.0;4.167 81.0;4.333 81.0;4.5 73.0;2.417 50.0;4.0 85.0;4.167 74.0;1.883 55.0;4.583 77.0;4.25 83.0;3.767 83.0;2.033 51.0;4.433 78.0;4.083 84.0;1.833 46.0;4.417 83.0;2.183 55.0;4.8 81.0;1.833 57.0;4.8 76.0;4.1 84.0;3.966 77.0;4.233 81.0;3.5 87.0;4.366 77.0;2.25 51.0;4.667 78.0;2.1 60.0;4.35 82.0;4.133 91.0;1.867 53.0;4.6 78.0;1.783 46.0;4.367 77.0;3.85 84.0;1.933 49.0;4.5 83.0;2.383 71.0;4.7 80.0;1.867 49.0;3.833 75.0;3.417 64.0;4.233 76.0;2.4 53.0;4.8 94.0;2.0 55.0;4.15 76.0;1.867 50.0;4.267 82.0;1.75 54.0;4.483 75.0;4.0 78.0;4.117 79.0;4.083 78.0;4.267 78.0;3.917 70.0;4.55 79.0;4.083 70.0;2.417 54.0;4.183 86.0;2.217 50.0;4.45 90.0;1.883 54.0;1.85 54.0;4.283 77.0;3.95 79.0;2.333 64.0;4.15 75.0;2.35 47.0;4.933 86.0;2.9 63.0;4.583 85.0;3.833 82.0;2.083 57.0;4.367 82.0;2.133 67.0;4.35 74.0;2.2 54.0;4.45 83.0;3.567 73.0;4.5 73.0;4.15 88.0;3.817 80.0;3.917 71.0;4.45 83.0;2.0 56.0;4.283 79.0;4.767 78.0;4.533 84.0;1.85 58.0;4.25 83.0;1.983 43.0;2.25 60.0;4.75 75.0;4.117 81.0;2.15 46.0;4.417 90.0;1.817 46.0;4.467 74.0] + +fit = meanshift(faithful) +@test isa(fit, MeanShiftResult{Float64}) +@test size(fit.centers) == (2,2) +@test_approx_eq fit.centers [1.2515981864368553 0.5620435287062047; 1.510671013899668 1.0093201201405357] +@test fit.assignments == [1,2,1,2,1,2,1,1,2,1,2,1,1,2,1,2,2,1,2,1,2,2,1,1,1,1,2,1,1,1,1,1,1,1,1,2,2,1,2,1,1,2,1,2,1,1,1,2,1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1,2,1,1,1,2,1,1,2,1,1,2,1,2,1,1,1,1,1,1,2,1,1,1,1,2,1,2,1,2,1,2,1,1,1,2,1,2,1,2,1,1,2,1,2,1,1,1,2,1,1,2,1,2,1,2,1,2,1,1,2,1,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,1,2,1,1,1,2,1,2,1,2,1,1,2,1,1,1,1,1,2,1,2,1,2,1,1,1,2,1,2,1,2,2,1,1,1,1,1,2,1,1,2,1,1,1,2,1,1,2,1,2,1,2,1,1,1,1,1,1,2,1,2,1,1,2,1,2,1,1,2,1,2,1,2,1,1,1,2,1,2,1,2,1,2,1,1,1,1,1,1,1,1,2,1,2,1,2,2,1,1,2,1,2,1,2,1,1,2,1,2,1,2,1,1,1,1,1,1,1,2,1,1,1,2,1,2,2,1,1,2,1,2,1] +@test fit.bandwidth == [0.1,0.1] +@test fit.counts == [175,97] +@test fit.scaled +@test_approx_eq fit.scaledby [3.4999999999999996,53.0] + +@time meanshift(faithful,scaled=false) +@time meanshift(faithful,[0.2,0.2],scaled=true) +@time meanshift(faithful,[4.0,50.0],scaled=false) From 91e12df4b44827da3515c8cc8e663eac300bc52a Mon Sep 17 00:00:00 2001 From: Benjamin Deonovic Date: Fri, 3 Apr 2015 10:46:04 -0500 Subject: [PATCH 2/2] add meanshift tests to runtests.jl --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 8fd19dfb..ca15e3b8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,6 +4,7 @@ tests = ["seeding", "kmedoids", "affprop", "dbscan", + "meanshift", "silhouette", "varinfo"]