当前位置: 首页>>代码示例>>Python>>正文


Python FactorAnalysis.n_components方法代码示例

本文整理汇总了Python中sklearn.decomposition.FactorAnalysis.n_components方法的典型用法代码示例。如果您正苦于以下问题:Python FactorAnalysis.n_components方法的具体用法?Python FactorAnalysis.n_components怎么用?Python FactorAnalysis.n_components使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.decomposition.FactorAnalysis的用法示例。


在下文中一共展示了FactorAnalysis.n_components方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: fit_factor_analysis

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def fit_factor_analysis(percentage=0.8):
    """
    Runs the factor analysis.

    Parameters:

        percentage: float, default:0.8

        The percentage of the cumulative sum of the eigenvalues to be held. This number defines the number of loading factors in the analysis.

    Returns:
        
        X: array of floats [n_samples,n_factors]

            The transformed data after the factor analysis.

        components: array of floats [n_factors,n_samples]

            The components of the factor analysis
    """
    fa = FactorAnalysis()
    fa.fit(data)
    C = fa.get_covariance()
    l,e = np.linalg.eigh(C)
    cs = np.cumsum(l[::-1])/np.sum(l)
    n = np.sum(cs<percentage)

    fa.n_components = n
    X_ = fa.fit_transform(data)
    components = fa.components_
    return X_,components
开发者ID:pedropazzini,项目名称:factor_analysis,代码行数:33,代码来源:factor_analysis_script.py

示例2: compute_scores

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def compute_scores(X):
    pca = PCA()
    fa = FactorAnalysis()

    pca_scores, fa_scores = [], []
    for n in n_components:
        pca.n_components = n
        fa.n_components = n
        pca_scores.append(np.mean(cross_val_score(pca, X)))
        fa_scores.append(np.mean(cross_val_score(fa, X)))

    return pca_scores, fa_scores
开发者ID:jamesshocker,项目名称:dukhi_saibin,代码行数:14,代码来源:Kaggle_pca.py

示例3: compute_scores

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def compute_scores(X, n_components):
    pca = PCA()
    fa = FactorAnalysis()

    pca_scores, fa_scores = [], []
    for n in n_components:
        print 'Processing dimension {}'.format(n)
        pca.n_components = n
        fa.n_components = n
        pca_scores.append(np.mean(cross_val_score(pca, X)))
        fa_scores.append(np.mean(cross_val_score(fa, X)))

    return pca_scores, fa_scores
开发者ID:stmarcell,项目名称:napwigner,代码行数:15,代码来源:extract_section.py

示例4: compute_scores

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def compute_scores(X, n_components):
  """
  This is the "y" data of the plots -- the CV scores.
  """
  pca = PCA()
  fa = FactorAnalysis()
  
  pca_scores, fa_scores = [], []
  for n in n_components:
    pca.n_components = n
    fa.n_components = n
    pca_scores.append(np.mean(cross_val_score(pca, X)))
    fa_scores.append(np.mean(cross_val_score(fa, X)))
  
  return pca_scores, fa_scores
开发者ID:acsutt0n,项目名称:Statistics,代码行数:17,代码来源:PCA_factor-analysis_scedastic.py

示例5: compute_scores

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def compute_scores(X, n_components):
    pca = PCA()
    fa = FactorAnalysis()

    pca_scores, fa_scores = [], []
    for n in n_components:
    	start = time.time()
        pca.n_components = n
        fa.n_components = n
        pca_scores.append(np.mean(cross_val_score(pca, X)))
        fa_scores.append(np.mean(cross_val_score(fa, X)))
        end = time.time()
        print 'PCA scores (%3d)' % n, pca_scores
        print 'FA  scores (%3d)' % n, fa_scores
        print 'TIME:           ', end-start

    return pca_scores, fa_scores
开发者ID:matijaSos,项目名称:tiny-binner,代码行数:19,代码来源:tetra.py

