本文整理汇总了Python中sklearn.decomposition.SparsePCA.components_方法的典型用法代码示例。如果您正苦于以下问题:Python SparsePCA.components_方法的具体用法?Python SparsePCA.components_怎么用?Python SparsePCA.components_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.decomposition.SparsePCA
的用法示例。
在下文中一共展示了SparsePCA.components_方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spca
# 需要导入模块: from sklearn.decomposition import SparsePCA [as 别名]
# 或者: from sklearn.decomposition.SparsePCA import components_ [as 别名]
def spca(data, num_components=None, alpha=1):
# creates a matrix with sparse principal component analysis
# build matrix with all data
data = [d.flatten() for d in data if not any(isnan(d))]
datamatrix = row_stack(data)
# center data
cdata = datamatrix - mean(datamatrix, axis=0)
if num_components is None:
num_components = cdata.shape[0]
# do spca on matrix
spca = SparsePCA(n_components=num_components, alpha=alpha)
spca.fit(cdata)
# normalize components
components = spca.components_.T
for r in xrange(0,components.shape[1]):
compnorm = numpy.apply_along_axis(numpy.linalg.norm, 0, components[:,r])
if not compnorm == 0:
components[:,r] /= compnorm
components = components.T
# calc adjusted explained variance from "Sparse Principal Component Analysis" by Zou, Hastie, Tibshirani
spca.components_ = components
#nuz = spca.transform(cdata).T
nuz = ridge_regression(spca.components_.T, cdata.T, 0.01, solver='dense_cholesky').T
#nuz = dot(components, cdata.T)
q,r = qr(nuz.T)
cumulative_var = []
for i in range(1,num_components+1):
cumulative_var.append(trace(r[0:i,]*r[0:i,]))
explained_var = [math.sqrt(cumulative_var[0])]
for i in range(1,num_components):
explained_var.append(math.sqrt(cumulative_var[i])-math.sqrt(cumulative_var[i-1]))
order = numpy.argsort(explained_var)[::-1]
components = numpy.take(components,order,axis=0)
evars = numpy.take(explained_var,order).tolist()
#evars = numpy.take(explained_var,order)
#order2 = [0,1,2,4,5,7,12,19]
#components = numpy.take(components,order2,axis=0)
#evars = numpy.take(evars,order2).tolist()
return components, evars