我正在从CSV导入数据并尝试对其进行分析,但是我需要将矩阵与其转置相乘。
def C_matrices(F11_list, F12_list, F21_list, F22_list):
C11_list = []
C12_list = []
C21_list = []
C22_list = []
for F11 in F11_list:
pos = F11_list.index(F11)
F = np.array([[float(F11), F12_list[pos]], [F21_list[pos], F22_list[pos]]])
print F
Ft = F.transpose()
print Ft
C = np.matmul(F, Ft)
C11_list.append(C[:1, :1])
C12_list.append(C[1:, 2:])
print C[:1, :1]
print C[1:, 2:]
break
我得到:
[['1.0' '0']
Traceback (most recent call last):
['0' '1']]
File "C:/Users/lukec/Desktop/School/3rd Year/1st Quarter/BENG 110/Project/read_F.py", line 48, in <module>
[['1.0' '0']
control()
['0' '1']]
File "C:/Users/lukec/Desktop/School/3rd Year/1st Quarter/BENG 110/Project/read_F.py", line 45, in control
C_matrices(F11_list, F12_list, F21_list, F22_list)
File "C:/Users/lukec/Desktop/School/3rd Year/1st Quarter/BENG 110/Project/read_F.py", line 34, in C_matrices
C = np.matmul(F, Ft)
TypeError: invalid data type for einsum
我不确定einsum为何失败,我得到2x2矩阵乘以2x2矩阵。任何帮助将不胜感激!
答案 0 :(得分:3)
从输出中可以看到,数组的元素不是数字,而是字符串
[['1.0' '0']
在操作之前,请确保将组件转换为数字float(foo)