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


Python PCA.n_components方法代码示例

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


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

示例1: test_pca

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
def test_pca():
    """PCA on dense arrays"""
    pca = PCA(n_components=2)
    X = iris.data
    X_r = pca.fit(X).transform(X)
    np.testing.assert_equal(X_r.shape[1], 2)

    X_r2 = pca.fit_transform(X)
    assert_array_almost_equal(X_r, X_r2)

    pca = PCA()
    pca.fit(X)
    assert_almost_equal(pca.explained_variance_ratio_.sum(), 1.0, 3)

    X_r = pca.transform(X)
    X_r2 = pca.fit_transform(X)

    assert_array_almost_equal(X_r, X_r2)

    # 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]]:
        pca.n_components = n_components
        pca.fit(X)
        cov = pca.get_covariance()
        precision = pca.get_precision()
        assert_array_almost_equal(np.dot(cov, precision), np.eye(X.shape[1]), 12)
开发者ID:Garrett-R,项目名称:scikit-learn,代码行数:29,代码来源:test_pca.py

示例2: compute_scores

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
def compute_scores(X):
    pca = PCA()
    pca_scores = []
    for n in n_components:
        pca.n_components = n
        pca.fit(X)
        pca_scores.append(pca.explained_variance_ratio_)
        # pca_scores.append(np.mean(cross_val_score(pca, X)))

    return pca_scores
开发者ID:phihes,项目名称:judgmentHMM,代码行数:12,代码来源:PCA.py

示例3: compute_scores

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA 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

示例4: reduceDimensionalityToTwo

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
def reduceDimensionalityToTwo(matrix):
    """
    Reduces the dimension via PCA (to 2) for visualisation.

    :param matrix (NumPy array) - The feature matrix.
    :return: coordinates (NumPy array) - Array containing the x and y coordinates.
    """
    pca = PCA()
    pca.n_components = 2
    coordinates = pca.fit_transform(matrix)

    return coordinates
开发者ID:NamanJn,项目名称:guide-cluster,代码行数:14,代码来源:readData.py

示例5: compute_scores

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA 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

示例6: compute_scores

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA 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

示例7: computeScores

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
    def computeScores(self, X):
        """
        Computes the scores for a given X feature vector considering various
        numbers of features
        """
        pca = PCA()
        pca_scores = []

        for n in self.n_components:
            print "Computing score for", n, "components"
            sys.stdout.flush()
            
            pca.n_components = n
            pca_scores.append(np.mean(cross_val_score(pca, X)))

        return pca_scores
开发者ID:urielmandujano,项目名称:ensemble_santander,代码行数:18,代码来源:pca.py

示例8: compute_scores

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA 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

示例9: pca

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

	normalizaTFXIDF (matriztfxidf)
	
	vetor = []
	
	# testa qual o melhor numero de componentes	que representa 95% dos dados
	for i in range(len(matriztfxidf[0])):
		num_pca_components = i
		pca = PCA(num_pca_components)
		pca.fit(matriztfxidf)
		PCA(copy=True, whiten=False)

		# guardo em um vetor para montar graficos
		# vetor.append(sum(pca.explained_variance_ratio_))
		
		if(sum(pca.explained_variance_ratio_) >= 0.95):
			#print "Numero de colunas: ", len(np.transpose(matriztfxidf))
			#print "Numero de linhas: ", len(matriztfxidf)
			#print "Numero de PCA componentes (95%): ",num_pca_components
			#print "Componentes"
			#print pca.explained_variance_ratio_

			pca.n_components = num_pca_components
			pca.fit(matriztfxidf)
			matrizReduzida = pca.fit_transform(matriztfxidf)
			#print "Tamanho da matriz redimensionada: ", matrizReduzida.shape
			#print "Nova matriz reduzida: ", matrizReduzida			
			break

	#inverso = pca.inverse_transform(matrizReduzida)
	
	#print "INVERSO"
	#print inverso
	
	#return matrizReduzida
	return num_pca_components
开发者ID:isabellavieira57,项目名称:bugReportsSummarization,代码行数:39,代码来源:mineracao.py

示例10: open

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
# #open file and write the results into a csv file
# myfile = open('results2.csv', 'wb')
# wr = csv.writer(myfile, dialect='excel')
# #wr.writerows(prediction)
# for row in prediction:
# 	wr.writerow([row])


#######################################With PCA#########################
#perform PCA
pca = PCA(n_components=22)
pca.fit(X_train)
print(pca.explained_variance_ratio_)

