AttributeError:'module'对象没有属性'matmul'

时间:2018-02-06 00:21:32

标签: python python-2.7

我刚刚在没有python的计算机上安装了python 2.7。一直在试图查看有关此问题的过去问题,但没有一个解决了我的情况。我尝试了dot()而不是matmal(),但是我没有得到我认为我正在寻找的答案。

from numpy import linalg as la
import numpy as np

def myCov(a):
at = a.T
[rows,cols]=np.shape(at);
#    print("rows=", rows, "cols=", cols)
rv = np.ndarray(shape=(rows,rows), dtype=float, order='F')
for i in range(rows):
    for j in range(rows):
        rv[i][j] = (np.dot(at[i]-np.mean(at[i]), at[j]-    np.mean(at[j])))/cols
return rv

def pro1(A):
    print("\n\nproblem1:\n")
    c1 = np.cov(A.T, None, True, True);
    c2 = myCov(A);
    print("c1=\n", c1)
    print("c2=\n", c2)
    print("c1=c2", abs(c1-c2) < 0.00001)

def pro2(A):
    print("\n\nproblem2:\n")
    B = myCov(A)
    eigvalues, eigvectors = la.eig(B)

    eigvectors12 = eigvectors[:, 0:2]
    eigvec1 = eigvectors12[:,0]
    eigvec2 = eigvectors12[:,1]

    print("eigvec1=", eigvec1)
    print("eigvec2=", eigvec2)
    projA = np.matmul(A , eigvectors12)

    print("reduced data=", projA)

    covProjA = myCov(projA)

    print("covariance matrix=", covProjA)
    varProjA = np.sum(covProjA)
    varProjA = np.matmul(np.matmul(eigvec1, B), eigvec1.T) +      np.matmul(np.matmul(eigvec2, B), eigvec2.T)

    print("variance=", varProjA)
    print("variance=eigv1+eigv2", abs(varProjA - eigvalues[0] - eigvalues[1]) < 0.00001)

    print ("eigvals=", eigvalues)
    print ("eigvectors=", eigvectors)
def main():
    float_formatter = lambda x: "%.4f" % x
    np.set_printoptions(formatter={'float_kind':float_formatter})
    A = np.loadtxt('magic04.txt', delimiter=',', usecols=range(10))
    print ("input matrix=",A)
    pro1(A)
    pro2(A)
#print("start")
main()

我一直得到AttributeError:'module'对象没有属性'matmul'。

1 个答案:

答案 0 :(得分:6)

您正在运行Numpy 1.9.2-8,但在Numpy 1.10之前未添加matmul

您应该将numpy升级到最新版本。我不知道您的环境是什么样的,但如果您使用的是pip,则可以从命令行运行pip install --upgrade numpy