示例6: factor_analysis

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def factor_analysis(tests):
	from sklearn.decomposition import FactorAnalysis
	from sklearn.cross_validation import cross_val_score
	
	matrix = correct_matrix(tests,kind='ctrl')
	print(matrix.shape)
	# matrix must have a number of rows divisible by 3.  
	# if it does not, eliminate some rows, or pass cv=a to cross_val_score,
	# where 'a' is a number by which the number of rows is divisible.  
	fa = FactorAnalysis()
	fa_scores = []
	n_components = np.arange(1,41)
	for n in n_components:
		fa.n_components = n
		fa_scores.append(np.mean(cross_val_score(fa, matrix)))

	plt.plot(n_components,fa_scores)
	
	return n_components,fa_scores
开发者ID:rgerkin,项目名称:upsit,代码行数:21,代码来源:__init__.py

示例7: test_factor_analysis

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def test_factor_analysis():
    # Test FactorAnalysis ability to recover the data covariance structure
    rng = np.random.RandomState(0)
    n_samples, n_features, n_components = 20, 5, 3

    # Some random settings for the generative model
    W = rng.randn(n_components, n_features)
    # latent variable of dim 3, 20 of it
    h = rng.randn(n_samples, n_components)
    # using gamma to model different noise variance
    # per component
    noise = rng.gamma(1, size=n_features) * rng.randn(n_samples, n_features)

    # generate observations
    # wlog, mean is 0
    X = np.dot(h, W) + noise

    assert_raises(ValueError, FactorAnalysis, svd_method='foo')
    fa_fail = FactorAnalysis()
    fa_fail.svd_method = 'foo'
    assert_raises(ValueError, fa_fail.fit, X)
    fas = []
    for method in ['randomized', 'lapack']:
        fa = FactorAnalysis(n_components=n_components, svd_method=method)
        fa.fit(X)
        fas.append(fa)

        X_t = fa.transform(X)
        assert_equal(X_t.shape, (n_samples, n_components))

        assert_almost_equal(fa.loglike_[-1], fa.score_samples(X).sum())
        assert_almost_equal(fa.score_samples(X).mean(), fa.score(X))

        diff = np.all(np.diff(fa.loglike_))
        assert_greater(diff, 0., 'Log likelihood dif not increase')

        # Sample Covariance
        scov = np.cov(X, rowvar=0., bias=1.)

        # Model Covariance
        mcov = fa.get_covariance()
        diff = np.sum(np.abs(scov - mcov)) / W.size
        assert_less(diff, 0.1, "Mean absolute difference is %f" % diff)
        fa = FactorAnalysis(n_components=n_components,
                            noise_variance_init=np.ones(n_features))
        assert_raises(ValueError, fa.fit, X[:, :2])

    f = lambda x, y: np.abs(getattr(x, y))  # sign will not be equal
    fa1, fa2 = fas
    for attr in ['loglike_', 'components_', 'noise_variance_']:
        assert_almost_equal(f(fa1, attr), f(fa2, attr))

    fa1.max_iter = 1
    fa1.verbose = True
    assert_warns(ConvergenceWarning, fa1.fit, X)

    # Test get_covariance and get_precision with n_components == n_features
    # with n_components < n_features and with n_components == 0
    for n_components in [0, 2, X.shape[1]]:
        fa.n_components = n_components
        fa.fit(X)
        cov = fa.get_covariance()
        precision = fa.get_precision()
        assert_array_almost_equal(np.dot(cov, precision),
                                  np.eye(X.shape[1]), 12)
开发者ID:1992huanghai,项目名称:scikit-learn,代码行数:67,代码来源:test_factor_analysis.py

示例8: simulate

