找回密码
 立即注册
首页 python python-基础 查看内容

乘以Matricies


我正在从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矩阵。任何帮助将不胜感激!

解决方法


您可以从输出中看到数组的元素不是数字,而是字符串:
[['1.0' '0']


确保float(foo)在操作之前将组件转换为数字。


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册