import Data.Array
import System.IO
import System.Random (randomRIO)
type Rating = Maybe Int
targetRatingsMatrix = [[4,5,4,4,4,4],[3,4,5,4,3,4],[1,2,3,5,5,5],[3,4,5,5,5,4],[4,4,5,5,4,3],[5,5,1,2,1,2]]
-- 2 3
transpose:: [[a]]->[[a]]
transpose ([]:_) = []
transpose x = (map head x) : transpose (map tail x)
mmult :: Num a => [[a]] -> [[a]] -> [[a]]
mmult a b = [[ sum $ zipWith (*) ar bc | bc <- (transpose b)] | ar <- a ]
makeZeroMatrix :: Int -> Int -> [[Int]]
makeZeroMatrix a b = replicate a (replicate b 0)
randomList :: Int -> IO([Int])
randomList 0 = return []
randomList n = do
r <- randomRIO (1,6)
rs <- randomList (n-1)
return (r:rs)
matrixSize :: [[Int]] -> (Int, Int)
matrixSize a = (sum $ map length a, length $ map length a)
splitEvery _ [] = []
splitEvery n list = first : (splitEvery n rest)
where
(first,rest) = splitAt n list
randomMatrixHelper :: [[Int]] -> IO([Int]) -> IO([[Int]])
randomMatrixHelper a b = do
b' <- b
let size = fst (matrixSize a)
let a' = concat a
let ab = add a' b'
return $ splitEvery (fst (matrixSize a)) ab
where
add :: [Int] -> [Int] -> [Int]
add x y = [a+b|a<-x,b<-y]
makeRandomMatrix :: Int -> Int -> IO([[Int]])
makeRandomMatrix n m = do
let a = makeZeroMatrix n m
let b = randomList (fst $ matrixSize a)
c <- randomMatrixHelper a b
return c
collaborative filtering recommender system
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.