本文介绍了Scikit中的多元/多元线性回归了解吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在.csv文件中具有以下格式的数据集(dataTrain.csv和dataTest.csv):
I have a dataset (dataTrain.csv & dataTest.csv) in .csv file with this format:
Temperature(K),Pressure(ATM),CompressibilityFactor(Z)
273.1,24.675,0.806677258
313.1,24.675,0.888394713
...,...,...
并可以使用以下代码构建回归模型和预测:
And able to build a regression model and prediction with this code:
import pandas as pd
from sklearn import linear_model
dataTrain = pd.read_csv("dataTrain.csv")
dataTest = pd.read_csv("dataTest.csv")
# print df.head()
x_train = dataTrain['Temperature(K)'].reshape(-1,1)
y_train = dataTrain['CompressibilityFactor(Z)']
x_test = dataTest['Temperature(K)'].reshape(-1,1)
y_test = dataTest['CompressibilityFactor(Z)']
ols = linear_model.LinearRegression()
model = ols.fit(x_train, y_train)
print model.predict(x_test)[0:5]
但是,我想做的是多元回归.因此,模型将为CompressibilityFactor(Z) = intercept + coef*Temperature(K) + coef*Pressure(ATM)
However, what I want to do is multivariate regression. So, the model will be CompressibilityFactor(Z) = intercept + coef*Temperature(K) + coef*Pressure(ATM)
如何在scikit-learn中做到这一点?
How to do that in scikit-learn?
推荐答案
如果上面的代码适用于单变量,请尝试
If your code above works for univariate, try this
import pandas as pd
from sklearn import linear_model
dataTrain = pd.read_csv("dataTrain.csv")
dataTest = pd.read_csv("dataTest.csv")
# print df.head()
x_train = dataTrain[['Temperature(K)', 'Pressure(ATM)']].to_numpy().reshape(-1,2)
y_train = dataTrain['CompressibilityFactor(Z)']
x_test = dataTest[['Temperature(K)', 'Pressure(ATM)']].to_numpy().reshape(-1,2)
y_test = dataTest['CompressibilityFactor(Z)']
ols = linear_model.LinearRegression()
model = ols.fit(x_train, y_train)
print model.predict(x_test)[0:5]
这篇关于Scikit中的多元/多元线性回归了解吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!