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


Python PLSRegression.fit方法代码示例

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


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

示例1: PLSCrossValidation

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def PLSCrossValidation(n_components, trainSet, validationSet):
  pls = PLSRegression(n_components=n_components)
  pls.fit(trainSet[predictorList], trainSet['Apps'])
  predictPls = pls.predict(validationSet[predictorList])
  different = predictPls.flat - validationSet['Apps']
  error_rate = np.mean(different ** 2)
  return error_rate
开发者ID:zechfox,项目名称:isl_exercises,代码行数:9,代码来源:ex9.py

示例2: Training

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def Training(df,seed, yratio, xratio, index = 1):
	snp_matrix = np.array(df.values)
	xdim, ydim = snp_matrix.shape

	ydimlist = range(0,ydim)
	xdimlist = range(0,xdim)

	random.seed(seed)
	random.shuffle(ydimlist) # shuffle the individuals
	random.shuffle(xdimlist) # shuffle the SNPs	
	accuracy = 0

	snp_matrix_shuffle = np.copy(snp_matrix[:,ydimlist])
	snp_matrix_shuffle = np.copy(snp_matrix[xdimlist,:])
	snp_matrix_train = snp_matrix_shuffle[:,0:int(ydim*yratio)]
	snp_matrix_test = snp_matrix_shuffle[:,int(ydim*yratio):]

	snp_matrix_train_x = snp_matrix_train[0:int(xdim*xratio),:]
	snp_matrix_test_x = snp_matrix_test[0:int(xdim*xratio),:]

	for i in range(int(xdim*xratio), xdim):
		snp_matrix_train_y = snp_matrix_train[i,:]
		snp_matrix_test_y = snp_matrix_test[i,:]
		if index != 7:
			if index == 1:
				clf = AdaBoostClassifier(n_estimators= 100)
			elif index == 2:
				clf = RandomForestClassifier(n_estimators=100)
			elif index == 3:
				clf = linear_model.LogisticRegression(C=1e5)
			elif index == 4:
				clf = svm.SVC(kernel = 'rbf')
			elif index == 5:
				clf = svm.SVC(kernel = 'poly')
			else:
				clf = svm.SVC(kernel = 'linear')
			clf = clf.fit(snp_matrix_train_x.T, snp_matrix_train_y)
			Y_pred = clf.predict(snp_matrix_test_x.T)
			prediction = snp_matrix_test_y - Y_pred
			wrong = np.count_nonzero(prediction)
			tmp = 1 - (wrong + 0.0) / len(prediction)
			print tmp
			accuracy += tmp

	accuracy = accuracy / (xdim - int(xdim*xratio))

	if index == 7:
		pls2 = PLSRegression(n_components = 50, scale=False, max_iter=1000)
		snp_matrix_train_y = snp_matrix_train[int(xdim*xratio):,:]
		pls2.fit(snp_matrix_train_x.T,snp_matrix_train_y.T)
		snp_matrix_test_x = snp_matrix_test[0:int(xdim*xratio),:]
		snp_matrix_test_y = snp_matrix_test[int(xdim*xratio):,:]		
		Y_pred = transform(pls2.predict(snp_matrix_test_x.T))
		prediction = snp_matrix_test_y - Y_pred.T
		xdim, ydim = prediction.shape
		wrong = np.count_nonzero(prediction)
		accuracy = 1 - wrong / (xdim * ydim + 0.0)
	return accuracy
开发者ID:zcpan,项目名称:Imputation,代码行数:60,代码来源:imp_adaboost.py

示例3: fit

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def fit(predictors, predictands, log=False, **kwargs):
	
	model = PLSRegression(n_components=2)
	try:
		model.fit(predictors, predictands)
	except:
		return None

	return model
开发者ID:jackaranda,项目名称:phasespace,代码行数:11,代码来源:pls_sklearn.py

示例4: trainmodels

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def trainmodels(m, x, y, iter=1000):
    '''For the model type m, train a model on x->y using built-in CV to
    parameterize.  Return both this model and an unfit model that can be used for CV.
    Note for PLS we cheat a little bit since there isn't a built-in CV trainer.
    '''
    
    if m == 'pls':
        #have to manually cross-validate to choose number of components
        kf = KFold(len(y), n_folds=3)
        bestscore = -10000
        besti = 0
        for i in xrange(1,min(100,len(x[0]))):
            #try larger number of components until average CV perf decreases
            pls = PLSRegression(i)
            scores = []
            #TODO: parallelize below
            for train,test in kf:
                xtrain = x[train]
                ytrain = y[train]
                xtest = x[test]
                ytest = y[test]            
                pls.fit(xtrain,ytrain)
                score = scoremodel(pls,xtest,ytest)
                scores.append(score)
                
            ave = np.mean(scores)
            if ave < bestscore*0.95: #getting significantly worse
                break
            elif ave > bestscore:
                bestscore = ave
                besti = i
        
        model = PLSRegression(besti) 
        model.fit(x,y)
        unfit = PLSRegression(besti)  #choose number of components using full data - iffy
        print "PLS components =",besti

    elif m == 'lasso':
        model = LassoCV(n_jobs=-1,max_iter=iter)
        model.fit(x,y)
        unfit = LassoCV(n_jobs=-1,max_iter=iter) #(alpha=model.alpha_)
        print "LASSO alpha =",model.alpha_
        return (model,unfit)
    elif m == 'ridge':
        model = RidgeCV()
        model.fit(x,y)
        print "Ridge alpha =",model.alpha_
        unfit = RidgeCV()
    else:
        model = ElasticNetCV(n_jobs=-1,l1_ratio=[.1, .5, .7, .9, .95, .99, 1],max_iter=iter)
        model.fit(x,y)
        print "Elastic alpha =",model.alpha_," l1_ratio =",model.l1_ratio_
        unfit = ElasticNetCV(n_jobs=-1,max_iter=iter)

    return (model,unfit)
