packages feed

kmeans-vector 0.3 → 0.3.1

raw patch · 4 files changed

+39/−61 lines, 4 filesdep +probablenew-component:exe:kmeans-examplePVP ok

version bump matches the API change (PVP)

Dependencies added: probable

API changes (from Hackage documentation)

Files

Math/KMeans.hs view
@@ -2,7 +2,7 @@  {- | Module      :  Math.KMeans-Copyright   :  (c) Alp Mestanogullari, Ville Tirronen, 2011-2014+Copyright   :  (c) Alp Mestanogullari, Ville Tirronen, 2011-2015 License     :  BSD3 Maintainer  :  Alp Mestanogullari <alpmestan@gmail.com> Stability   :  experimental@@ -152,13 +152,14 @@      -- centroidsOf :: Clusters a -> Centroids     centroidsOf cs = G.map centroidOf cs-      where -        n = fromIntegral $ G.length cs+      where          centroidOf (Cluster elts) =              V.map (/n)            . L.foldl1' addCentroids           $ map extract elts++          where n = fromIntegral (length elts)      -- pairToClosestCentroid :: Centroids -> a -> (Int, a)     pairToClosestCentroid cs a = (minDistIndex, a)
+ examples/example.hs view
@@ -0,0 +1,30 @@+import Control.Applicative+import Control.Monad+import Math.KMeans+import Math.Probable++import qualified Data.Vector.Unboxed as V+import qualified Data.Vector         as G++runKMeans :: [V.Vector Double] -> Clusters (V.Vector Double)+runKMeans = kmeans id euclidSq 2++oneVecOf :: RandT IO Double -> RandT IO (V.Vector Double)+oneVecOf doubleGen = vectorOf 10 doubleGen++doubleGen1 :: RandT IO Double+doubleGen1 = normal (-1500) 0.1++doubleGen2 :: RandT IO Double+doubleGen2 = normal 1500 0.1++main :: IO ()+main = do+    v1s <- mwc $ listOf 500 (oneVecOf doubleGen1)+    v2s <- mwc $ listOf 500 (oneVecOf doubleGen2)+    let input = v1s ++ v2s++    let clusters = runKMeans input+    G.mapM_ print clusters+    putStrLn $ show (G.length clusters)+            ++ " cluster(s) found."
− examples/persons.hs
@@ -1,53 +0,0 @@-import Control.Applicative-import Control.Monad-import Math.KMeans-import Test.QuickCheck--import qualified Data.Vector.Unboxed as V-import qualified Data.Vector         as G--data Person = Person -    { age    :: Int-    , weight :: Double-    , name   :: String-    , salary :: Int-    } deriving (Eq)--instance Show Person where-    show p = "<" ++ name p ++ ", " -          ++ show (weight p) ++ "kg, " -          ++ show (salary p) ++ "€/month, "-          ++ show (age p) ++ "y.o>"--instance Arbitrary Person where-    arbitrary = do-        Person <$> choose (2, 100)-               <*> choose (5, 150)-               <*> pure "francis"-               <*> choose (500, 100000)--persons :: Gen [Person]-persons = vector 5--d :: Distance-d v1 v2 = V.sum $ V.zipWith (\x1 x2 -> abs (x1 - x2)) v1 v2--personToVec :: Person -> V.Vector Double-personToVec p = V.fromList -    [ fromIntegral $ age p -    , weight p -    , fromIntegral $ salary p-    ]--runKMeans :: [Person] -> Clusters Person-runKMeans = kmeans personToVec d 2--main :: IO ()-main = do-    ps <- generate persons-    print ps--    let clusters = runKMeans ps-    putStrLn $ show (G.length clusters)-            ++ " cluster(s) found."-    G.mapM_ print clusters
kmeans-vector.cabal view
@@ -1,5 +1,5 @@ Name:                kmeans-vector-Version:             0.3+Version:             0.3.1 Synopsis:            An implementation of the kmeans clustering algorithm based on the vector package Description:         Provides a simple (but efficient) implementation of the k-means clustering algorithm. The goal of this algorithm is to, given a set of n-dimensional points, regroup them in k groups, such that each point gets to be in the group to which it is the closest to (using the 'center' of the group).                      .@@ -14,7 +14,7 @@ License-file:        LICENSE Author:              Alp Mestanogullari <alpmestan@gmail.com>, Ville Tirronen Maintainer:          Alp Mestanogullari <alpmestan@gmail.com>-Copyright:           2011-2014 Alp Mestanogullari+Copyright:           2011-2015 Alp Mestanogullari Stability:	         Experimental Category:            Math Build-type:          Simple@@ -26,11 +26,11 @@   ghc-prof-options:  -prof -auto-all   ghc-options: 	     -O2 -funbox-strict-fields -Wall -executable kmeans-persons-  main-is:           persons.hs+executable kmeans-example+  main-is:           example.hs   hs-source-dirs:    examples   ghc-options:       -O2 -funbox-strict-fields-  build-depends:     base >= 4 && < 5, vector >= 0.7, kmeans-vector, QuickCheck+  build-depends:     base >= 4 && < 5, vector >= 0.7, kmeans-vector, probable  benchmark bench   main-is:           bench.hs