Python中的 sklearn.cross_decomposition.PLSRegression()函数
PLS 回归是一种回归方法,它考虑了两个数据集中的潜在结构。由于单标签和多标签学习原因,偏最小二乘回归在基于 MRI 的评估中表现良好。 PLSRegression 从 PLS 获取,模式=”A” 和 deflation_mode=”regression”。此外,在一维响应的情况下,已知 PLS2 或 PLS。
Syntax: class sklearn.cross_decomposition.PLSRegression(n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)
Parameters:
This function accepts five parameters which are mentioned above and defined below:
- n_components:<int>: Its default value is 2, and it accepts the number of components that are needed to keep.
- scale:<bool>: Its default value is True, and it accepts whether to scale the data or not.
- max_iteran :<int>: Its default value is 500, and it accepts the maximum number of iteration of the NIPALS inner loop.
- tol: <non-negative real>: Its default value is 1e-06, and it accepts tolerance used in the iterative algorithm.
- copy:<bool>: Its default value is True, and it shows that deflection should be done on a copy. Don’t care about side effects when the default value is set True.
Return Value: PLSRegression is an approach for predicting response.
下面的示例说明了 PLSRegression() 模型的使用。
例子:
Python3
import numpy as np
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.cross_decomposition import PLSRegression
from sklearn.model_selection import train_test_split
# load boston data using sklearn datasets
boston = datasets.load_boston()
# separate data and target values
x = boston.data
y = boston.target
# tabular data structure with labeled axes
# (rows and columns) using DataFrame
df_x = pd.DataFrame(x, columns=boston.feature_names)
df_y = pd.DataFrame(y)
# create PLSRegression model
pls2 = PLSRegression(n_components=2)
# split data
x_train, x_test, y_train, y_test = train_test_split(
df_x, df_y, test_size=0.30, random_state=1)
# fit the model
pls2.fit(x_train, y_train)
# predict the values
Y_pred = pls2.predict(x_test)
# plot the predicted Values
plt.plot(Y_pred)
plt.xticks(rotation=90)
plt.show()
# print the predicted value
print(Y_pred)
输出:
使用 PLSRegression 绘制预测值
使用训练好的模型打印预测值