# Factor Analysis using Python-经管之家官网！

>>

## TOP热门关键词

FactorAnalysisusingPython#TheFactorAnalysisalgorithmimportpylabasplimportnumpyasnpdeffactoranalysis(y,nRedDim):Ndata=np.shape(y)[0]N=np.shape(y)[1]y=y-y.mean(axis=0)C=np.cov(np.transpose(y))Cd=C.diago ...
 坛友互助群 扫码加入各岗位、行业、专业交流群

Factor Analysis using Python

1. # The Factor Analysis algorithm
2. import pylab as pl
3. import numpy as np

4. def factoranalysis(y,nRedDim):
5. Ndata = np.shape(y)[0]
6. N = np.shape(y)[1]

7. y = y-y.mean(axis=0)
8. C = np.cov(np.transpose(y))
9. Cd = C.diagonal()
10. Psi = Cd
11. scaling = np.linalg.det(C)**(1./N)

12. W = np.random.normal(0,np.sqrt(scaling/nRedDim),(N,nRedDim))

13. nits = 1000
14. oldL = -np.inf

15. for i in range(nits):

16. # E-step
17. A = np.dot(W,np.transpose(W)) + np.diag(Psi)
18. logA = np.log(np.abs(np.linalg.det(A)))
19. A = np.linalg.inv(A)

20. WA = np.dot(np.transpose(W),A)
21. WAC = np.dot(WA,C)
22. Exx = np.eye(nRedDim) - np.dot(WA,W) + np.dot(WAC,np.transpose(WA))

23. # M-step
24. W = np.dot(np.transpose(WAC),np.linalg.inv(Exx))
25. Psi = Cd - (np.dot(W,WAC)).diagonal()
26. #Sigma1 = (dot(transpose(y),y) - dot(W,WAC)).diagonal()/Ndata

27. tAC = (A*np.transpose(C)).sum()

28. L = -N/2*np.log(2.*np.pi) -0.5*logA - 0.5*tAC
29. if (L-oldL)<(1e-4):
30. print "Stop",i
31. break
32. print L
33. oldL = L
34. A = np.linalg.inv(np.dot(W,np.transpose(W))+np.diag(Psi))
35. Ex = np.dot(np.transpose(A),W)

36. return np.dot(y,Ex)

37. data = np.array([[0.1,0.1],[0.2,0.2],[0.3,0.3],[0.35,0.3],[0.4,0.4],[0.6,0.4],[0.7,0.45],[0.75,0.4],[0.8,0.35]])
38. newData = factoranalysis(data,2)
39. pl.plot(newData[:,0],newData[:,1],'.')
40. pl.show()

• Machine Learning: An Algorithmic Perspective (2nd Edition)
• http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.FactorAnalysis.html

「经管之家」APP：经管人学习、答疑、交友，就上经管之家！

## 人气文章

#### 本文标题：Factor Analysis using Python

1.凡人大经济论坛-经管之家转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。