【问题标题】:Clojure / Incanter Data Transformations CapabilitiesClojure / Incanter 数据转换功能
【发布时间】:2013-02-05 00:16:32
【问题描述】:

我正在考虑将 Clojure / Incanter 作为 R 的替代品 只是想知道 clojure / incanter 是否有能力执行以下操作:

  1. 将 SQL 语句的结果作为数据集导入(我在 R 中使用 dbGetQuery 执行此操作)。
  2. 重塑数据集 - 将行转换为列,也称为“pivot”/“unpivot”——我在 R 中使用 reshape、reshape2 包(在 R 世界中称为熔化和铸造数据)来执行此操作。李>
  3. 将重构后的数据集保存到 SQL 表中(我在 R 中使用 RMySQL 中的 dbWriteTable 函数执行此操作)

【问题讨论】:

    标签: clojure incanter


    【解决方案1】:

    您可能对core.matrix 感兴趣 - 这是一个将多维数组和数值计算功能引入 Clojure 的项目。仍处于非常活跃的开发阶段,但已经可用。

    特点:

    • 简洁、实用的 API
    • 正确的多维数组
    • 使用 Clojure 数据的惯用风格,例如嵌套向量 [[1 2] [3 4]] 可以自动用作 2x2 矩阵。
    • 您可能期望的所有阵列重塑功能。
    • 所有常用的矩阵运算(乘法、缩放、行列式等)
    • 支持多种后端矩阵实现,例如JBLAS 实现高性能(使用本机代码)

    在这里查看一些示例代码:

      ;; a matrix can be defined using a nested vector
      (def a (matrix [[2 0] [0 2]]))
    
      ;; core.matrix.operators overloads operators to work on matrices
      (* a a)
    
      ;; a wide range of mathematical functions are defined for matrices
      (sqrt a)  
    
      ;; you can get rows and columns of matrices individually
      (get-row a 0)
    
      ;; Java double arrays can be used as vectors
      (* a (double-array [1 2]))
    
      ;; you can modify double arrays in place - they are examples of mutable vectors
      (let [a (double-array [1 4 9])]
        (sqrt! a)   ;; "!" signifies an in-place operator
        (seq a))
    
      ;; you can coerce matrices between different formats
      (coerce [] (double-array [1 2 3]))
    
      ;; scalars can be used in many places that you can use a matrix
      (* [1 2 3] 2)
    
      ;; operations on scalars alone behave as you would expect
      (* 1 2 3 4 5)
    
      ;; you can do various functional programming tricks with matrices too
      (emap inc [[1 2] [3 4]])
    

    core.matrix 已被 Rich Hickey 批准为官方 Clojure 贡献库,未来 Incanter 很可能会转而使用 core.matrix。

    SQL 表支持不直接包含在 core.matrix 中,但它只是将结果集从 clojure.java.jdbc 转换为 core.matrix 数组的一种方式。像下面这样的东西应该可以解决问题:

    (coerce [] (map vals resultset))
    

    然后你可以随心所欲地使用 core.matrix 对其进行转换和处理。

    【讨论】:

    • 很高兴看到这方面取得的进展。我希望 core.matrix 会在数字 Clojure 空间中带来一些活力,包括 Incanter。
    猜你喜欢
    • 2014-10-17
    • 2011-04-18
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2014-10-30
    相关资源
    最近更新 更多