本文整理汇总了Python中sklearn.decomposition.PCA.partial_fit方法的典型用法代码示例。如果您正苦于以下问题:Python PCA.partial_fit方法的具体用法?Python PCA.partial_fit怎么用?Python PCA.partial_fit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.decomposition.PCA
的用法示例。
在下文中一共展示了PCA.partial_fit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pca_opt
# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import partial_fit [as 别名]
def pca_opt(sparseArray, pcaModel=None, svdopt=None):
array = sparseArray.toarray()
if pcaModel is None:
if svdopt is not None:
ReadData.svdOpt = svdopt
print (ReadData.svdOpt)
newSize = len(array[0]) / ReadData.svdOpt
print(newSize)
if len(array) < 3000:
pca = PCA(n_components=newSize)
Xtransformed = pca.fit_transform(array)
Xtransformed = sparse.csr_matrix(Xtransformed)
else:
chunkSize = 20
iter = int(len(array)/chunkSize)
chunks = [array[i:i + chunkSize] for i in range(0, iter*chunkSize, chunkSize)]
pca = IncrementalPCA(n_components=newSize)
for chunk in chunks:
pca.partial_fit(chunk)
Xtransformed = None
for chunk in chunks:
Xchunk = pca.transform(chunk)
if Xtransformed == None:
Xtransformed = Xchunk
else:
Xtransformed = np.vstack((Xtransformed, Xchunk))
Xtransformed = sparse.csr_matrix(Xtransformed)
return pca, Xtransformed
else:
if len(array) < 2900:
Xtransformed = pcaModel.transform(array)
Xtransformed = sparse.csr_matrix(Xtransformed)
else:
chunkSize = 2000
chunks = [array[i:i + chunkSize] for i in range(0, len(array), chunkSize)]
Xtransformed = None
for chunk in chunks:
Xchunk = pcaModel.transform(chunk)
if Xtransformed == None:
Xtransformed = Xchunk
else:
Xtransformed = np.vstack((Xtransformed, Xchunk))
Xtransformed = sparse.csr_matrix(Xtransformed)
return pcaModel, Xtransformed