我在Python非常初学者。 我有矩阵乘法的问题。 我使用列表(矩阵)列表从txt文件中读取矩阵。矩阵乘法在Python,多处理
当我想用多我有鸿沟列出了问题和池功能使用新的列表。
我该如何解决这个问题?
请从评论帮助
def matrix_multiplication(list1, list2):
A = numpy.matrix(list1)
B = numpy.matrix(list2)
return A*B
def counting(dane):
left_matrix = matrices[0]
for matrix in matrices[1:]:
left_matrix = numpy.matrix(left_matrix)
matrix = numpy.matrix(matrix)
left_matrix = matrix_multiplication(left_matrix, matrix)
if __name__ == "__main__":
matrices = []
with open('sample-probka2.txt', 'r') as file:
matrix_reader = csv.reader(file, delimiter=';')
current_matrix = []
for row in matrix_reader:
if len(row) == 0:
matrices.append(current_matrix)
current_matrix = []
else:
current_matrix.append(list(map(float, row)))
print (matrices)
counting(matrices)
np = multiprocessing.cpu_count()
print('You have', np, 'processors')
matrices2 = numpy.array_split(matrices, np)
print(matrices2)
pool = Pool(processes=np)
count = pool.starmap(liczenie, matrices2)
print count
错误,与企图恢复格式化:
multiprocessing.pool.RemoteTraceback:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py",
line 119, in worker result = (True, func(*args, **kwds))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py", line 47,
in starmapstar return list(itertools.starmap(args[0], args[1])) TypeError: counting() takes 1 positional argument but 61 were given
你得到的错误是什么? – Aaron
multiprocessing.pool.RemoteTraceback: “”“ Traceback(最近呼叫的最后一个): File”/Library/Frameworks/Python.framework/Versions/3。6/lib/python3.6/multiprocessing/pool.py“,第119行,在worker result =(True,func(* args,** kwds)) File”/Library/Frameworks/Python.framework/Versions/ 3.6/lib/python3.6/multiprocessing/pool.py“,第47行,在starmapstar 返回列表(itertools.starmap(args [0],args [1])) TypeError:counting()取1个位置参数,但61给予 “”“ –
在我看来,我应该修改'矩阵2'。在这种情况下,我不能使用'pool.starmap(计数,矩阵2)。我应该改变一些东西,但我不知道是什么。 –