- Haskell Data Analysis Cookbook
- Nishant Shukla
- 205字
- 2021-12-08 12:43:36
Implementing a frequency table using Data.MultiSet
A frequency map of values is often useful to detect outliers. We will use an existing library that does much of the work for us.
Getting ready
We will be using the multiset
package from Hackage:
$ cabal install multiset
How to do it...
Create a new file, which we will call Main.hs
, and perform the following steps:
- We will use the
fromList
andtoOccurList
functions fromData.MultiSet
:import Data.MultiSet (fromList, toOccurList)
- Define a simple data type for colors:
data Color = Red | Green | Blue deriving (Show, Ord, Eq)
- Create a list of these colors:
main :: IO () main = do let items = [Red, Green, Green, Blue, Red, Green, Green]
- Implement the frequency map and print it out:
let freq = toOccurList . fromList $ items print freq
- Run the code to display the frequency list:
$ runhaskell Main.hs [ (Red, 2), (Green, 4), (Blue, 1) ]
How it works...
The toOccurList :: MultiSet a -> [(a, Int)]
function creates a frequency map from a list. We construct MuliSet
using the provided fromList
function.
See also
If importing a new library is not desired, see the previous recipe on Implementing a frequency map using Data.List.
推薦閱讀
- OpenCV 3和Qt5計(jì)算機(jī)視覺(jué)應(yīng)用開(kāi)發(fā)
- Django:Web Development with Python
- Blockly創(chuàng)意趣味編程
- Python自然語(yǔ)言處理(微課版)
- Visual Basic程序設(shè)計(jì)與應(yīng)用實(shí)踐教程
- SQL經(jīng)典實(shí)例(第2版)
- Visual FoxPro程序設(shè)計(jì)習(xí)題集及實(shí)驗(yàn)指導(dǎo)(第四版)
- Visual Basic程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)(第二版)
- Service Mesh實(shí)戰(zhàn):基于Linkerd和Kubernetes的微服務(wù)實(shí)踐
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition
- Python計(jì)算機(jī)視覺(jué)與深度學(xué)習(xí)實(shí)戰(zhàn)
- Ubuntu Server Cookbook
- HTML5+CSS3+jQuery Mobile+Bootstrap開(kāi)發(fā)APP從入門到精通(視頻教學(xué)版)
- Building Microservices with Go