Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Visualizing points using Graphics.EasyPlot


Sometimes, it's convenient to simply visualize data points before clustering or classifying to inspect the data. This recipe will feed a list of points to a plotting library to easily see a diagram of the data.

Getting ready

Install easyplot from cabal:

$ cabal install easyplot

Create a CSV file containing two-dimensional points:

$ cat input.csv

1,2
2,3
3,1
4,5
5,3
6,1

How to do it…

In a new file, Main.hs, follow these steps:

  1. Import the required library to read in CSV data as well the library to plot points:

    import Text.CSV
    import Graphics.EasyPlot
  2. Create a helper function to convert a list of string records into a list of doubles. For example, we want to convert [ "1.0,2.0", "3.5,4.5" ] into [ (1.0, 2.0), (3.5, 4.5) ]:

    tupes :: [[String]] -> [(Double, Double)]
    
    tupes records = [ (read x, read y) | [x, y] <- records ]
  3. In main, parse the CSV file to be used later on:

    main = do 
      result <- parseCSVFromFile "input.csv"
  4. If the CSV file is valid, plot...