我在将矩阵从文本文件加载到Python时遇到问题。我有一个带有矩阵的txt文件,如下所示:
Matrix # 1[6 x 8]
[[0.0322202 0.09434484 0.25002295 0.2528428 0.10415784 0.24142196
0.010517 0.25487851]
[0.18335361 0.1277445 0.06118253 0.18690019 0.20396079 0.28628478
0.23368012 0.07932863]
[0.01586491 0.08351546 0.09179019 0.19086502 0.26448857 0.00341661
0.0076354 0.14970549]
[0.05323177 0.01803223 0.26651485 0.249316 0.00885857 0.28183164
0.06242965 0.10416661]
[0.05393575 0.2685312 0.0928845 0.0165103 0.19793575 0.18197242
0.10990779 0.11711208]
[0.11764279 0.23854231 0.14737164 0.15334971 0.26638431 0.04492217
0.12121334 0.0157779 ]]
-------------------------
Matrix # 2[8 x 8]
[[0.03673123 0.04280058 0.13064546 0.0483846 0.06306662 0.04791767
0.20136789 0.06709436]
[0.00859638 0.2915551 0.1329647 0.00975984 0.12029034 0.2637118
0.12587069 0.11391991]
[0.05633049 0.08800232 0.03203959 0.02466364 0.10011332 0.15201659
0.22264326 0.0558971 ]
[0.05053821 0.04099701 0.27159803 0.08778437 0.20792823 0.2030534
0.25036928 0.16582882]
[0.16867485 0.03230341 0.19495864 0.10821256 0.12185273 0.05480103
0.22728856 0.25456569]
[0.02218817 0.23359441 0.15457978 0.0275037 0.06745245 0.12328887
0.16972525 0.02161821]
[0.28029231 0.16327778 0.27735648 0.20591421 0.21236012 0.17597595
0.20992926 0.01747133]
[0.10150612 0.22284606 0.11146442 0.18066627 0.12760146 0.10264632
0.24329665 0.26529221]]
-------------------------
Matrix # 3[8 x 5]
[[0.0044761 0.26838514 0.22510378 0.22843132 0.07689473]
[0.16908802 0.15970796 0.25875775 0.04569838 0.04147033]
[0.08524995 0.04703752 0.05619528 0.14943606 0.24411115]
[0.0667661 0.13352421 0.19563742 0.11554089 0.10493734]
[0.14797975 0.06908592 0.06823431 0.04430664 0.09185596]
[0.02574791 0.0367757 0.23516482 0.1551992 0.27722899]
[0.14542998 0.01641985 0.24688273 0.21755754 0.09459343]
[0.26374249 0.12827675 0.1170908 0.004356 0.08593468]]
-------------------------
和一千个类似矩阵。
我编写了如下所示的Python代码:
import csv
matrices = []
if __name__ == "__main__":
with open('matrices-2-2.txt', 'r') as file:
matrix_reader = csv.reader(file, delimiter =" ")
current_matrix = [];
for row in matrix_reader:
if row == "-------------------------":
matrices.append(current_matrix)
current_matrix = []
elif row == "Matrix":
self()
else:
current_matrix.append(list(map(float, row)))
print(matrices)
我设法加载了矩阵。不幸的是,目前,我在尝试将它们相乘时遇到问题。我创建了以下代码:
import multiprocessing
from multiprocessing import Pool
from time import time
import numpy
import re
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 = []
start = time()
with open('matrices-2-2.txt', 'r') as file:
content = file.read()
# gets each matrix based on the pattern [[ ]]
matrices_string = re.findall("\[([\d\s.\n\]\[]+)\]", content)
# loops over each matrix
for matrix_string in matrices_string:
# parses each row of the matrix
matrix_rows = re.findall("\[([\d\s.\n\]]+)\]", matrix_string)
# gets all the numbers for each row of the matrix and remakes the matrix as a list of floats
matrices.append([list(map(float, re.findall("\d+\.\d+", row))) for row in matrix_rows])
print('Load Matrices')
np = multiprocessing.cpu_count()
print('You have', np, 'processors')
counting(matrices)
stop = time()
print('The Calculation took ', stop - start, 'seconds')
matrices2 = numpy.array_split(matrices, np)
start = time()
pool = Pool(processes=np)
count = pool.map(counting, matrices2)
stop = time()
print("Parallel Calculation took ", stop - start, 'seconds')
编译器显示以下错误:
文件“ /.../Matrix1.py”,第45行,在 计数(矩阵) 正在计数的文件“ /.../Matrix1.py”,第21行 left_matrix = matrix_multiplication(left_matrix,矩阵) 文件“ /.../Matrix1.py”,行12,在matrix_multiplication中 回报(A * B) mul 中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/matrixlib/defmatrix.py”,第343行 返回N.dot(自身,asmatrix(其他)) ValueError:形状(6,6)和(5,6)不对齐:6(dim 1)!= 5(dim 0) 请帮助,我希望该算法能够正常工作。
答案 0 :(得分:0)
这些条件row == "-------------------------"
,row == "Matrix"
永远不会是真实的,因为每一行都是一个数组。
我可以猜到您遇到ValueError
异常。
因此,要修复代码,您需要按照line[0] == "matrix"
或"-" in line[0]
您也可以在价值线上使用方括号,将其删除。
由于您没有使用有效的CSV文件,因此我认为您可以更改代码以仅对该文件进行迭代,并且当您找到值行时,可以使用line.split(' ')
命令,如下例所示:
for line in open('matrix.txt', 'r'):
if "matrix" in line or "-" in line:
continue
print([float(element) for element in line.split(' ')])
答案 1 :(得分:0)
如前所述,我相信正则表达式可能是解析文本文件的最佳方法。 下面的代码在您输入的内容中起作用
import re
from pprint import pprint
matrices = []
if __name__ == "__main__":
with open('matrices-2-2.txt', 'r') as file:
content = file.read()
# gets each matrix based on the pattern [[ ]]
matrices_string = re.findall("\[([\d\s.\n\]\[]+)\]", content)
#loops over each matrix
for matrix_string in matrices_string:
# parses each row of the matrix
matrix_rows = re.findall("\[([\d\s.\n\]]+)\]", matrix_string)
# gets all the numbers for each row of the matrix and remakes the matrix as a list of floats
matrices.append([list(map(float, re.findall("\d+\.\d+", row))) for row in matrix_rows])
pprint(current_matrix)
一段输出:
[[[0.0322202,
0.09434484,
0.25002295,
0.2528428,
0.10415784,
0.24142196,
0.010517,
0.25487851],
[0.18335361,
0.1277445,
0.06118253,
0.18690019,
0.20396079,
0.28628478,
0.23368012,
0.07932863],
[0.01586491,
要乘以矩阵,您应该加载如下矩阵: matrix_1 = numpy.array(my_matrix_as_list_1) matrix_2 = numpy.array(my_matrix_as_list_2) 然后,您可以像这样进行矩阵乘法: 结果= numpy.dot(矩阵_1,矩阵_2)