首页 > 解决方案 > 逻辑回归拟合时显示值错误

问题描述

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=0)

log=LogisticRegression()

print (x_train.shape)                --(5, 13)
print (x_test.shape)                 --(3, 13)
print(y_train.shape)                 --(5,)
print(y_test.shape)                  --(3,)

log.fit(x_train,y_train)

请参阅以下我从 youtube 和互联网来源获取的代码,上面的代码给出了以下错误。请帮我解决错误:

ValueError                                Traceback (most recent call last)
<ipython-input-16-86c1075a1e93> in <module>
 ----> 1 log.fit(x_train,y_train)

  /srv/conda/lib/python3.6/site-packages/sklearn/linear_model/logistic.py in fit(self, X, y,     sample_weight)
1287         X, y = check_X_y(X, y, accept_sparse='csr', dtype=_dtype, order="C",
1288                          accept_large_sparse=solver != 'liblinear')
-> 1289         check_classification_targets(y)
1290         self.classes_ = np.unique(y)
1291         n_samples, n_features = X.shape

   /srv/conda/lib/python3.6/site-packages/sklearn/utils/multiclass.py in check_classification_targets(y)
  169     if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
  170                       'multilabel-indicator', 'multilabel-sequences']:
  --> 171         raise ValueError("Unknown label type: %r" % y_type)
  172 
  173 

ValueError: Unknown label type: 'continuous'

标签: pythonscikit-learn

解决方案


逻辑回归是一种用于预测二元类的统计方法。因变量或目标变量必须是二元的。在您的情况下,您有“连续”目标

逻辑回归的类型:

  • 二元逻辑回归:目标变量只有两种可能的结果。

  • 多项 Logistic 回归:目标变量具有三个或更多名义类别

  • 序数逻辑回归:目标变量具有三个或更多序数类别(示例:产品评分从 1 到 5)


推荐阅读