Skip to content

Commit

Permalink
moved visualizing_correlation_matrices into the echarts chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
daslu committed Jan 4, 2025
1 parent 559aa2d commit 0f78253
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 140 deletions.
3 changes: 1 addition & 2 deletions notebooks/chapters.edn
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"interactions_ols"]}
{:part "Data Visualization"
:chapters ["tableplot_datavis_intro"
"echarts"
"visualizing_correlation_matrices"]}
"echarts"]}
{:part "Linear Algebra"
:chapters ["linear_algebra_intro"
"fastmath_vector_geom2d3d"
Expand Down
68 changes: 67 additions & 1 deletion notebooks/noj_book/echarts.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;; # Data Visualization with Echarts

;; author: Zuzhi Hu, Ken Huang
;; author: Zuzhi Hu, Ken Huang, Daniel Slutsky

;; In this tutorial, we explore how we can use
;; [Apache Echarts](https://echarts.apache.org/)
Expand Down Expand Up @@ -545,3 +545,69 @@ reshaped-stocks

;; Note the slider control and the tooltips.

;; ### Correlation heatmaps

;; Let us demonstrate how one may visualize [correlation matrices](https://en.wikipedia.org/wiki/Correlation#Correlation_matrices).
;; The following auxiliary functions are inspired by the discussion at the [Clojurians Zulip chat](https://scicloj.github.io/docs/community/chat/):
;; [#**data-science>correlation matrix plot ?**](https://clojurians.zulipchat.com/#narrow/stream/151924-data-science/topic/correlation.20matrix.20plot.20.3F)

;; Rounding numbers:

(defn round
[n scale rm]
(.setScale ^java.math.BigDecimal (bigdec n)
(int scale)
^RoundingMode (if (instance? java.math.RoundingMode rm)
rm
(java.math.RoundingMode/valueOf
(str (if (ident? rm) (symbol rm) rm))))))

;; For example (see [RoundingMode](https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html))

(round (/ 2.0 3) 2 :DOWN)
(round (/ 2.0 3) 2 :UP)
(round (/ 2.0 3) 2 :HALF_EVEN)

;; Computing a correlation matrix and representing it as a dataset:

(defn correlations-dataset [data columns-to-use]
(let [matrix (->> columns-to-use
(mapv #(get data %))
fastmath.stats/correlation-matrix)]
(->> matrix
(map-indexed
(fn [i row]
(let [coli (columns-to-use i)]
(->> row
(map-indexed
(fn [j corr]
(let [colj (columns-to-use j)]
{:i i
:j j
:coli coli
:colj colj
:corr corr
:corr-round (round corr 2 :HALF_EVEN)})))))))
(apply concat)
tc/dataset)))

;; For example:
(-> noj-book.datasets/iris
(correlations-dataset [:sepal-length :sepal-width :petal-length :petal-width]))

;; Visualizing a corrleation matrix as a heatmap:

(let [columns-for-correlations [:sepal-length :sepal-width
:petal-length :petal-width]
correlations (-> noj-book.datasets/iris
(correlations-dataset columns-for-correlations)
(tc/select-columns [:coli :colj :corr-round])
tc/rows)]
(echarts-heatmap {:xyz-data correlations
:xs columns-for-correlations
:ys columns-for-correlations
:min -1
:max 1
:series-name "correlation"}))


137 changes: 0 additions & 137 deletions notebooks/noj_book/visualizing_correlation_matrices.clj

This file was deleted.

0 comments on commit 0f78253

Please sign in to comment.