gpt4 book ai didi

python - Scikit Learn中的多变量/多元线性回归?

转载 作者:IT老高 更新时间:2023-10-28 21:13:06 28 4
gpt4 key购买 nike

我在 .csv 文件中有一个数据集(dataTrain.csv 和 dataTest.csv),格式如下:

Temperature(K),Pressure(ATM),CompressibilityFactor(Z)
273.1,24.675,0.806677258
313.1,24.675,0.888394713
...,...,...

并且能够使用此代码构建回归模型和预测:

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)

如何在 scikit-learn 中做到这一点?

最佳答案

如果上面的代码适用于单变量,试试这个

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]

关于python - Scikit Learn中的多变量/多元线性回归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42055615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com