Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Explanation of the example process of PCA in python

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the explanation of the example process of PCA in python". In the daily operation, I believe that many people have doubts about the process of explaining the example of PCA in python. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the process explanation of PCA examples in python". Next, please follow the editor to study!

1. Normalization of input matrix.

2. Calculate the sample covariance matrix.

3. Solving the maximum eigenvalue specified by the covariance matrix corresponds to the eigenvector.

4. Determine the transformation matrix and solve the reduced-dimensional data.

Example

# / usr/nom/env python# _ * _ coding:utf-8_*_# @ Time: 2021-9-3 10 Time @ Author: a bigfish# @ FileName: maindemo13.py# @ Software: PyCharm import matplotlib.pyplot as pltimport numpy as npfrom pylab import * # first import data This section reads the analysis data def loadDataSet (filename, delim='\ t') from the storage list or unit: # the'\ t 'here represents the delimiter between different variables T represents the space typed by the tab key fr = open (filename) stringArr = [line.strip (). Split (delim) for line in fr.readlines ()] dataArr = [list (map (float, line) for line in stringArr] return np.mat (dataArr) # defines the pca analysis function def pca (dataset, topNfeat = 99999): # the maximum number of eigenvalues of topNfeat, usually not set Because the following visual analysis meanVals = np.mean (dataset, axis=0) # find the mean meanRemoved = dataset-meanVals # preprocess covMat = np.cov (meanRemoved, rowvar=0) # solve the input data covariance matrix eigVals, eigVects = np.linalg.eig (np.mat (covMat)) # solve the eigenvalues Eigenvector eigVaInd = np.argsort (eigVals) # sorts eigenvalues, defaults to ascending eigVaInd = eigVaInd [- 1eigVaInd-(topNfeat):-1] # reverse processing according to the specified number redEigVects = eigVects [:, eigVaInd] # select the corresponding eigenvector lowDataMat = meanRemoved * redEigVects # data dimensionality reduction Xhump recontMat = (lowDataMat * redEigVects.T) + meanVals # c for data reconstruction Optional options return lowDataMat, recontMat, eigVals # return data # define the feature value drawing function def plotEig (dataset, numFeat=20): mpl.rcParams ['font.sans-serif'] = [' Times NewRoman'] sumData = np.zeros ((1, numFeat)) dataset = dataset / sum (dataset) for i in range (numFeat): sumData [0, I] = sum (dataset [0: I]) X = np.linspace (1, numFeat) NumFeat) fig = plt.figure () ax = fig.add_subplot (211b) ax.plot (X, (sumData*100). T, 'rkozy') mpl.rcParams ['font.sans-serif'] = [' SimHei'] plt.ylabel ('cumulative variance percentage') ax2 = fig.add_subplot (212) ax2.plot (X.T, (dataset [0: numFeat] .T) * 100 Plt.xlabel ('principal component fraction') plt.ylabel ('variance percentage') plt.show () # defines the raw data and the first principal component rendering function def plotData (OrigData, recData): import matplotlib.pyplot as plt fig = plt.figure () ax = fig.add_subplot (111) ax.scatter (OrigData [:, 0] .flatten (). A [0], OrigData [: 1] .flatten () .A [0], cymbals bluetooth dagger markerboards'^', swatches 90) ax.scatter (recData [:, 0] .flatten () .A [0], recData [:, 1] .flatten () .A [0], cymbals blueprints, marker='o',s=90) plt.show () so far The study on "explaining the example process of PCA in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report