# 需要导入模块: from sklearn.decomposition import FactorAnalysis [as 别名]
# 或者: from sklearn.decomposition.FactorAnalysis import n_components [as 别名]
def simulate(data, factors=0, maxtrials=5, multiplier=1, seed=0):
    n = len(data)
    dim = len(data[0])
    simulated = np.zeros((n,dim))
    distribution = np.zeros((n,dim))
    iteration = 0
    BestRMSR = 1
    trialsWithoutImprovement = 0

    #apply distribution from supplied data
    distribution = data.copy()
    TargetCorr = corr(data.T)
    IntermidiateCorr = TargetCorr.copy()
    BestCorr = IntermidiateCorr
    #print data.shape
    #print simulated.shape
    #print TargetCorr, TargetCorr.shape

    if(factors == 0):
        eigvalsObserved = np.linalg.eigvals(IntermidiateCorr)
        eigvalsRandom = np.zeros((100,dim))
        randomData = np.zeros((n,dim))

        for i in range(0, 100):
            for j in range(0, dim):
                randomData[:, j] = np.random.permutation(distribution[:, j])
            eigvalsRandom[i, :] = np.linalg.eigvals(corr(randomData.T))
        eigvalsRandom = np.mean(eigvalsRandom, axis=0)
        factors = max(1, np.sum(eigvalsObserved > eigvalsRandom))

    #steps 5,6
    SharedComp = np.random.normal(0, 1, (n, factors))
    UniqueComp = np.random.normal(0, 1, (n, dim))
    SharedLoad = np.zeros((dim, factors))
    UniqueLoad = np.zeros(dim)

    while trialsWithoutImprovement < maxtrials:
        iteration += 1

        #Calculate factor loadings and apply to reproduce desired correlations (steps 7, 8)
        fa = FactorAnalysis()
        fa.n_components = factors
        fa.fit(IntermidiateCorr)
        FactLoadings = fa.components_.T
        #print FactLoadings.shape

        if (factors == 1):
            SharedLoad[:, 0] = FactLoadings[:, 0]
        else:
            SharedLoad = FactLoadings
        #print SharedLoad

        SharedLoad = np.clip(SharedLoad, -1, 1)
        #print SharedLoad

        if (SharedLoad[0, 0] < 0):
            SharedLoad *= -1
        #print SharedLoad

        SharedLoadSq = SharedLoad * SharedLoad
        #print SharedLoadSq

        for i in range(0, dim):
            SharedLoadSum = np.sum(SharedLoadSq[i, :])
            if(SharedLoadSum < 1):
                UniqueLoad[i] = 1 - SharedLoadSum
            else:
                UniqueLoad[i] = 0
        UniqueLoad = np.sqrt(UniqueLoad)
        #print UniqueLoad

        MergedShare = np.dot(SharedComp, SharedLoad.T)
        for i in range(0, dim):
            simulated[:, i] = MergedShare[:, i] + UniqueComp[:, i]*UniqueLoad[i]
        #print simulated

        #Replace normal with nonnormal distributions (step 9)
        for i in range(0, dim):
            indices = np.argsort(simulated[:, i])
            simulated = np.array(simulated)[indices]
            simulated[:, i] = distribution[:, i]
        #print simulated
        #print distribution

        #Calculate RMSR correlation, compare to lowest value, take appropriate action (steps 10, 11, 12)
        ReproducedCorr = corr(simulated.T)
        ResidualCorr = TargetCorr - ReproducedCorr;
        #print ResidualCorr

        RMSR = np.sqrt(np.sum(np.tril(ResidualCorr) ** 2) / (0.5 * (dim*dim - dim)))
        #print RMSR

        if (RMSR < BestRMSR):
            BestRMSR = RMSR
            BestCorr = IntermidiateCorr
            BestRes = ResidualCorr
            IntermidiateCorr = IntermidiateCorr + multiplier*ResidualCorr
            trialsWithoutImprovement = 0
        else:
            trialsWithoutImprovement += 1
#.........这里部分代码省略.........
开发者ID:ansteh,项目名称:multivariate,代码行数:103,代码来源:nonnormal.py


注:本文中的sklearn.decomposition.FactorAnalysis.n_components方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。