Skip to content

Commit

Permalink
My distance metric for vectors was stupid: let's try again
Browse files Browse the repository at this point in the history
  • Loading branch information
batterseapower committed Sep 12, 2008
1 parent fdd4ac5 commit 994b028
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Algorithms/MachineLearning/Framework.hs
Expand Up @@ -2,6 +2,7 @@
-- the machine learning algorithms. -- the machine learning algorithms.
module Algorithms.MachineLearning.Framework where module Algorithms.MachineLearning.Framework where


import Algorithms.MachineLearning.LinearAlgebra
import Algorithms.MachineLearning.Utilities import Algorithms.MachineLearning.Utilities


import Numeric.LinearAlgebra import Numeric.LinearAlgebra
Expand Down Expand Up @@ -126,7 +127,7 @@ instance MetricSpace Double where
distance x y = abs (x - y) distance x y = abs (x - y)


instance MetricSpace (Vector Double) where instance MetricSpace (Vector Double) where
distance = (<.>) distance x y = vectorSumSquares (x - y)


-- --
-- Models -- Models
Expand Down
4 changes: 4 additions & 0 deletions Algorithms/MachineLearning/LinearAlgebra.hs
Expand Up @@ -56,6 +56,10 @@ applyVector fns inputs = fromList $ map ($ inputs) fns
vectorSum :: Element a => Vector a -> a vectorSum :: Element a => Vector a -> a
vectorSum v = constant 1 (dim v) <.> v vectorSum v = constant 1 (dim v) <.> v


-- | The sum of the squares of the elements of the vector
vectorSumSquares :: Element a => Vector a -> a
vectorSumSquares v = v <.> v

-- | Mean of the elements in a vector. -- | Mean of the elements in a vector.
vectorMean :: Element a => Vector a -> a vectorMean :: Element a => Vector a -> a
vectorMean v = (vectorSum v) / fromIntegral (dim v) vectorMean v = (vectorSum v) / fromIntegral (dim v)
Expand Down

0 comments on commit 994b028

Please sign in to comment.