I’ve just released daru version 0.0.5, which brings in a lot of new features and consolidates existing ones. NMatrix is now well integrated into Daru and all of the operations that can be performed using Arrays as the underlying implementation can be performed using NMatrix as well (except some operations involving missing data).
The new features include extensive support for missing data, hierarchial sorting of data frames and vectors by preserving indexing, ability to group, split and aggregate data with group by, and quickly summarizing data by generating excelstyle pivot tables. This release also includes new aritmetic and statistical functions on Data Frames and Vectors. Both DataFrame and Vector are now mostly compatible with statsample, allowing for a much larger scope of statistical analysis by leveraging the methods already provided in statsample.
The interface for interacting with nyaplot for plotting has also been revamped, allowing much greater control on the way graphs are handled by giving direct access to the graph object. A new class for hierarchial indexing of data (called MultiIndex) has also been added, which is immensely useful when grouping/splitting/aggregating data.
Lets look at all these features one by one:
Data Types
You can now either use Ruby Arrays or NMatrix as the underlying implementation. Since NMatrix is fast and makes use of C storage, it is recommended to use nmatrix when dealing with large sets of data. Daru will store any data as Ruby Array unless explicitly specified.
Thus to specify the data type of a Vector use the option :dtype
and either supply it with :array
or :nmatrix
, and if using the NMatrix dtype, you can also specify the C data type that NMatrix will use internall by using the option :nm_dtype
and supplying it with one of the NMatrix data types (it currently supports ints, floats, rationals and complex numbers. Check the docs for further details).
As an example, consider creating a Vector which uses NMatrix underneath, and stores data using the :float64
NMatrix data type, which stands for double precision floating point numbers.
1 2 3 4 5 6 7 8 

Another distinction between types of data that daru offers is :numeric
and :object
. This is a generic feature for distinguishing numerical data from other types of data (like Strings or DateTime objects) that might be contained inside Vectors or DataFrames. These distinctions are important because statistical and arithmetic operations can only be applied on structures with type numeric.
To query the data structure for its type, use the #type
method. If the underlying implemetation is an NMatrix, it will return the NMatrix data type, otherwise for Ruby Arrays, it will be either :numeric
or :object
.
1 2 

Thus Daru exposes three methods for querying the type of data:
* #type
 Get the generic type of data to know whether numeric computation can be performed on the object. Get the C data type used by nmatrix in case of dtype NMatrix.
* #dtype
 Get the underlying data representation (either :array or :nmatrix).
Working with Missing Data
Any data scientist knows how common missing data is in reallife data sets, and to address that need, daru provides a host of functions for this purpose. This functionality is still in its infancy but should be up to speed soon.
The #is_nil?
function will return a Vector object with true
if a value is nil
and false
otherwise.
1 2 3 4 5 6 7 8 9 10 11 

The #nil_positions
function returns an Array that contains the indexes of all the nils in the Vector.
1


The #replace_nils
functions replaces nils with a supplied value.
1 2 3 4 5 6 7 8 9 10 

The statistics functions implemented on Vectors ensure that missing data is not considered during computation and are thus safe to call on missing data.
Hierarchical sorting of DataFrame
It is now possible to use the #sort
function on Daru::DataFrame such that sorting happens hierarchically according to the order of the specified vector names.
In case you want to sort according to a certain attribute of the data in a particular vector, for example sort a Vector of strings by length, then you can supply a code block to the :by
option of the sort method.
Supply the :ascending
option with an Array containing ‘true’ or ‘false’ depending on whether you want the corresponding vector sorted in ascending or descending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

Vector objects also have a similar sorting method implemented. Check the docs for more details. Indexing is preserved while sorting of both DataFrame and Vector.
DSL for plotting with Nyaplot
Previously plotting with daru required a lot of arguments to be supplied by the user. The interface did not take advatage of Ruby’s blocks, nor did it expose many functionalities of nyaplot. All that changes with this new version, that brings in a new DSL for easy plotting (recommended usage with iruby notebook).
Thus to plot a line graph with data present in a DataFrame:
1 2 3 4 5 6 