#from the variance ratio we choose the first 15 variables 
pca.n_components = 15
X_reduced = pca.fit_transform(X_train)
X_reduced.shape

# separate the train data into differentes samples
kf = KFold(len(X_reduced), n_folds=2)

score_model1 = []

#itirate the samples
for train_index, test_index in kf:

	#fit the train data
	regr.fit(X_reduced[train_index], Y_train[train_index])

	#Score with test data that we got from the kfold
开发者ID:nesbtesh,项目名称:Machine-Learnign-With-Sklearn,代码行数:33,代码来源:LogisticRregression.py

示例11: main

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
def main():

    conn = MySQLdb.connect(
                user="root",
                passwd="",
                db="Player_Team_Data",
                cursorclass=MySQLdb.cursors.DictCursor)


    # In[97]:
    # read in AllPlayerNames .csv from basketball-reference
    df_all_players = pd.read_csv('~/Insight/Players100.csv')
        # Remove rows that were separated by random 'Player' entries
    df_all_players = df_all_players[df_all_players.Name != 'Player']
        
    name_list=list(df_all_players.values)
    new_name_list = []
        
    for name in name_list:
            # convert entries to strings
        name = str(name)
        new_name_list.append(name)
            
    unique_name_list=list(set(new_name_list))
    unique_name_list.sort()
    name_list_final = []
    for name in unique_name_list:
        match = re.search('([\w\.\-\']+) ([\w\.\-]+)', name)
        prename = match.group(1).lower()[1:] + ' ' + match.group(2).lower()
        name_list_final.append(prename)

    name_list_fin = [x for x in name_list_final if x != 'george hill']
    accuracy_list=[]
    for player in name_list_fin:
        print player
        cmd_Rk= 'SELECT Rk FROM NBA_player_data WHERE Player_Name IN (\'' + player + '\')  AND Year IN (\'2015\');'
        df_Rk = pd.read_sql(cmd_Rk, con=conn) 

        f_pre = []
        f_avg = []
        f_tru = []
        for Rki in df_Rk.Rk:
            Rk = str(Rki)
            print Rk
            cmd_target_2015 = 'SELECT PTS,3P,TRB,AST,STL,BLK,TOV FROM NBA_player_data WHERE Player_Name IN (\'' + player + '\') AND Year IN (\'2015\') AND Rk < '+Rk+' ;'
            cmd_target_2014 = 'SELECT PTS,3P,TRB,AST,STL,BLK,TOV FROM NBA_player_data WHERE Player_Name IN (\'' + player + '\') AND Year IN (\'2014\') AND Rk >= '+Rk+' ;'
            cmd_train_2015 = 'SELECT Rk,Home_Away,DateDiff,TeamID,Win,OPPG,OTPR,O3Ppercent,ORPG,OBPG,OSPG,DEF,O3PM,OFGpercent,OTPG,OAPG,TPG,SPG,TRBR,OBLKpercent FROM NBA_player_data WHERE Player_Name IN (\'' + player + '\') AND Year IN (\'2015\') AND Rk < '+Rk+';'
            cmd_train_2014 = 'SELECT Rk,Home_Away,DateDiff,TeamID,Win,OPPG,OTPR,O3Ppercent,ORPG,OBPG,OSPG,DEF,O3PM,OFGpercent,OTPG,OAPG,TPG,SPG,TRBR,OBLKpercent FROM NBA_player_data WHERE Player_Name IN (\'' + player + '\') AND Year IN (\'2014\') AND Rk >= '+Rk+';'
            cmd_operate = 'SELECT Rk,Home_Away,DateDiff,TeamID,Win,OPPG,OTPR,O3Ppercent,ORPG,OBPG,OSPG,DEF,O3PM,OFGpercent,OTPG,OAPG,TPG,SPG,TRBR,OBLKpercent FROM NBA_player_data WHERE Player_Name IN (\'' + player + '\') AND Year IN (\'2015\') AND Rk = '+Rk+';'
            cmd_truth = 'SELECT PTS,3P,TRB,AST,STL,BLK,TOV FROM NBA_player_data WHERE Player_Name IN (\'' + player + '\') AND Year IN (\'2015\') AND Rk = '+Rk+' ;'

            df_target_2015 = pd.read_sql(cmd_target_2015, con=conn) 
            df_target_2014 = pd.read_sql(cmd_target_2014, con=conn) 
            df_train_2015 = pd.read_sql(cmd_train_2015, con=conn) 
            df_train_2014 = pd.read_sql(cmd_train_2014, con=conn) 
            df_operate = pd.read_sql(cmd_operate, con=conn) 
            df_truth = pd.read_sql(cmd_truth, con=conn) 
            df_truth = df_truth.applymap(lambda x: float(x))

            df_target=pd.concat([df_target_2014, df_target_2015],ignore_index=True)
            df_train=pd.concat([df_train_2014, df_train_2015],ignore_index=True)
            df_target = df_target.applymap(lambda x:float(x))
            df_train = df_train.applymap(lambda x:float(x))
            df_target_2015 = df_target_2015.applymap(lambda x: float(x))

            df_inquire = df_operate.applymap(lambda x:float(x))
            df_train_plus_inquire=pd.concat([df_train, df_inquire])
            df_raw = df_train_plus_inquire.reindex()
            df_raw_scaled = df_raw.copy()
            df_raw_scaled = df_raw_scaled.applymap(lambda x: np.log(x))
            df_raw_transform = df_raw.copy()

            df_raw_scaled = df_raw_scaled.apply(lambda x:preprocessing.StandardScaler().fit(x).transform(x))
            df_raw_transform = df_raw_transform.apply(lambda x:preprocessing.StandardScaler().fit(x))

            pca = PCA()
            pca.fit(df_raw_scaled)
            pca.n_components=7
            train_reduced = pca.fit_transform(df_raw_scaled)
            df_train_reduced=pd.DataFrame(train_reduced)
            df_evaluate = df_train_reduced.tail(1)
            df_train_scaled = df_train_reduced.iloc[:-1]


            # rf = RandomForestRegressor(n_estimators=100)
            # rf.fit(df_train_scaled, df_target)
            # predictions = rf.predict(df_evaluate).round()[0]
            
            PTS = LinR()
            PTS.fit(df_train_scaled, df_target.PTS)
            pPTS = PTS.predict(df_evaluate)
            REB = LinR()
            REB.fit(df_train_scaled, df_target.TRB)
            pREB = REB.predict(df_evaluate)
            AST = LinR()
            AST.fit(df_train_scaled, df_target.AST)
            pAST = AST.predict(df_evaluate)
            TP = LinR()
            TP.fit(df_train_scaled, df_target['3P'])
            pTP = TP.predict(df_evaluate)
