如何为sklearn的线性回归预测增加范围 [英] How to add a range to sklearn's linear regression predictions

查看:458
本文介绍了如何为sklearn的线性回归预测增加范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在拟合模型之前为预测添加范围.

I wonder if there is a way to add a range to the predictions prior to fitting the model.

火车数据中有问题的变量从技术上讲是百分比得分,但是当我预测测试集时,我会得到负值或> 100的值.

The variable in question in my train data is technically a percentage score, but when I predict my test set, I get negative values or values >100.

就目前而言,我正在手动对预测列表进行标准化.我也曾经剪掉负数和> 100,然后分配0和100.

For now, I am manually normalizing the predictions list. I also used to cut off negatives and >100 and assign then a 0 and 100.

但是,只有让fit函数知道此约束,才有意义,对吧?

However, it only makes sense if the fit function could be made aware of this constraint, right?

这是数据的示例行:

test_df = pd.DataFrame([[0, 40, 28, 30, 40, 22, 60, 40, 21, 0, 85, 29, 180, 85, 36, 741, 25.0]], columns=['theta_1', 'phi_1', 'value_1', 'theta_2', 'phi_2', 'value_2', 'theta_3', 'phi_3', 'value_3', 'theta_4', 'phi_4', 'value_4', 'theta_5', 'phi_5', 'value_5', 'sum_readings', 'estimated_volume'])

我一直在阅读,很多人认为这不是线性回归问题,但是他们的逻辑并不健全.另外,有人说可以应用对数刻度,但仅在与阈值进行比较的情况下才有效,即手动分类,即对逻辑回归问题使用线性回归!就我而言,我需要百分比,因为它们是必需的输出.

I have been reading and a lot of people consider this not a linear regression problem but their logic is not sound. Also, some say that one can apply a log scale but that only works in the case of comparison against a threshold, i.e., manual classification, i.e., using linear regression for a logistic regression problem! In my case, I need the percentages as they are the required output.

非常感谢您的反馈/想法.

Your feedbacks/thought are much appreciated.

推荐答案

某些算法不会提出超出范围的预测值,例如sklearn.neighbors.KNeighborsRegressor或sklearn.ensemble.RandomForestRegressor.

Some algorithms will not propose out of range predicted values such as sklearn.neighbors.KNeighborsRegressor or sklearn.ensemble.RandomForestRegressor.

线性回归器可以给出超出目标范围的值,此处为示例:

Linear Regressor can give out of target range values, here an example :

from sklearn.ensemble import RandomForestRegressor
import numpy as np
from sklearn.linear_model import LinearRegression

y = np.linspace(0,1,100)
X = 2* y
X = X.reshape(-1,1)

>>>> rf.predict(np.array([[4.]])), lr.predict(np.array([[4.]]))
# (array([0.9979798]), array([2.]))

但是您可以使用一个技巧:您可以将[0,1]空间映射到[-inf,inf]空间,并在预测后返回初始空间.

but you can use a trick : you can map your [0, 1] space to [-inf, inf] space and came back in the initial space after prediction.

以下是使用sigmoid的示例:

Here an example using sigmoid :

def sigmoid(x):
    return 1/(1+np.exp(-x))

def sigmoid_m1(x):
    return -np.log((1/x)-1)

rf = RandomForestRegressor()
lr = LinearRegression()
rf.fit(X,sigmoid_m1(y*0.9+0.05))
lr.fit(X,sigmoid_m1(y*0.9+0.05))
>>>> sigmoid(rf.predict(np.array([[4.]]))), sigmoid(lr.predict(np.array([[4.]])))
# (array([0.9457559]), array([0.99904361]))

使用这种解决方案要当心,因为您完全改变了数据的分布,并且会带来很多问题.

Take care using this kind of solution because you totally change the distribution of the data and it can create a lot of problems.

这篇关于如何为sklearn的线性回归预测增加范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