As you can see, the #plot
function exposes the Nyaplot::Plot
and Nyaplot::Diagram
objects to user after populating them with the relevant data. So the new interface lets experienced users utilize the full power of nyaplot but keeps basic plotting very simple to use for new users or for quick and dirty visualization needs. Unfortunately for now, until a viable solution to interfacing with nyaplot is found, you will need to use the nyaplot API directly.
Refer to this notebook for advanced plotting tutorials.
Statistics and arithmetic on DataFrames.
Daru includes a host of methods for simple statistical analysis on numeric data. You can call mean
, std
, sum
, product
, etc. directly on the DataFrame. The corresponding computation is performed on numeric Vectors within the DataFrame, and missing data if any is excluded from the calculation by default.
So for this DataFrame:
1 2 3 4 5 6 7 8 

To calculate the mean of numeric vectors:
1


Apart from that you can use the #describe
method to calculate many statistical features of numeric Vectors in one shot and see a summary of statistics for numerical vectors in the DataFrame that is returned. For example,
1


The covariance and correlation coeffiecients between the numeric vectors can also be found with #cov
and #corr
1 2 3 4 5 6 7 

Hierarchial indexing
A new way of hierarchially indexing data has been introduced in version 0.0.5. This is done with the new Daru::MultiIndex
class. Hierarchial indexing allows grouping sets of similar data by index and lets you select sub sets of data by specifying an index name in the upper hierarchy.
A MultiIndex can be created by passing a bunch of tuples into the Daru::MultiIndex class. A DataFrame or Vector can be created by passing it a MultiIndex object into the index
option. A MultiIndex can be used for determining the order of Vectors in a DataFrame too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 

Selecting a top level index from the hierarchy will select all the rows under that name, and return a new DataFrame with just that much data and indexes.
1


Alternatively passing the entire tuple will return just that row as a Daru::Vector
, indexed according to the column index.
1


Hierachical indexing is especially useful when aggregating or splitting data, or generating data summaries as we’ll see in the following examples.
Splitting and aggregation of data
When dealing with large sets of scattered data, it is often useful to ‘see’ the data grouped according to similar values in a Vector instead of it being scattered all over the place.
The #group_by
function does exactly that. For those familiar SQL, #group_by
works exactly like the GROUP BY clause, but is much easier since its all Ruby.
The #group_by
function will accept one or more Vector names and will scan those vectors for common elements that can be grouped together. In case multiple names are specified it will check for common attributes accross rows.
So for example consider this DataFrame:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 

To group this DataFrame by the columns :a
and :b
, pass them as arguments to the #group_by
function, which returns a Daru::Core::GroupBy
object.
Calling #groups
on the returned GroupBy
object returns a Hash
with the grouped rows.
1 2 3 4 5 6 7 8 9 

To see the first group of each group from this collection, call #first
on the grouped
variable. Calling #last
will return the last member of each group.
1 2 3 4 5 6 7 8 9 

On a similar note #head(n)
will return the first n
groups and #tail(n)
the last n
groups.
The #get_group
function will select only the rows that a particular group belongs to and return a DataFrame with those rows. The original indexing is ofcourse preserved.
1 2 3 4 5 6 

The Daru::Core::GroupBy
object contains a bunch of methods for creating summaries of the grouped data. These currently include #mean
, #std
, #product
, #sum
, etc. and many more to be added in the future. Calling any of the aggregation methods will create a new DataFrame which will have the index as the group and the aggregated data of the nongroup vectors as the corresponding value. Of course this aggregation will apply only to :numeric
type Vectors and missing data will not be considered while aggregation.
1


A hierarchichally indexed DataFrame is returned. Check the GroupBy
docs for more aggregation methods.
Generating Excelstyle Pivot Tables
You can generate an excelstyle pivot table with the #pivot_table
function. The levels of the pivot table are stored in MultiIndex objects.
To demonstrate with an example, consider this CSV file on sales data.
To look at the data from the point of view of the manager and rep:
1


You can see that the pivot table has summarized the data and grouped it according to the manager and representative.
To see the sales broken down by the products:
1


Compatibility with statsample
Daru is now completely compatible with statsample and you can now perform all of the functions by just passing it a Daru::DataFrame or Daru::Vector to perform statistical analysis.
Find more examples of using daru for statistics in these notebooks.
Heres an example to demonstrate:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 

References
 Pivot Tables example taken from here.