有序逻辑回归:Intercept_返回[1]而不是[n]

时间:2019-02-28 14:08:15

标签: python python-3.x scikit-learn logistic-regression

我正在使用mordscikitlearn)库进行序数(即多项式)岭回归。

y是包含1到19的整数值的单列。

X由7个数值变量组成,这些数值变量存储在4个存储桶中,并赋值为最终的28个二进制变量。

import pandas as pd
import numpy as np    
from sklearn import metrics
from sklearn.model_selection import train_test_split
import mord

in_X, out_X, in_y, out_y = train_test_split(X, y,
                                            stratify=y,
                                            test_size=0.3,
                                            random_state=42)

mul_lr = mord.OrdinalRidge(alpha=1.0,
                           fit_intercept=True,
                           normalize=False,
                           copy_X=True,
                           max_iter=None,
                           tol=0.001,
                           solver='auto').fit(in_X, in_y)

mul_lr.coef_返回[28 x 1]数组,但是mul_lr.intercept_返回单个值(而不是19)。

知道我缺少什么吗?

1 个答案:

答案 0 :(得分:5)

如果您希望模型对所有19个类别进行预测,则需要在训练模型之前首先将标签y转换为一种热编码。

from sklearn.preprocessing import OneHotEncoder

y-=1 # range from 1 to 19 -> range from 0 to 18
enc = OneHotEncoder(n_values=19)
y = enc.fit_transform(y).toarray()
"""
train a model
"""

现在mul_lr.intercept_.shape应该是(19,)