python - 如何使用 MapReduce 在 python 中乘以矩阵?
问题描述
假设我有两个 2X2 矩阵,如下所示。
A,0,0,1
A,0,1,0
A,1,0,0
A,1,1,1
B,0,0,2
B,0,1,3
B,1,0,4
B,1,1,5
例如B,1,0,4
,表示矩阵 B,第 1 行,第 0 列,值 4。
如何使用 MapReduce 方法计算两个指标的乘积以获得此输出:
0,0,2
0,1,3
1,0,4
1,1,5
解决方案
我制作了一个cache.txt
文件来输入矩阵的尺寸。(row_a, col_b, col_a)
input.txt
包含矩阵。
代码如下。
cache_info = open("cache.txt").readlines()[0].split(",")
row_a, col_b, col_a = map(int, cache_info)
mapperOutput = open("mapperOutput.txt", "w")
for line in open("input.txt"):
matrix_index, row, col, value = line.rstrip().split(",")
if matrix_index == "A":
for i in range(0, col_b):
key = row + "," + str(i)
mapperOutput.write("%s\t%s\t%s" % (key, col, value) + "\n")
else:
for j in range(0, row_a):
key = str(j) + "," + col
mapperOutput.write("%s\t%s\t%s" % (key, row, value) + "\n")
mapperOutput.close()
numbers1 = list()
for line in open("mapperOutput.txt"):
curr_index, index, value = line.rstrip().split("\t")
index, value = map(int, [index, value])
numbers1.append((curr_index, index, value))
numbers2 = numbers1
initValue1 = list()
initValue2 = list()
for i in numbers1:
checker = 0
for j in numbers2:
if i == j:
if checker == 0:
checker += 1
continue
if i[0] == j[0]:
if i[1] == j[1]:
initValue1.append([i[0],str(i[1]),i[2]*j[2]])
initValue2 = initValue1
myOut = dict()
counter = 0
for i in initValue1:
if counter > (row_a*col_b*col_a):
break
if i[0] in myOut.keys():
counter += 1
continue
counter += 1
myOut[i[0]] = i[2]
inercounter = 0
for j in initValue2:
if i[0] == j[0]:
if i[1] != j[1]:
inercounter += 1
if inercounter > col_b - 1:
continue
myOut[i[0]] += j[2]
for key,value in myOut.items():
print(key,value)
输出将是:
0,0 2
0,1 3
1,0 4
1,1 5
推荐阅读
- javascript - Firebase 函数 - http 获取带有强调的参数
- flutter - 如何从 DrawerHeader 中删除 Padding
- kubernetes - 如何以编程方式将工作负载部署到 GCP Kubernetes?
- php - 用于获取用户数据的 codeigniter 会话的替代方法
- c# - xamarin.forms - 如何让两个视图的事件处理程序调用相同的代码
- import - 在 Keras 中导入模型后,顺序层内的隐藏层
- sql - BigQuery Standard SQL 中具有不同架构的联合表
- python - keras 无法多次调用 model.predict_classes
- java - 发生在线程和原子变量之间
- mysql - 如果表不包含值,则 MySQL 连接返回 null