91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 11KB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發布日期: 2021-05-05
  • 語言: 其他
  • 標簽:

資源簡介

內含主成分分析PCA代碼和測試數據

資源截圖

代碼片段和文件信息



import?numpy?as?np
import?matplotlib.pyplot?as?plt

def?TXTtoNumpy(TXTfilename?lableState=False?Print=False?delim?=?‘\t‘):
????‘‘‘

????:param?TXTfilename:?Path?about?TXT?file
????:param?lableState:?True?for?have?labels?of?data
????:param?print:?to?print?info?about?data
????:param?delim:?to?split?‘\t‘
????:return:
????‘‘‘
????TXTfr?=?open(TXTfilename)
????TXTList?=?TXTfr.readlines()
????stringArr?=?[line.strip().split(delim)?for?line?in?TXTList]


????n_examples?=?len(stringArr)

????if?lableState:
????????n_features?=?len(stringArr[0])-1
????????labels?=?np.zeros(n_examples)
????????labels?=?[int(line[n_features])?for?line?in?stringArr]
????else:
????????n_features?=?len(stringArr[0])

????if?Print:
????????print(“n_examples:?“?n_examples)
????????print(“n_features:?“?n_features)

????floatList?=?np.zeros((n_examples?n_features))

????for?i?in?range(0?n_features):
????????floatList[:i]?=?[float(line[i])?for?line?in?stringArr]

????if?lableState:
????????return?floatList?labels
????else:
????????return?floatList

def?pca(npArr?k?show?=?False):

????‘‘‘

????:param?npArr:?shape=(n_examples?n_features)
????:param?k:?to?keep?k?components
????:param?show:?True?to?show?figure?about?origData?and?reconData
????:return:?LowNpArr?loss
????‘‘‘
????#?Preprocessing

????n_examples?=?npArr.shape[0]
????n_features?=?npArr.shape[1]

????mean?=?np.zeros(n_features)
????std?=?np.zeros(n_features)

????StdMeanNpArr?=?np.zeros((n_examples?n_features))

????mean?=?np.average(npArr?axis=0).reshape(1n_features)
????std?=?np.std(npArr?axis=0).reshape(1n_features)
????StdMeanNpArr?=?(npArr?-?mean)?/?std

????#?pca
????sigma?=?np.cov(StdMeanNpArr?rowvar=0)
????eigValue?eigVects?=?np.linalg.eig(sigma)?#?獲得協方差矩陣的特征值,特征向量
????eigValInd?=?np.argsort(eigValue)?#?返回特征值從小到大排序的索引
????eigValInd?=?eigValInd[:-(k+1):-1]?#?從后向前一共取k個值的索引
????redEigVects?=?eigVects[:eigValInd]?#?取出指定索引的特征向量
????LowNpArr?=?np.dot(StdMeanNpArr?redEigVects)?#?原數據與選定的特征向量內積,得到降維數據
????reconNpArr?=?np.dot(LowNpArr?redEigVects.T)?#?重構數據

????redEigValue?=?eigValue[eigValInd]
????loss?=?1?-?np.sum(redEigValue)/np.sum(eigValue)?#?重構數據的損失程度
????print(“PCA?loss:?“?loss)

????if?show:
????????fig?=?plt.figure()
????????ax?=?fig.add_subplot(111)
????????ax.scatter(StdMeanNpArr[:?0]?StdMeanNpArr[:?1]?marker?=?‘^‘?c=‘red‘)
????????ax.scatter(reconNpArr[:0]reconNpArr[:1]?marker=‘o‘?c=‘blue‘)
????????plt.show()

????return?LowNpArr?loss




if?__name__?==?‘__main__‘:
????filename?=?‘./testSet3.txt‘
????npArr?lables?=?TXTtoNumpy(filename?lableState=True?Print=True)
????LowNpArr?loss?=?pca(npArr?k=1?show=True)














?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-04-25?09:08??PCA\
?????文件???????22076??2011-06-08?23:58??PCA\testSet3.txt
?????文件????????2812??2019-04-25?09:08??PCA\PCA.py

評論

共有 條評論

相關資源