Reader small image

You're reading from  Scala Data Analysis Cookbook

Product typeBook
Published inOct 2015
Reading LevelIntermediate
Publisher
ISBN-139781784396749
Edition1st Edition
Languages
Right arrow
Author (1)
Arun Manivannan
Arun Manivannan
author image
Arun Manivannan

Arun Manivannan has been an engineer in various multinational companies, tier-1 financial institutions, and start-ups, primarily focusing on developing distributed applications that manage and mine data. His languages of choice are Scala and Java, but he also meddles around with various others for kicks. He blogs at http://rerun.me. Arun holds a master's degree in software engineering from the National University of Singapore. He also holds degrees in commerce, computer applications, and HR management. His interests and education could probably be a good dataset for clustering.
Read more about Arun Manivannan

Right arrow

Reading and writing CSV files


Reading and writing a CSV file in Breeze is really a breeze. We just have two functions in breeze.linalg package to play with. They are very intuitively named csvread and csvwrite.

In this recipe, you'll see how to:

  1. Read a CSV file into a matrix

  2. Save selected columns of a matrix into a new matrix

  3. Write the newly created matrix into a CSV file

  4. Extract a vector out of the matrix

  5. Write the vector into a CSV

How it works...

There are just two functions that we need to remember in order to read and write data from and to CSV files. The signatures of the functions are pretty straightforward too:

csvread(file, separator, quote, escape, skipLines)
csvwrite(file, mat, separator, quote, escape, skipLines)

Let's look at the parameters by order of importance:

  • file: java.io.File: Represents the file location.

  • separator: Defaults to a comma so as to represent a CSV. Could be overridden when needed.

  • skipLines: This is the number of lines to be skipped while reading the file. Generally, if there is a header, we pass a skipLines=1.

  • mat: While writing, this is the matrix object that is being written.

  • quote: This defaults to double quotes. It is a character that implies that the value inside is one single value.

  • escape: This defaults to a backspace. It is a character used to escape special characters.

Let's see these in action. For the sake of clarity, I have skipped the quote and the escape parameter while calling the csvread and csvwrite functions. For this recipe, we will do three things:

  • Read a CSV file as a matrix

  • Extract a sub-matrix out of the read matrix

  • Write the matrix

Read the CSV as a matrix:

  1. Let's use the csvread function to read a CSV file into a 100*3 matrix. We'll also skip the header while reading and print 5 rows as a sample:

    val usageMatrix=csvread(file=new File("WWWusage.csv"), separator=',', skipLines=1)
    //print first five rows
    println ("Usage matrix \n"+ usageMatrix(0 to 5,::))
    Output :
    1.0  1.0  88.0
    2.0  2.0  84.0
    3.0  3.0  85.0
    4.0  4.0  85.0
    5.0  5.0  84.0
    6.0  6.0  85.0
    
  2. Extract a sub-matrix out of the read matrix:

    For the sake of generating a submatrix let's skip the first column and save the second and the third column into a new matrix. Let's call it firstColumnSkipped:

    val firstColumnSkipped= usageMatrix(::, 1 to usageMatrix.cols-1)
    
    //Sample some data so as to ensure we are fine
    println ("First Column skipped \n"+ firstColumnSkipped(0 to 5, ::))
    
    Output :
    1.0  88.0
    2.0  84.0
    3.0  85.0
    4.0  85.0
    5.0  84.0
    6.0  85.0
    
  3. Write the matrix:

    As a final step, let's write the firstColumnSkipped matrix to a new CSV file named firstColumnSkipped.csv:

    //Write this modified matrix to a file
    csvwrite(file=new File ("firstColumnSkipped.csv"), mat=firstColumnSkipped, separator=',')
Previous PageNext Chapter
You have been reading a chapter from
Scala Data Analysis Cookbook
Published in: Oct 2015Publisher: ISBN-13: 9781784396749
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Arun Manivannan

Arun Manivannan has been an engineer in various multinational companies, tier-1 financial institutions, and start-ups, primarily focusing on developing distributed applications that manage and mine data. His languages of choice are Scala and Java, but he also meddles around with various others for kicks. He blogs at http://rerun.me. Arun holds a master's degree in software engineering from the National University of Singapore. He also holds degrees in commerce, computer applications, and HR management. His interests and education could probably be a good dataset for clustering.
Read more about Arun Manivannan