- 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.
推薦閱讀
- C# 7 and .NET Core Cookbook
- Software Defined Networking with OpenFlow
- 新一代通用視頻編碼H.266/VVC:原理、標(biāo)準(zhǔn)與實(shí)現(xiàn)
- Web Scraping with Python
- Java開發(fā)入行真功夫
- The DevOps 2.4 Toolkit
- Natural Language Processing with Java and LingPipe Cookbook
- C++ Fundamentals
- 移動(dòng)增值應(yīng)用開發(fā)技術(shù)導(dǎo)論
- PyQt編程快速上手
- Splunk Essentials
- Python深度學(xué)習(xí)入門:從零構(gòu)建CNN和RNN
- C++標(biāo)準(zhǔn)庫(第2版)
- AngularJS by Example
- Visual FoxPro程序設(shè)計(jì)(第二版)