Book Image

Java Data Science Cookbook

By : Rushdi Shams
Book Image

Java Data Science Cookbook

By: Rushdi Shams

Overview of this book

If you are looking to build data science models that are good for production, Java has come to the rescue. With the aid of strong libraries such as MLlib, Weka, DL4j, and more, you can efficiently perform all the data science tasks you need to. This unique book provides modern recipes to solve your common and not-so-common data science-related problems. We start with recipes to help you obtain, clean, index, and search data. Then you will learn a variety of techniques to analyze, learn from, and retrieve information from data. You will also understand how to handle big data, learn deeply from data, and visualize data. Finally, you will work through unique recipes that solve your problems while taking data science to production, writing distributed data science applications, and much more - things that will come in handy at work.
Table of Contents (16 chapters)
Java Data Science Cookbook
About the Author
About the Reviewer
Customer Feedback

Calculating Pearson's correlation of two sets of data points

PearsonsCorrelation computes correlations defined by the formula cor(X, Y) = sum[(xi - E(X))(yi - E(Y))] / [(n - 1)s(X)s(Y)], where E(X) and E(Y) are means of X and Y, and s(X) and s(Y) are their respective standard deviations.

How to do it...

  1. Create a method that takes two double arrays that represent two sets of data points:

            public void calculatePearson(double[] x, double[] y){ 
  2. Create a PearsonsCorrelation object:

            PearsonsCorrelation pCorrelation = new PearsonsCorrelation(); 
  3. Compute correlation of the two sets of data points:

            double cor = pCorrelation.correlation(x, y); 
  4. Use the correlation as per your requirements, and close the method:


The complete code for the recipe is as follows:

import org.apache.commons.math3.stat.correlation.PearsonsCorrelation; 
public class PearsonTest { 
   public static void main(String[] args...