本文整理汇总了Python中sklearn.decomposition.PCA.ind方法的典型用法代码示例。如果您正苦于以下问题:Python PCA.ind方法的具体用法?Python PCA.ind怎么用?Python PCA.ind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.decomposition.PCA
的用法示例。
在下文中一共展示了PCA.ind方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_cfg
# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import ind [as 别名]
def from_cfg(cls, cfg):
'''
:param cfg: dictionary containing the parameters.
'''
grid = HDF5Interface(cfg["grid"], ranges=cfg["ranges"])
wl = grid.wl
min_v = grid.wl_header["min_v"]
if 'wl' in cfg:
low, high = cfg['wl']
ind = determine_chunk_log(wl, low, high) #Sets the wavelength vector using a power of 2
wl = wl[ind]
else:
ind = np.ones_like(wl, dtype="bool")
npix = len(wl)
m = len(grid.list_grid_points)
test_index = cfg['test_index']
if test_index < m:
#If the index actually corresponds to a spectrum in the grid, we're dropping it out. Otherwise,
#leave it in by simply setting test_index to something larger than the number of spectra in the grid.
m -= 1
fluxes = np.empty((m, npix))
z = 0
for i, spec in enumerate(grid.fluxes):
if i == test_index:
test_spectrum = spec[ind]
continue
fluxes[z,:] = spec[ind]
z += 1
#Normalize all of the fluxes to an average value of 1
#In order to remove interesting correlations
fluxes = fluxes/np.average(fluxes, axis=1)[np.newaxis].T
#Subtract the mean from all of the fluxes.
flux_mean = np.average(fluxes, axis=0)
fluxes -= flux_mean
#"Whiten" each spectrum such that the variance for each wavelength is 1
flux_std = np.std(fluxes, axis=0)
fluxes /= flux_std
pca = PCA()
pca.fit(fluxes)
comp = pca.transform(fluxes)
components = pca.components_
mean = pca.mean_
print("Shape of PCA components {}".format(components.shape))
if not np.allclose(mean, np.zeros_like(mean)):
import sys
sys.exit("PCA mean is more than just numerical noise. Something's wrong!")
#Otherwise, the PCA mean is just numerical noise that we can ignore.
ncomp = cfg['ncomp']
print("Keeping only the first {} components".format(ncomp))
pcomps = components[0:ncomp]
gparams = np.empty((m, 3))
z = 0
for i, params in enumerate(grid.list_grid_points):
if i == test_index:
test_params = np.array([params["temp"], params["logg"], params["Z"]])
continue
gparams[z, :] = np.array([params["temp"], params["logg"], params["Z"]])
z += 1
#Create w
w = np.empty((ncomp, m))
for i,pcomp in enumerate(pcomps):
for j,spec in enumerate(fluxes):
w[i,j] = np.sum(pcomp * spec)
pca = cls(wl, min_v, flux_mean, flux_std, pcomps, w, gparams)
pca.ind = ind
return pca