【问题标题】:Map-Reduce to solve Matrix multiplication in python with HadoopMap-Reduce 使用 Hadoop 解决 python 中的矩阵乘法
【发布时间】:2018-02-06 18:18:18
【问题描述】:

我想应用 map-reduce 来处理带有 Hadoop 的 python 中的矩阵乘法。目标是计算 A * B。输出应该与输入相似。

输入是两个矩阵A和B的格式如下:

A,0,0,0.0
A,0,1,1.0
...
A,1,3,8.0
A,1,4,9.0
B,0,0,0.0
B,0,1,1.0
...
B,4,0,12.0
B,4,1,13.0

A,0,0,0.0 表示索引为 A(0,0),值为 0.0,与 B 相同。

这是我的地图功能:

import sys
import string
import numpy
#Split line into array of entry data
entry = line.split(",")
# Set row, column, and value for this entry
row = int(entry[1])
col = int(entry[2])
value = float(entry[3])

#If this is an entry in matrix A...
if (entry[0] == "A"):

        #Generate the necessary key-value pairs
        for i in range(col):
                print('<{}{},{} {} {}}>'.format(row,i,A,col,value))
#Otherwise, if this is an entry in matrix B...
else:
        #Generate the necessary key-value pairs
        for i in range(row):
                print('<{}{},{} {} {}}>'.format(i,col,B,row,value))

我想知道如何编写 reduce 函数。 这是我将使用的框架:

import sys
import string
import numpy

#number of columns of A/rows of B
n = int(sys.argv[1])

#Create data structures to hold the current row/column values (if needed; your code goes here)



currentkey = None

# input comes from STDIN (stream data that goes to the program)
for line in sys.stdin:

        #Remove leading and trailing whitespace
        line = line.strip()

        #Get key/value
        key, value = line.split('\t',1)

        #Parse key/value input (your code goes here)

    #If we are still on the same key...
    if key==currentkey:

            #Process key/value pair (your code goes here)


    #Otherwise, if this is a new key...
    else:
            #If this is a new key and not the first key we've seen
            if currentkey:

                    #compute/output result to STDOUT (your code goes here)

            currentkey = key

            #Process input for new key (your code goes here)

#Compute/output result for the last key (your code goes here)

为了运行这两个函数,我将使用一个带有以下代码的小型测试数据集来测试它们:

cat smalltest.txt | python src/map.py 2 3 | sort -n | python src/reduce.py 5

Map 给出 的输出,然后使用sort -n 对键进行排序,所以我将使用reducer 来处理矩阵计算。我的困惑在于编写 reducer 函数。

【问题讨论】:

  • 我的一个困惑是我被告知只需要打印 map 函数的输出,然后 reduce 将自行提取信息。但是,这对我来说没有意义,我不知道如何编写代码。
  • lendap.wordpress.com/2015/02/16/… 我发现这个链接很好地描述了这个问题,但我仍然对 python 编码有点困惑。

标签: python hadoop matrix mapreduce


【解决方案1】:

不知道为什么要减少
我的numpy 方法(带有一些字符串/列表/压缩体操)

 strin = '''A,0,0,0.0
A,0,1,1.0
A,1,0,8.0
A,1,1,9.0
B,0,0,0.0
B,0,1,1.0
B,1,0,12.0
B,1,1,13.0'''.split()

lines = [*map(lambda x: x.split(","),strin)]

linesT = [*zip(*lines)]

linesT

[('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
 ('0', '0', '1', '1', '0', '0', '1', '1'),
 ('0', '1', '0', '1', '0', '1', '0', '1'),
 ('0.0', '1.0', '8.0', '9.0', '0.0', '1.0', '12.0', '13.0')]

现在我们可以获得数组 A、B 的 dims 数据

lastA = linesT[0].index("B") - 1

rowsA, colsA = int(linesT[1][lastA]) + 1, int(linesT[2][lastA]) + 1

datA = [*map(float, linesT[3][0:lastA + 1])]

A = np.array(datA).reshape((rowsA, colsA))

A
Out[50]: 
array([[ 0.,  1.],
       [ 8.,  9.]])

firstB = lastA + 1

rowsB, colsB = int(linesT[1][-1]) + 1, int(linesT[2][-1]) + 1

datB = [*map(float, linesT[3][firstB::])]

B = np.array(datB).reshape((rowsB, colsB))

B
Out[51]: 
array([[  0.,   1.],
       [ 12.,  13.]])

A @ B
Out[52]: 
array([[  12.,   13.],
       [ 108.,  125.]])

【讨论】:

  • 你的回答是对的,我理解你的逻辑,但我的困惑是如何使用reducer来处理从mapper返回的键值。
【解决方案2】:

好吧,我直奔主题,

    lines = [*map(lambda x: x.split(","),strin)]

是一种简化的方式,如果 lambda 函数本身甚至不在具有语法的输入中,就好像该字符串不存在 老实说,减少它是你应该感谢的事情,这段代码(不要太苛刻)很乱,所以我不明白你为什么抱怨自动减少..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多