开发者ID:dkoes,项目名称:qsar-tools,代码行数:57,代码来源:trainlinearmodel.py

示例5: get_correlations

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def get_correlations(param, spec, wave):
    '''Returns correlations between spec and params by wavelengths'''
    # using PLS
    pls = PLSRegression(10)
    pls.fit(spec, param)
    
    #get corretalions
    nparam = param.shape[1]
    cor = pls.coefs*np.asarray([pls.x_std_]*nparam).T
    cor /= np.tile(pls.y_std_, (cor.shape[0],1))

    return cor
开发者ID:drdangersimon,项目名称:experements,代码行数:14,代码来源:pls_da.py

示例6: do_pls

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def do_pls(X, Y):
    pls2 = PLSRegression(n_components=2)
    pls2.fit(X,Y)
    out = pls2.transform(X)
    print(out)
    print(out.shape)

    plt.title("PLS2")
    plt.xlabel("PL1")
    plt.ylabel("PL2")
    plt.grid();
    plt.scatter(out[:, 0], out[:, 1], c=Y, cmap='viridis')
    plt.savefig('pls.png', dpi=125)
开发者ID:flikka,项目名称:ML-examples,代码行数:15,代码来源:multivariate.py

示例7: pls_approach

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def pls_approach():
    from sklearn.cross_decomposition import PLSRegression

    (X, Y), cities = pull_xy_data()

    pls = PLSRegression()
    pls.fit(X, Y)

    plsX, plsY = pls.transform(X, Y)

    plot(plsX, cities, ["Lat01", "Lat02", "Lat03"], ellipse_sigma=1)

    return "OK What Now?"
开发者ID:csxeba,项目名称:NitaGeo,代码行数:15,代码来源:canonics.py

示例8: __init__

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
class PLSPredictor:
    def __init__(self):
        self.pls2 = PLSRegression(n_components=2,
                                  scale=True,
                                  max_iter=500,
                                  tol=1e-06,
                                  copy=True)

    def predict(self, values):
        self.pls2.predict(values)

    def train(self, measured_values, screen_points):
        self.pls2.fit(measured_values, screen_points)
开发者ID:BrainTech,项目名称:pisak,代码行数:15,代码来源:predictors.py

示例9: __one_pls

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
    def __one_pls(self, cat):

        np.seterr(all='raise')

        lcat = np.zeros(self.train_set['labels'].size)

        lcat[self.train_set['labels'] != cat] = -1
        lcat[self.train_set['labels'] == cat] = +1

        pls = PLSRegression(n_components=2, scale=False)

        pls.fit(self.train_set['data'], lcat)

        return pls
开发者ID:allansp84,项目名称:spectralcubes,代码行数:16,代码来源:pls.py

示例10: build_model

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def build_model(X, y):
	# gbr = GradientBoostingRegressor(learning_rate= 0.03, n_estimators=2000, max_depth=8, subsample=0.9)
	# rf = RandomForestRegressor(n_estimators=200)
	# lr = LinearRegression(fit_intercept=True)
	# knr = KNeighborsRegressor(n_neighbors=10, weights='uniform')
	# svr = SVR(C=5.0, kernel='linear')
	pls = PLSRegression(n_components=35)
	return pls.fit(X, y)
开发者ID:numb3r33,项目名称:online-hackathon,代码行数:10,代码来源:helper.py

示例11: reduce_PLS

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def reduce_PLS(dataframe):
    PLS_file="data/pls_structure.pickle"
    selectedcolumn=[x for x in dataframe.columns if x not in ["id","click","device_id","device_ip"]]
    X=np.array(dataframe[selectedcolumn])
    y=np.array(dataframe["click"])
    if os.path.exists(PLS_file):
        stand_PLS=pickle.load(open(PLS_file,'rb'))
        print "PLS structure is loaded."
    else:
        stand_PLS=PLSRegression(n_components=10,scale=True)
        stand_PLS.fit(X, y[:,np.newaxis])
        stand_PLS.y_scores_=None
        stand_PLS.x_scores_=None
        pickle.dump(stand_PLS,open(PLS_file,"wb"))
        print "PLS transform structure is stored."
    T=stand_PLS.transform(X)
    print "PLS transformation is performed."
    return T
