Gretings我有一个文件matrix.txt,使用以下矩阵3x3 dtype float32
----------------- matrix.txt ------------
[[ 2.94795074e+00 3.15288849e-02 -8.67170450e+02]
[-2.21123258e-17 2.94877180e+00 -5.95651904e+02]
[ 1.49601560e-04 1.52843706e-04 1.00000000e+00]]
p_homo = np.array([[1], [1], [1]])
file = open('matrix.txt', 'r')
matrix_file_l =list()
matrix_file_l = file.read()
matrix_file = np.array(matrix_file_l, dtype="float32")
def mult_matrix(matrix1, matrix2):
matrixx = np.empty([len(matrix1), len(matrix2[0])])
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
matrixx[i][j] += matrix1[i][k] * matrix2[k][j]
return matrixx
matrix = mult_matrix(matrix_file, p_homo)
matrix_file是一个字符串,我不能转换为float32我尝试以多种方式进行转换
matrix_file = np.array(matrix_file_l, dtype="float32")
ValueError: could not convert string to float: '[[-5.62093010e+01...
这项工作对我来说...我使用numpy np.save
和np.load
而不是文件file = open('matrix.txt', 'w')
file.write(matrix)
matrix_value = file.read()
将我的矩阵保存在matrix.npy之后很容易进行操作,因为numpy将值保存为numpy.ndarray而不是字符串....
--------------- script1 ------------------
...
np.save('matrix', matrix_value)
...
---------- script2 ---------------
p_homo = np.array([[1], [1], [1]])
a= np.load('matrix.npy')
...
matrix = mult_matrix(a, p_homo)