【问题标题】:Load Matrices From File - Python从文件加载矩阵 - Python
【发布时间】:2018-11-22 14:10:08
【问题描述】:

我在将矩阵从文本文件加载到 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(self, asmatrix(other)) ValueError:形状(6,6)和(5,6)未对齐:6(dim 1)!= 5(dim 0) 请帮忙,我希望这个算法能正常工作。

【问题讨论】:

  • 文本是否与您在评论中提供的标题完全相同,标题为 'Matrix # 1[5x8] ?第二个矩阵呢。它是由 ----- 分隔的吗?你希望它最终看起来像什么?
  • @Vipluv 我刚刚把矩阵的例子写到帖子里了
  • 是您的单个矩阵行分布在两行上还是只是在这里发布矩阵文本的人工制品?
  • 请解释“它不起作用”是什么意思。您还应该按照之前 cmets 中的要求验证文本文件的确切格式。如果您阅读并关注How to create a Minimal, Complete, and Verifiable example,我们都会受益。
  • @Vipluv Single Matrix 分为两行

标签: python file matrix


【解决方案1】:

如前所述,我相信正则表达式可能是解析文本文件的最佳方式。 下面的代码适用于您提供的输入

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(matrix_1, matrix_2)

【讨论】:

  • 感谢佩德罗·托雷斯!我设法加载了矩阵。但是,我在它们的乘法过程中遇到了一个问题……我在问题描述中添加了代码。我会寻求帮助...
  • 我很高兴它有帮助!我没有看到你提到的乘法错误
  • 您能否详细说明如何更正代码?我对此非常关心。非常感谢!
【解决方案2】:

这些条件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(' ')])

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多