You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@mahout.apache.org by Paul Hubenig <ph...@yahoo.com> on 2012/09/26 04:55:24 UTC
problem mutliplying matrices
Create a simple matrix:
1 2
3 4
package exp
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.apache.hadoop.io.{IOUtils, IntWritable, SequenceFile}
import org.apache.mahout.math.{SequentialAccessSparseVector, VectorWritable, RandomAccessSparseVector}
object MakeMatrix extends App with CommonMethods {
val matrixFileOutput = "/Users/phubenig/matrixFile.sf"
val conf = new Configuration()
val fs = getFileSystem(true, conf)
val path = new Path(matrixFileOutput)
val arr = Array(Array(1, 2), Array(3, 4))
val rank = 2
val key = new org.apache.hadoop.io.IntWritable()
val writer = SequenceFile.createWriter(fs, conf, path, classOf[IntWritable], classOf[VectorWritable])
for (i <- 0 until rank) {
key.set(i)
val v = new SequentialAccessSparseVector(rank)
for (j <- 0 until rank) {
v.setQuick(j, arr(i)(j))
}
writer.append(key, new VectorWritable(v))
}
IOUtils.closeStream(writer)
}
Engage mahout to square this matrix.
$ mahout matrixmult --numRowsA 2 --numColsA 2 --numRowsB 2 --numColsB 2 --inputPathA matrixFile.sf --inputPathB matrixFile.sf
Read back the result:
package exp
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.io.{IntWritable, SequenceFile}
import org.apache.mahout.math.{VectorWritable , Vector}
import org.apache.hadoop.fs.Path
object ReadMatrix extends App with CommonMethods {
val matrixFile = "/Users/phubenig/productWith-96/part-00000"
val conf = new Configuration()
val fs = getFileSystem(true, conf)
val path = new Path(matrixFile)
var key = new IntWritable()
var value = new VectorWritable()
val reader = new SequenceFile.Reader(fs, path, conf)
var i = 0
while (reader.next(key, value)) {
val vect = value.get()
for (j <- 0 until 2) {
println("(" + i + ", " + j + "): " + vect.get(j))
}
i += 1
}
reader.close()
}
I get the answer:??
(0, 0): 10.0
(0, 1): 14.0
(1, 0): 14.0
(1, 1): 20.0
i.e.,
10 14
14 20
Correct answer??
1 2 1 2 7 10
x =
3 4 3 4 15 22
I tried mahout transpose and that seems to work.
Am I specifying the matrix wrong somehow?
10 =|(1, 3)| * |(1,3)|
20 =|(2,4)| * |(2,4)|
14 = (1,3) , (2,4)
but why is it doing this?
}}