kmeans-par 1.5.0 → 1.5.1
raw patch · 4 files changed
+71/−26 lines, 4 filesdep +QuickCheckdep +hspecPVP ok
version bump matches the API change (PVP)
Dependencies added: QuickCheck, hspec
API changes (from Hackage documentation)
+ Algorithms.Lloyd.Sequential: step :: Metric a => (Vector Double -> a) -> Vector Cluster -> Vector Point -> Vector Cluster
+ Algorithms.Lloyd.Strategies: step :: Metric a => (Vector Double -> a) -> Vector Cluster -> Vector (Vector Point) -> Vector Cluster
Files
- kmeans-par.cabal +62/−16
- src/Algorithms/Lloyd/Sequential.lhs +6/−9
- src/Algorithms/Lloyd/Strategies.lhs +2/−1
- test/Spec.hs +1/−0
kmeans-par.cabal view
@@ -1,5 +1,5 @@ name: kmeans-par-version: 1.5.0+version: 1.5.1 synopsis: Sequential and parallel implementations of Lloyd's algorithm. license: MIT license-file: LICENSE@@ -10,20 +10,66 @@ cabal-version: >=1.10 library- exposed-modules: Algorithms.Lloyd.Sequential,- Algorithms.Lloyd.Strategies- other-modules: Data.Functor.Extras,- Data.Vector.Split- other-extensions: ViewPatterns - build-depends: base == 4.*, vector, semigroups, parallel, metric- hs-source-dirs: src- ghc-options: -threaded -O2 -with-rtsopts=-N -rtsopts- default-language: Haskell2010+ exposed-modules:+ Algorithms.Lloyd.Sequential,+ Algorithms.Lloyd.Strategies+ other-modules:+ Data.Functor.Extras,+ Data.Vector.Split+ other-extensions:+ ViewPatterns + build-depends:+ base == 4.*,+ vector,+ semigroups,+ parallel,+ metric+ hs-source-dirs:+ src+ ghc-options:+ -threaded -O2 -with-rtsopts=-N -rtsopts+ default-language:+ Haskell2010 benchmark kmeans-benchmark- type: exitcode-stdio-1.0- main-is: Main.lhs- hs-source-dirs: benchmark- build-depends: base == 4.*, random, criterion, normaldistribution, kmeans-par, deepseq, vector, metric- ghc-options: -threaded -O2 -with-rtsopts=-N -rtsopts- default-language: Haskell2010+ type:+ exitcode-stdio-1.0+ main-is:+ Main.lhs+ hs-source-dirs:+ benchmark+ build-depends:+ base == 4.*,+ random,+ criterion,+ normaldistribution,+ kmeans-par,+ deepseq,+ vector,+ metric+ ghc-options:+ -threaded -O2 -with-rtsopts=-N -rtsopts+ default-language:+ Haskell2010++test-suite spec+ type: + exitcode-stdio-1.0+ hs-source-dirs:+ src,+ test+ main-is:+ Spec.hs+ build-depends:+ base == 4.*,+ hspec >= 1.5,+ normaldistribution,+ metric,+ parallel,+ semigroups,+ vector,+ QuickCheck+ default-language:+ Haskell2010+ ghc-options:+ -threaded -O2 -with-rtsopts=-N -rtsopts
src/Algorithms/Lloyd/Sequential.lhs view
@@ -11,7 +11,8 @@ > PointSum(..), > makeNewClusters, > assign,-> assignPS+> assignPS,+> step > )where > > import Prelude hiding (zipWith, map, foldr, replicate, length, zip, head)@@ -102,8 +103,8 @@ > return (cluster, point `d` centroid cluster) > > assign :: Metric a => (Vector Double -> a) -> Vector Cluster -> Vector Point -> Vector (Vector Point)-> assign metric clusters points = let nc = length clusters in create $ do-> vector <- MV.replicate nc empty+> assign metric clusters points = create $ do+> vector <- MV.replicate (length clusters) empty > points `forM_` \point -> do > let cluster = closestCluster metric clusters point > position = identifier cluster@@ -118,20 +119,16 @@ > > makeNewClusters :: Vector PointSum -> Vector Cluster > makeNewClusters vector = do-> (pointSum@(PointSum count _), index) <- zip vector $ fromList [0..length vector]-> guard $ count > 0 -- We don't want an empty PointSum: amongst other -> -- things, this'd lead to division by zero when -> -- computing the centroid.+> (pointSum, index) <- zip vector $ fromList [0..length vector] > return $ toCluster index pointSum > > step :: Metric a => (Vector Double -> a) -> Vector Cluster -> Vector Point -> Vector Cluster > step = makeNewClusters ...: assignPS-> The algorithm consists of iteratively finding the centroid of each existing cluster, then reallocating points according to which centroid is closest, until convergence. As the algorithm isn't guaranteed to converge, we cut execution if-convergence hasn't been observed after eighty iterations:+convergence hasn't been observed some provided number of iterations: > newtype ExpectDivergent = ExpectDivergent { expectDivergent :: Int } >
src/Algorithms/Lloyd/Strategies.lhs view
@@ -10,7 +10,8 @@ > Cluster(..), > ExpectDivergent(..), > Partitions(..),-> kmeans+> kmeans,+> step > ) where > > import Prelude hiding (zipWith, foldr1, map)
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}