设置线性回归预测的范围

2024-03-29 07:02:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题类似于question。但我没有得到答案。我需要进一步澄清。你知道吗

我第一次使用sklearn线性回归预测向数据集中添加更多的数据点。添加更多的数据点将帮助我更准确地识别异常值。我已经建立了我的模型,并得到了预测,但我希望模型返回具有一定范围的预测点。有可能做到这一点吗?你知道吗

我想在名为'delivery_fee'的列中预测值。 列中的值从3开始稳步增加,直到达到27。 列中紧跟在27后面的最后一个值是47。你知道吗

我希望模型能预测27到47之间的数值。你知道吗

我的代码:

import sklearn
from sklearn.model_selection import train_test_split 
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing

#create a copy of the dataframe
delivery_linreg = outlierFileNew.copy()

le = preprocessing.LabelEncoder()
delivery_linreg['branch_code'] = le.fit_transform(delivery_linreg['branch_code'])

#select all columns in the datframe except for delivery_fee
x = delivery_linreg[[x for x in delivery_linreg.columns if x != 'delivery_fee']]
#selecting delivery_fee as the column to be predicted
y = delivery_linreg.delivery_fee
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

#fitting simple linear regression to training set
linreg = LinearRegression()
linreg.fit(x_train,y_train)
delivery_predict = linreg.predict(x_test)

我的模型返回从4到17的值。这不是我想要的范围。关于如何改变预测范围有什么建议吗?你知道吗

谢谢你


Tags: the数据from模型testimportmodeltrain