gpt4 book ai didi

python - 如何使用二次回归?

转载 作者:行者123 更新时间:2023-11-30 09:40:43 28 4
gpt4 key购买 nike

我正在尝试学习如何拟合二次回归模型。数据集可以从以下地址下载: https://filebin.net/ztr9har5nio7x78v

设“AdjSalePrice”为目标变量,“SqFtTotLiving”、“SqFtLot”、“Bathrooms”、“Bedrooms”、“BldgGrade”为预测变量。

假设“SqFtTotLiving”是度数为 2 的变量。Python 代码为:

import pandas as pd
import numpy as np
import statsmodels.api as sm
import sklearn


houses = pd.read_csv("house_sales.csv", sep = '\t')#separador é tab

colunas = ["AdjSalePrice","SqFtTotLiving","SqFtLot","Bathrooms","Bedrooms","BldgGrade"]

houses1 = houses[colunas]


X = houses1.iloc[:,1:] ##
y = houses1.iloc[:,0] ##

如何使用 sklearn 和 statsmodels 拟合二次回归模型?我只能使用线性回归...

最佳答案

首先,如果您有 X 的一维数组,请执行以下操作:

x = np.array([1,2,3,4,5])
x = x.reshape((-1,1))

结果:

>>>x
array([[1],
[2],
[3],
[4],
[5]])

然后这个:

from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model

poly = PolynomialFeatures(degree=2)
poly_variables = poly.fit_transform(x)

poly_var_train, poly_var_test, res_train, res_test = train_test_split(poly_variables, y, test_size = 0.3, random_state = 4)

regression = linear_model.LinearRegression()

model = regression.fit(poly_var_train, res_train)
testing_score = model.score(poly_var_test, res_test)

关于python - 如何使用二次回归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58884108/

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