• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python cross_decomposition.PLSRegression类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: fit

	def fit(self, predictors, predictands, locations, log=False, **kwargs):

		self.locations = locations
		self.models = []
		self.n = predictors['n']

		id = 0
		for location in locations:
			X = extract_n_by_n(predictors, location, **kwargs)
			Y = predictands[:,id]

			if log:
				Y = np.log(Y)

			#pca = PCA(n_components='mle', whiten=True)
			model = PLSRegression(n_components=2)
			
			model = model.fit(X,Y)
			#components = pca.components_
			#pca.components_ = components
			
			self.models.append(model)
			print "pls: ", location, model.score(X, Y), model.x_loadings_.shape, np.argmax(model.x_loadings_, axis=0)

			id += 1
开发者ID:jackaranda,项目名称:phasespace,代码行数:25,代码来源:pls_sklearn.py


示例2: PLSCrossValidation

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,代码行数:7,代码来源:ex9.py


示例3: build_model

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,代码行数:8,代码来源:helper.py


示例4: Training

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,代码行数:58,代码来源:imp_adaboost.py


示例5: get_correlations

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,代码行数:12,代码来源:pls_da.py


示例6: do_pls

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,代码行数:13,代码来源:multivariate.py


示例7: pls_approach

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,代码行数:13,代码来源:canonics.py


示例8: __init__

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,代码行数:13,代码来源:predictors.py


示例9: __one_pls

    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,代码行数:14,代码来源:pls.py


示例10: fit_base_model

def fit_base_model(classifiers, fully, dummyY, trainx, testx):
    """ Takes a list of classifiers and/or PLS regression and
    does dimension reduction by returning the predictions of the classifiers
    or first two scores of the PLS regression on bootstrapped subsamples of
    the data."""

    trainProbs = []
    testProbs = []

    iterations = 0
    for clf in classifiers:
        for i in range(clf[1]):
            iterations += 1
            print(iterations)
            print(clf[0])
            train_rows = np.random.choice(trainx.shape[0],
                                          round(trainx.shape[0] * base_prop),
                                          True)
            oob_rows = list(set(range(trainx.shape[0])) - set(train_rows))
            print(len(train_rows))
            print(len(oob_rows))
            x = trainx[train_rows, :]
            if clf[0] == 'PLS':
                y = dummyY[train_rows, :]
                mod = PLSRegression().fit(x, y)
                trainscores = mod.transform(trainx)
                testscores = mod.transform(testx)
                trainProbs.append(trainscores[:, 0])
                trainProbs.append(trainscores[:, 1])
                testProbs.append(testscores[:, 0])
                testProbs.append(testscores[:, 1])
            else:
                y = fully[train_rows]
                print('\t Fitting model...')
                mod = clf[0].fit(x, y)
                print('\t Predicting training results...')
                tpreds = mod.predict_proba(trainx)
                trainProbs.append(list(tpreds[:, 1]))
                print('\t Predicting test results...')
                testProbs.append(list(mod.predict_proba(testx)[:, 1]))
                print('\t OOB score: ' + str(log_loss(fully[oob_rows],
                                                      tpreds[oob_rows, :])))
    return trainProbs, testProbs
开发者ID:bacovcin,项目名称:KaggleScripts,代码行数:43,代码来源:stacking.py


示例11: pls_regr

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,代码行数:20,代码来源:cal.py


示例12: train_PLSR

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,代码行数:21,代码来源:svdmi.py


示例13: lex_function_learning

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,代码行数:21,代码来源:hypernym_classification.py


示例14: reduce_PLS

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,代码行数:18,代码来源:click_utilities.py


示例15: PLSRegression

plt.xticks(())
plt.yticks(())
plt.show()

# #############################################################################
# PLS regression, with multivariate response, a.k.a. PLS2

n = 1000
q = 3
p = 10
X = np.random.normal(size=n * p).reshape((n, p))
B = np.array([[1, 2] + [0] * (p - 2)] * q).T
# each Yj = 1*X1 + 2*X2 + noize
Y = np.dot(X, B) + np.random.normal(size=n * q).reshape((n, q)) + 5

pls2 = PLSRegression(n_components=3)
pls2.fit(X, Y)
print("True B (such that: Y = XB + Err)")
print(B)
# compare pls2.coef_ with B
print("Estimated B")
print(np.round(pls2.coef_, 1))
pls2.predict(X)

# PLS regression, with univariate response, a.k.a. PLS1

n = 1000
p = 10
X = np.random.normal(size=n * p).reshape((n, p))
y = X[:, 0] + 2 * X[:, 1] + np.random.normal(size=n * 1) + 5
pls1 = PLSRegression(n_components=3)
开发者ID:Titan-C,项目名称:scikit-learn,代码行数:31,代码来源:plot_compare_cross_decomposition.py


示例16: print

X = dataset["data"]
y = dataset["target"]

# Center each feature and scale the variance to be unitary
X = preprocessing.scale(X)

# Compute the variance for each column
print(numpy.var(X, 0).sum())

# Now use PCA using 3 components
pca = PCA(3)
X2 = pca.fit_transform(X)
print(numpy.var(X2, 0).sum())

pls = PLSRegression(3)
pls.fit(X, y)
X2 = pls.transform(X)
print(numpy.var(X2, 0).sum())

# Make predictions using an SVM with PCA and PLS
pca_error = 0
pls_error = 0
n_folds = 10

svc = LinearSVC()

for train_inds, test_inds in KFold(X.shape[0], n_folds=n_folds):
    X_train, X_test = X[train_inds], X[test_inds]
    y_train, y_test = y[train_inds], y[test_inds]
开发者ID:charanpald,项目名称:tyre-hug,代码行数:29,代码来源:feexp.py


