本文整理汇总了Python中sklearn.discriminant_analysis.LinearDiscriminantAnalysis.explained_variance_ratio_方法的典型用法代码示例。如果您正苦于以下问题:Python LinearDiscriminantAnalysis.explained_variance_ratio_方法的具体用法?Python LinearDiscriminantAnalysis.explained_variance_ratio_怎么用?Python LinearDiscriminantAnalysis.explained_variance_ratio_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.discriminant_analysis.LinearDiscriminantAnalysis
的用法示例。
在下文中一共展示了LinearDiscriminantAnalysis.explained_variance_ratio_方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _dimReduce
# 需要导入模块: from sklearn.discriminant_analysis import LinearDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.LinearDiscriminantAnalysis import explained_variance_ratio_ [as 别名]
def _dimReduce(df, method='pca', n_components=2, labels=None, standardize=False, smatFunc=None, ldaShrinkage='auto'):
if method == 'kpca':
"""By using KernelPCA for dimensionality reduction we don't need to impute missing values"""
if smatFunc is None:
smatFunc = corrTSmatFunc
pca = KernelPCA(kernel='precomputed', n_components=n_components)
smat = smatFunc(df).values
xy = pca.fit_transform(smat)
pca.components_ = pca.alphas_
pca.explained_variance_ratio_ = pca.lambdas_ / pca.lambdas_.sum()
return xy, pca
elif method == 'pca':
if standardize:
normed = df.apply(lambda vec: (vec - vec.mean())/vec.std(), axis=0)
else:
normed = df.apply(lambda vec: vec - vec.mean(), axis=0)
pca = PCA(n_components=n_components)
xy = pca.fit_transform(normed)
return xy, pca
elif method == 'lda':
if labels is None:
raise ValueError('labels needed to perform LDA')
if standardize:
normed = df.apply(lambda vec: (vec - vec.mean())/vec.std(), axis=0)
else:
normed = df.apply(lambda vec: vec - vec.mean(), axis=0)
if df.shape[1] > df.shape[0]:
"""Pre-PCA step"""
ppca = PCA(n_components=int(df.shape[0]/1.5))
normed = ppca.fit_transform(df)
lda = LinearDiscriminantAnalysis(solver='eigen', shrinkage=ldaShrinkage, n_components=n_components)
lda.fit(normed, labels.values)
lda.explained_variance_ratio_ = np.abs(lda.explained_variance_ratio_) / np.abs(lda.explained_variance_ratio_).sum()
xy = lda.transform(normed)
elif method == 'pls':
if labels is None:
raise ValueError('labels needed to perform PLS')
if standardize:
normed = df.apply(lambda vec: (vec - vec.mean())/vec.std(), axis=0)
else:
normed = df.apply(lambda vec: vec - vec.mean(), axis=0)
pls = PLSRegression(n_components=n_components)
pls.fit(normed, labels)
pls.explained_variance_ratio_ = np.zeros(n_components)
xy = pls.x_scores_
return xy, pls