【问题标题】:Third parameter (kwargs) sklearn fit() function第三个参数 (kwargs) sklearn fit() 函数
【发布时间】:2020-09-18 20:26:06
【问题描述】:

我对 scikit-learn 很陌生,我对 fit() 函数有疑问。我试图在互联网上查找信息,但找不到太多信息。 在分配中,我必须创建一个传递给分类器拟合函数的参数字典,这意味着该函数将采用 3 个参数(X、y、kwargs)。这本字典应该有什么参数?显然,这些是 fit 函数的超参数。在网上我只找到了 xgbooster 的信息,但我不应该使用它,只有来自 sklearn 的分类器。

我还在网上发现 fit 可以接受一个名为 **fit_params 的字典,但函数可能采用的参数却一无所获。

希望我的问题很清楚,提前非常感谢!

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    模型超参数不是fit 函数的参数,而是您需要预先创建的模型类对象的参数。

    如果您有一个包含要传递给模型的参数的字典,则需要以这种方式执行操作(此处使用 Logistic 回归):

    from sklearn.linear_model import LogisticRegression
    params = {"C":10, "max_iter":200}
    LR = LogisticRegression(**params)
    

    现在您已经创建了指定超参数的模型,您可以继续使用您的数据进行拟合。

    LR.fit(X, y)
    

    【讨论】:

    • 谢谢,显然字典是为了传递一些分类器需要为fit函数指定的参数(主要是sample_weight,或者check_input...)
    【解决方案2】:

    我以前没有使用过 scikit-learn,但是你可以通过使用 __doc__ 方法获取你不确定的函数的文档。估计器的fit() 方法为其__doc__ 方法返回此值:

    Fit the SVM model according to the given training data.
    
            Parameters
            ----------
            X : {array-like, sparse matrix} of shape (n_samples, n_features)                 or (n_samples, n_samples)
                Training vectors, where n_samples is the number of samples
                and n_features is the number of features.
                For kernel="precomputed", the expected shape of X is
                (n_samples, n_samples).
    
            y : array-like of shape (n_samples,)
                Target values (class labels in classification, real numbers in
                regression)
    
            sample_weight : array-like of shape (n_samples,), default=None
                Per-sample weights. Rescale C per sample. Higher weights
                force the classifier to put more emphasis on these points.
    
            Returns
            -------
            self : object
    
            Notes
            -----
            If X and y are not C-ordered and contiguous arrays of np.float64 and
            X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
    
            If X is a dense array, then the other methods will not support sparse
            matrices as input.
    

    我运行它以获得该输出:

    from sklearn import svm
    clf = svm.SVC(gamma=0.001, C=100.)
    print(clf.fit.__doc__)
    

    【讨论】:

    • 感谢您的信息。我想那时没有第三个输入,我会尝试使用不同的分类器!
    猜你喜欢
    • 2012-04-09
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2017-03-12
    • 2021-09-25
    相关资源
    最近更新 更多