#.........这里部分代码省略.........
开发者ID:wongmi22,项目名称:Insight,代码行数:103,代码来源:Validation_Lm.py

示例12: genfromtxt

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
from src.Utility.ScatterWithHistPlot import ScatterWithHistPlot

__author__ = 'stanley'

from sklearn.decomposition import PCA, KernelPCA
from numpy import genfromtxt
from sklearn import preprocessing

nbaData = genfromtxt('../../NBA2012-15/Classification/NBA12_14.csv', delimiter=',')

label = nbaData[:,0]
features = nbaData[:,1:]

pca = PCA()
pca.fit(features)

#Print out variance
print pca.explained_variance_ratio_

# plot first 2 components
pca.n_components=2
f_reduced = pca.fit_transform(features)


showGraph = ScatterWithHistPlot()
showGraph.plot(f_reduced, label)


开发者ID:Sandy4321,项目名称:NBA_Machine-Learning,代码行数:28,代码来源:PCA.py

示例13: TfidfVectorizer

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import n_components [as 别名]
        card = card.split(' ')
        cardT = []
        for w in card:
            if w != ''  and w != '\r\n':
                cardT.append(w)
        cards.append(' '.join(cardT))
        
vectorizer = TfidfVectorizer(min_df = 5,max_df = 0.5,ngram_range = (1,2))
X = vectorizer.fit_transform(cards).toarray()

n_components = np.arange(50,80, 5)  # options for n_components
print X.shape

fa = PCA()

fa_scores = []
for n in n_components:
    print n
    
    sys.stdout.flush()
    fa.n_components = n
    fa.fit(X)
    fa_scores.append(fa.score(X))
    print '\t',fa_scores[-1]
    

fa.n_components = n_components[np.argmax(fa_scores)]
Y = fa.fit_transform(X)

for name,ii in zip(cardnames,range(len(Y))):
    print name+'@'+ '@'.join(str(v) for v in list(Y[ii,:]))
开发者ID:adamsumm,项目名称:MtG-SetGen,代码行数:33,代码来源:tfidf.py


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