Book Image

Clojure for Data Science

By : Henry Garner
Book Image

Clojure for Data Science

By: Henry Garner

Overview of this book

Table of Contents (18 chapters)
Clojure for Data Science
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Matrices


A matrix is a two-dimensional grid of numbers. The dimensions are expressed as the number of rows and columns in the matrix.

For example, A is a matrix with four rows and two columns:

In mathematical notation, a matrix will usually be assigned to a variable with an upper-case letter to distinguish it from other variables in an equation.

We can construct a matrix from our dataset using Incanter's incanter.core/to-matrix function:

(defn ex-3-16 []
  (->> (swimmer-data)
       (i/$ ["Height, cm" "Weight"])
       (i/to-matrix)))

Incanter also defines the incanter.core/matrix function that will take a sequence of scalar values or a sequence of sequences and convert them into a matrix if it can:

(defn ex-3-17 []
  (->> (swimmer-data)
       (i/$ "Height, cm")
       (i/matrix)))

If you run this in the REPL, the output will be a summary of the contents of the matrix:

  A 859x1 matrix
 ---------------
 1.66e+02
 1.92e+02
 1.73e+02
 ...
 1.88e+02
 1.87e+02
 1.83e+02

Incanter returns...