Uploaded image for project: 'Mahout'
  1. Mahout
  2. MAHOUT-1691

iterable of vectors to matrix

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Not A Problem
    • Affects Version/s: 0.10.1
    • Fix Version/s: 0.11.1
    • Component/s: Math
    • Labels:

      Description

      In Mahout scala bindings, instead of writing

      val res = drmX.mapBlock(drmX.ncol) {
        case (keys, block) => {
          val copy = block.like
          copy := block.map(row => (row - mean) / std)
          (keys, copy)
        }
      }
      

      I would like to be able to write

      val res = drmX.mapBlock(drmX.ncol) {
        case (keys, block) => {
          keys -> block.map(row => (row - mean) / std)
        }
      }
      

      Solution: add a method for implicit conversion from iterable to Matrix:

        implicit def iterable2Matrix(that: Iterable[Vector]): Matrix = {
          val first = that.head
          val nrow = that.size
          val ncol = first.size
      
          val m = if (first.isDense) {
            new DenseMatrix(nrow, ncol)
          } else {
            new SparseRowMatrix(nrow, ncol)
          }
      
          that.zipWithIndex.foreach { case (row, idx) => 
            m.assignRow(idx.toInt, row)
          }
      
          m
        }
      

      If it sounds nice, I can send a pull request with this implemented

        Attachments

          Activity

            People

            • Assignee:
              smarthi Suneel Marthi
              Reporter:
              agrigorev Alexey Grigorev
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: