I just installed python 2.7 on a computer which never had python on it. Been trying to see past questions about this problem but none of them resolved my situation. I tried dot() instead of matmal(), that wokrs but I do not get the answer I think I'm looking for.
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()
I keep getting AttributeError: 'module' object has no attribute 'matmul'.
You are running Numpy 1.9.2-8, but matmul
was not added until Numpy 1.10.
You should upgrade numpy to the latest version. I don't know what your environment looks like, but if you are using pip, you can run pip install --upgrade numpy
from the command line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With