Analysis
Gather statistics on datasets.

Analysis of data

Sometimes datasets are too large or too abstract in their format to manually analyze and estimate statistics on certain columns or patterns. DataVec comes with some helper utilities for performing a data analysis, and maximums, means, minimums, and other useful metrics.

Using Spark for analysis

If you have loaded your data into Apache Spark, DataVec has a special AnalyzeSpark class which can generate histograms, collect statistics, and return information about the quality of the data. Assuming you have already loaded your data into a Spark RDD, pass the JavaRDD and Schema to the class.
If you are using DataVec in Scala and your data was loaded into a regular RDD class, you can convert it by calling .toJavaRDD() which returns a JavaRDD. If you need to convert it back, call rdd().
The code below demonstrates some of many analyses for a 2D dataset in Spark analysis using the RDD javaRdd and the schema mySchema:
1
import org.datavec.spark.transform.AnalyzeSpark;
2
import org.datavec.api.writable.Writable;
3
import org.datavec.api.transform.analysis.*;
4
5
int maxHistogramBuckets = 10
6
DataAnalysis analysis = AnalyzeSpark.analyze(mySchema, javaRdd, maxHistogramBuckets)
7
8
DataQualityAnalysis analysis = AnalyzeSpark.analyzeQuality(mySchema, javaRdd)
9
10
Writable max = AnalyzeSpark.max(javaRdd, "myColumn", mySchema)
11
12
int numSamples = 5
13
List<Writable> sample = AnalyzeSpark.sampleFromColumn(numSamples, "myColumn", mySchema, javaRdd)
Copied!
Note that if you have sequence data, there are special methods for that as well:
1
SequenceDataAnalysis seqAnalysis = AnalyzeSpark.analyzeSequence(mySchema, sequenceRdd)
2
3
List<Writable> uniqueSequence = AnalyzeSpark.getUniqueSequence("myColumn", seqSchema, sequenceRdd)
Copied!

Analyzing locally

The AnalyzeLocal class works very similarly to its Spark counterpart and has a similar API. Instead of passing an RDD, it accepts a RecordReader which allows it to iterate over the dataset.
1
import org.datavec.local.transforms.AnalyzeLocal;
2
3
int maxHistogramBuckets = 10
4
DataAnalysis analysis = AnalyzeLocal.analyze(mySchema, csvRecordReader, maxHistogramBuckets)
Copied!

Utilities

AnalyzeLocal

[source]
Analyse the specified data - returns a DataAnalysis object with summary information about each column

analyze

1
public static DataAnalysis analyze(Schema schema, RecordReader rr, int maxHistogramBuckets)
Copied!
Analyse the specified data - returns a DataAnalysis object with summary information about each column
    param schema Schema for data
    param rr Data to analyze
    return DataAnalysis for data

analyzeQualitySequence

1
public static DataQualityAnalysis analyzeQualitySequence(Schema schema, SequenceRecordReader data)
Copied!
Analyze the data quality of sequence data - provides a report on missing values, values that don’t comply with schema, etc
    param schema Schema for data
    param data Data to analyze
    return DataQualityAnalysis object

analyzeQuality

1
public static DataQualityAnalysis analyzeQuality(final Schema schema, final RecordReader data)
Copied!
Analyze the data quality of data - provides a report on missing values, values that don’t comply with schema, etc
    param schema Schema for data
    param data Data to analyze
    return DataQualityAnalysis object

AnalyzeSpark

[source]
AnalizeSpark: static methods for analyzing and

analyzeSequence

1
public static SequenceDataAnalysis analyzeSequence(Schema schema, JavaRDD<List<List<Writable>>> data,
2
int maxHistogramBuckets)
Copied!
    param schema
    param data
    param maxHistogramBuckets
    return

analyze

1
public static DataAnalysis analyze(Schema schema, JavaRDD<List<Writable>> data)
Copied!
Analyse the specified data - returns a DataAnalysis object with summary information about each column
    param schema Schema for data
    param data Data to analyze
    return DataAnalysis for data

analyzeQualitySequence

1
public static DataQualityAnalysis analyzeQualitySequence(Schema schema, JavaRDD<List<List<Writable>>> data)
Copied!
Randomly sample values from a single column
    param count Number of values to sample
    param columnName Name of the column to sample from
    param schema Schema
    param data Data to sample from
    return A list of random samples

analyzeQuality

1
public static DataQualityAnalysis analyzeQuality(final Schema schema, final JavaRDD<List<Writable>> data)
Copied!
Analyze the data quality of data - provides a report on missing values, values that don’t comply with schema, etc
    param schema Schema for data
    param data Data to analyze
    return DataQualityAnalysis object

min

1
public static Writable min(JavaRDD<List<Writable>> allData, String columnName, Schema schema)
Copied!
Randomly sample a set of invalid values from a specified column. Values are considered invalid according to the Schema / ColumnMetaData
    param numToSample Maximum number of invalid values to sample
    param columnName Same of the column from which to sample invalid values
    param schema Data schema
    param data Data
    return List of invalid examples

max

1
public static Writable max(JavaRDD<List<Writable>> allData, String columnName, Schema schema)
Copied!
Get the maximum value for the specified column
    param allData All data
    param columnName Name of the column to get the minimum value for
    param schema Schema of the data
    return Maximum value for the column