开发者ID:snaillians,项目名称:kaggle-click,代码行数:20,代码来源:click_utilities.py

示例12: pls_regr

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def pls_regr(x, y):
    from sklearn.cross_decomposition import PLSRegression
    n = len(x[0])
    if n < 2:
        raise TypeError
    score = -999999999999
    pls = None
    '''
    for i in range(3, n):
        pls2 = PLSRegression(n_components=i)
        pls2.fit(x,y)
        cscore = pls2.score(x, y)
        #print i, cscore 
        if cscore > score:
            pls = pls2
            score = cscore
    '''
    pls = PLSRegression(n_components=5)
    pls.fit(x,y)
    return pls
开发者ID:wgx998877,项目名称:pyglass,代码行数:22,代码来源:cal.py

示例13: lex_function_learning

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def lex_function_learning( class_name,  hyper_vec ) :

		#pls2 = KernelRidge( kernel = "rbf", gamma= 100)
		#pls2 = KernelRidge( )
		pls2 = PLSRegression(n_components=50, max_iter=5000)

		X = extract_postive_features ( train_dataset[class_name][0], train_dataset[class_name][1] )			

		Y = []

		for hypo_vec in X :

			sub = hyper_vec-hypo_vec
			Y.append(sub) # Target = difference vector ( Hypernym_vector - Hyponym_vector )
			#Y.append(hyper_vec) # Target = Hypernym vector 

		pls2.fit( X, Y)	
		train_acc = pls2.score(X, Y)
		print "class = ", class_name, "train len = ", len(X)
		
		return pls2, train_acc, len(X)
开发者ID:anupama-gupta,项目名称:Hypernymy,代码行数:23,代码来源:hypernym_classification.py

示例14: train_PLSR

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def train_PLSR(x_filename, y_filename, model_filename, n):
    """
    Train a PLSR model and save it to the model_filename.
    X and Y matrices are read from x_filename and y_filename.
    The no. of PLSR components is given by n. 
    """
    X = loadMatrix(x_filename)[0].todense()
    Y = loadMatrix(y_filename)[0].todense()
    if X.shape[0] != Y.shape[0]:
        sys.stderr.write("X and Y must have equal number of rows!\n")
        raise ValueError
    sys.stderr.write("Learning PLSR...")
    startTime = time.time()
    pls2 = PLSRegression(copy=True, max_iter=10000, n_components=n, scale=True, tol=1e-06)
    pls2.fit(X, Y)  
    model = open(model_filename, 'w') 
    pickle.dump(pls2, model, 1)
    model.close()
    endTime = time.time()
    sys.stderr.write(" took %ss\n" % str(round(endTime-startTime, 2)))  
    pass
开发者ID:TPNguyen,项目名称:svdmi,代码行数:23,代码来源:svdmi.py

示例15: hacerPLS

# 需要导入模块: from sklearn.cross_decomposition import PLSRegression [as 别名]
# 或者: from sklearn.cross_decomposition.PLSRegression import fit [as 别名]
def hacerPLS(X,Y):
    pls_wild_b = PLSRegression(n_components = 9) 
    pls_wild_b.fit(X,Y)
    Z = pls_wild_b.transform(X)
    scores = list() 
    scores_std = list()
    n_features = np.shape(X)[1]
    
    X,X_test_tot, Y, Y_test_tot = cross_validation.train_test_split(X,Y,test_size = 0.5,random_state = 0)
    N = np.shape(X)[0]
    
    for num_comp in range(n_features):
        kf = KFold(N,n_folds = 10)
        aux_scores = list()
        for train, test in kf:
            X_train, X_test, y_train, y_test = X[train], X[test], Y[train], Y[test]
              
            if num_comp == 0:
                y_pred = np.mean(y_test)
                y_pred = y_pred* np.ones(np.shape(y_test))
                aux_scores.append(metrics.mean_squared_error(y_test,y_pred))
            
            else:
                pls_foo = PLSRegression(n_components = num_comp)                        
                pls_foo.fit(X_train,y_train)
                y_pred = pls_foo.predict(X_test)
            
                #obtaing the score
                this_score = metrics.mean_squared_error(y_test,y_pred)
                aux_scores.append(this_score)
                
        scores.append(np.mean(aux_scores))
        scores_std.append(np.std(aux_scores))
    
    plt.plot(scores)
    xlabel('Componentes')
    ylabel("$MSE$")
    title("Animales PLS")
    plt.show()
    
    num_comp = np.argmin(scores)
    
    pls_pred = PLSRegression(n_components =2)
    pls_pred.fit(X,Y)
    y_pred_test = pls_pred.predict(X_test_tot)
    
    print "MSE test = " + str(metrics.mean_squared_error(Y_test_tot,y_pred_test))
开发者ID:locobiedma,项目名称:TFG-carlos-biedma-tapia,代码行数:49,代码来源:patogenos.py


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