示例17: loadData


(Xtrain, ytrain) = loadData(xtrainpath, ytrainpath)
(Xtest, ytest) = loadData(xtestpath, ytestpath)

#trim off background and scale
ytrain=ytrain[:,1:]
#ytrain=scale(ytrain)
Xtrain=standardize(Xtrain)

#trim off background and scale
ytest = ytest[:,1:]
#ytest = scale(ytest)
Xtest = standardize(Xtest)

pls = PLSRegression(n_components=10)
pls.fit(Xtrain, ytrain)
y_pls = pls.predict(Xtest)
print 1 + pls.score(Xtest, ytest)


pls_rmse=[]
pls_rmse.append(sqrt(mean_squared_error(ytest[:,0], y_pls[:,0])))
pls_rmse.append(sqrt(mean_squared_error(ytest[:,1], y_pls[:,1])))
pls_rmse.append(sqrt(mean_squared_error(ytest[:,2], y_pls[:,2])))
pls_rmse.append(sqrt(mean_squared_error(ytest[:,3], y_pls[:,3])))

fig = plt.figure(figsize=(20,10))

ax1 = fig.add_subplot(241)
ax1.plot(y_pls[:,0], c='r', label='PLS Fit')
开发者ID:mwalton,项目名称:artificial-olfaction,代码行数:29,代码来源:pls.py


示例18: PLSRegression

    plt.bar(np.arange(np.shape(X_train_prepro)[1]), pca_wild_b.components_[i])
    if i == 0:
        plt.ylabel('1st component')
    elif i == 1:
        plt.ylabel('2nd component')
    else:
        plt.ylabel('3rd component')
    axis_c = plt.gca()
    axis_c.set_xticklabels(wild_boar_ddbb['header'][3:],fontsize = 7)
    axis_c.set_xticks(axis_c.get_xticks() + 0.5)
    print "dentro del bucleeeeeeeeeee"

#Select the number of components using CV
#%%
##PLSR
pls_wild_b = PLSRegression(n_components = 3)
pls_wild_b.fit(X_train_prepro,Y_train)
X_train_pls_proj = pls_wild_b.transform(X_train_prepro)
print("loadings")

for i in range(pls_wild_b.n_components):
    plt.figure()
    plt.bar(np.arange(np.shape(X_train_prepro)[1]), pls_wild_b.x_loadings_[:,i])
    if i == 0:
        plt.ylabel('PLS 1st component')
    elif i == 1:
        plt.ylabel('PLS2nd component')
    else:
        plt.ylabel('PLS 3rd component')
    axis_c = plt.gca()
    axis_c.set_xticklabels(wild_boar_ddbb['header'][3:],fontsize = 7)
开发者ID:locobiedma,项目名称:TFG-carlos-biedma-tapia,代码行数:31,代码来源:lab6.7.2.py


示例19: print

General Linear Model -- Elastic Net
'''
clf = linear_model.ElasticNet(alpha=0.2, l1_ratio=0.01)
clf.fit(x_scaled, y_scaled)
print(clf.coef_)

yvalid_scaled = clf.predict(xvalid_scaled)

err1= MAPE(y, scalery.inverse_transform(clf.predict(x_scaled)).reshape(-1,1))
err = MAPE(yvalid, scalery.inverse_transform(yvalid_scaled).reshape(-1,1))

'''
General Linear Model -- Elastic Net
'''
from sklearn.cross_decomposition import PLSRegression
pls = PLSRegression(n_components=20)
pls.fit(x_scaled, y_scaled)
print(pls.coef_)

yvalid_scaled = pls.predict(xvalid_scaled)

err1= MAPE(y, scalery.inverse_transform(pls.predict(x_scaled)).reshape(-1,1))
err = MAPE(yvalid, scalery.inverse_transform(yvalid_scaled).reshape(-1,1))

from sklearn.decomposition import PCA
reduced_data = PCA(n_components=2).fit_transform(xtrain_minmax)

pca = PCA(n_components=2)
pca.fit(xtrain_minmax)
print(pca.explained_variance_ratio_)
开发者ID:changminWu,项目名称:projet-Informatique,代码行数:30,代码来源:main.py


示例20: range

    for i in range (5):
        plt.plot(nComponents,plsCanScores[i,:],lw=3)

    plt.xlim(1,np.amax(nComponents))
    plt.title('PLS Cannonical accuracy')
    plt.xlabel('Number of components')
    plt.ylabel('accuracy')
    plt.legend (['LR','LDA','GNB','Linear SVM','rbf SVM'],loc='lower right')
    plt.grid(True)

if (0):
    #%% PLS Regression
    nComponents = np.arange(1,nClasses+1)
    plsRegScores = np.zeros((5,np.alen(nComponents)))
    for i,n in enumerate(nComponents):
        plsReg = PLSRegression(n_components=n)
        plsReg.fit(Xtrain,Ytrain)
        XtrainT = plsReg.transform(Xtrain)
        XtestT = plsReg.transform(Xtest)
        plsRegScores[:,i] = util.classify(XtrainT,XtestT,labelsTrain,labelsTest)

    
    plsReg = PLSRegression(n_components=2)
    plsReg.fit(Xtrain,Ytrain)
    xt = plsReg.transform(Xtrain)
    fig = plt.figure()
    util.plotData(fig,xt,labelsTrain,classColors)
    plt.title('First 2 components of projected data')
    

    #%% Plot accuracies for PLSSVD 
开发者ID:manuwhs,项目名称:Trapyng,代码行数:31,代码来源:baseFeatureExtractionLib.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python cross_validation.check_cv函数代码示例发布时间:2022-05-27
下一篇:
Python covariance.ShrunkCovariance类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap