Plotting Graphs – Data Science with Scala

Data visualization is an integral part of data science. The programming language Scala has many characteristics that make it popular for data science use cases among other languages like R and Python. Immutable data structures and functional constructs are some of the features that make it so attractive to data scientists. Popular big data crunching frameworks like Spark or Flink do have their fair share on an ever growing ecosystem of tools and libraries for data analysis and engineering. Scala is particularly well suited to build robust libraries for scalable data analytics.

In this post we are going to introduce Breeze, a library for fast linear algebraic manipulation of data sets, together with tools for visualization and NLP. Starting with basic creation of vectors, we will create an application for plotting stock prices. The stock data is obtained form Yahoo Finance, but can also be downloaded here for SAP, YAHOO, BMW, and IBM.

Installing Breeze

You can follow the here described steps by cloning the sample project from github, the project uses Scala 2.11.7 and SBT 0.13.8, or by creating your own standalone project with SBT. For Breeze make sure you add it as a dependency in your build file sbt.build

For fast exploration sbt console executed within the projects directory can be used to follow the below steps.

Loading the Stock Price Data

As a preliminary step we are going to required depencies we will need for our data exploration:

Next we will load the data from the CSV files obtained previously. All files contain a header file describing the schema of it. The files have 7 columns that are: (1) Date, (2) Open, (3) High, (4) Low, (5) Close, (6) Volume, and (7) Adj. Close. For a first plot visualizing the close values would be a good exercise for creating a Vector with Breeze.

Breeze comes with data structures well known and used by data scientists: Vectors and Matrices. Turning our provided CSV files of stock price information into a usable data structure for manipulation and plotting is described in the next steps. We will use a DenseVector to represent the closing prices for each day. A separate vector will be used for the days themselves. Additionally Breeze provides SparseVector optimized for storage and a HashVector optimized for access. Read the overview here about Breeze data structures for more details.

Reading the closing prices into a vector in Scala looks like the below code. All is done in one line:

The steps explained in detail:

  1.  The Source class of the Scala Standard API provides an iterable representation of source data. Here from our CSV file: Source.fromFile("sap_stocks.csv")
  2. Next we are iterating over each line getLines(), except for the first line drop(1) .
  3. Iterating over each line in a functional manner gives us the oppertunity to parse each line into a double value of the 5 column
  4. DenseVector expects a Sequence type and not an Iterator, hence the casting toSeq()
  5. To turn this into a vector we have to use the splat operator :_ *

On the x-axis of our stock graph the time of the corresponding closing price gets plotted. Please note that for simplicity we are numerating the days as double values prior to creating a DenseVector. Again, all in one line 😉

The steps explained in detail:

    1. We create a DenseVector from a range ranking from 0 to the number of values contained in sap_stock_close in single steps: .range(0, sap_stock_close.length, 1)
    2. Our numeration needs to be of type Double, so that sap_stock_close and sap_stock_days are of the same type. We again use functional iteration to cast each element to Double: .map( _.toDouble)

Basic Vector Manipulation in Breeze

The Linear Algebra Cheat Sheet of Breeze is a fairly good resource to get an overview of possible manipulations supported for Vectors. One common scenario is slicing and indexing of vectors. Another one would be element wise manipulation, of which we’ve already seen an example with the splat operator.

Getting specific values from a vector can be achieved by using an index. In contrast to Scala collections Breeze data structures do support negative indexes, for example to obtain the last value in a vector:

Plotting Graphs with Scala

Data visualization is an integral part of data science. In this example we can use breeze-viz for drawing out the stock prices. Breeze-viz is the visualization package by Breeze that wraps the very popular Java charting library JFreeChart.

First we need import the package to make it available:

The first step for plotting with breeze-viz is to create a Figure. This will launch an empty Java Swing application that you might see appear in your task bar depending on your OS:

A Figure supports multiple plots and we can add one plot using the subplot function:

Having a plot added to our figure we can now draw the actual function on top of it. The changes will become visible once the figure gets refreshed. After the refresh you should be able to see your plot:

On the x-axis we display the numerated trading days, while the y-axis plots the closing prices at each day:

sap_stock_graph_jfreechart

Let us further do some simple customization of the plot to demonstrate basic feature. For example we will add a label to the plot. In order to see the label of the graph we also have to enable the legend.

Between trading day 4000 and 4500 on the plot we can notice a unusual jump of the stock price, which likely is an error in the data or a very unusual event in the history of SAP.

To further highlight it we can work with so called DomainMarkers and RangerMarkers, which we will set around the values of that particular event. We also are going to add further labels:

Last we can add a text placed within the plot to further highlight the drastic event discovered.

Our final plot:

sap_stock_plot

Further Readings

1 thought on “Plotting Graphs – Data Science with Scala”

Leave a Reply