如何在不拟合的情况下实例化具有已知系数的Scikit-Learn线性模型 [英] How to instantiate a Scikit-Learn linear model with known coefficients without fitting it

查看:120
本文介绍了如何在不拟合的情况下实例化具有已知系数的Scikit-Learn线性模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

作为实验的一部分,我正在测试各种保存的模型,但是其中一个模型来自我编写的算法,而不是来自sklearn模型拟合.

I am testing various saved models as part of an experiment, but one of the models comes from an algorithm I wrote, not from a sklearn model-fitting.

但是,我的自定义模型仍然是线性模型,因此我想实例化LinearModel实例并将coef_intercept_属性设置为我的自定义拟合算法的值,以便可以将其用于预测.

However, my custom model is still a linear model so I want to instantiate a LinearModel instance and set the coef_ and intercept_ attributes to the values from my custom fitting algorithm so I can use it for predictions.

到目前为止我尝试过的事情:

from sklearn.linear_model import LinearRegression

my_intercepts = np.ones(2)
my_coefficients = np.random.randn(2, 3)

new_model = LinearRegression()
new_model.intercept_ = my_intercepts
new_model.coef_ = my_coefficients

似乎可以正常预测:

X_test = np.random.randn(5, 3)

new_model.predict(X_test)

它通过了此测试:

from sklearn.utils.validation import check_is_fitted

check_is_fitted(new_model)

问题

这种方法还好吗?感觉就像是骇客,我怀疑有一种适当"的方法可以做到这一点.

Is this method fine? It feels like a hack and I suspect there is a 'proper' way to do this.

推荐答案

尽管问题中的简单方法有效,但危险是您以后可能调用对象的fit方法并覆盖系数.

Although the simple technique in the question works, the danger is that you might later call the object's fit method and over-write your coefficients.

如果仅将模型用于预测,则更合适"的方法是从sklearn的类继承并按如下方式重载fit方法:

A slightly more 'proper' way to do this, if the model is only going to be used for prediction, would be to inherit from sklearn's class and overload the fit method as follows:

class LinearPredictionModel(LinearRegression):
    """
    This model is for prediction only.  It has no fit method.
    You can initialize it with fixed values for coefficients 
    and intercepts.  

    Parameters
    ----------
    coef, intercept : arrays
        See attribute descriptions below.

    Attributes
    ----------
    coef_ : array of shape (n_features, ) or (n_targets, n_features)
        Coefficients of the linear model.  If there are multiple targets
        (y 2D), this is a 2D array of shape (n_targets, n_features), 
        whereas if there is only one target, this is a 1D array of 
        length n_features.
    intercept_ : float or array of shape of (n_targets,)
        Independent term in the linear model.
    """

    def __init__(self, coef=None, intercept=None):
        if coef is not None:
            coef = np.array(coef)
            if intercept is None:
                intercept = np.zeros(coef.shape[0])
            else:
                intercept = np.array(intercept)
            assert coef.shape[0] == intercept.shape[0]
        else:
            if intercept is not None:
                raise ValueError("Provide coef only or both coef and intercept")
        self.intercept_ = intercept
        self.coef_ = coef

    def fit(self, X, y):
        """This model does not have a fit method."""
        raise NotImplementedError("model is only for prediction")

然后,按如下所示实例化模型:

Then, instantiate the model as follows:

new_model = LinearPredictionModel(coef=my_coefficients, intercept=my_intercepts)

我认为实现此目的的唯一正确"方法是让我使用fit方法中的自定义算法完全实现一个新类.但是出于在scikit学习环境中测试系数的简单需求,此方法似乎可以正常工作.

I think the only 'proper' way to do this would be for me to fully implement a new class with my custom algorithm in the fit method. But for the simple needs of testing the coefficients in a scikit-learn environment, this method seems to work fine.

这篇关于如何在不拟合的情况下实例化具有已知系数的Scikit-Learn线性模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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