【问题标题】:Python: Logistic Regression gives ValueError: Unknown label type: 'continuous'Python:逻辑回归给出 ValueError:未知标签类型:“连续”
【发布时间】:2017-12-13 10:52:22
【问题描述】:

我有一个与逻辑回归有关的问题,我收到了ValueError

这是我的数据集:

             sub1         sub2              sub3       sub4
pol_1     0.000000      0.000000            0.0      0.000000   
pol_2     0.000000      0.000000            0.0      0.000000   
pol_3     0.050000      0.000000            0.0      0.000000   
pol_4     0.000000      0.000000            0.0      0.000000   
pol_5     0.000000      0.000000            0.0      0.000000   
pol_6     0.000000      0.000000            0.0      0.000000   
pol_7     0.000000      0.000000            0.0      0.000000   
pol_8     0.000000      0.000000            0.0      0.000000   
pol_9     0.000000      0.000000            0.0      0.000000   
pol_10    0.000000      0.000000            0.0      0.032423   
pol_11    0.000000      0.000000            0.0      0.000000   
pol_12    0.000000      0.000000            0.0      0.000000   
pol_13    0.000000      0.000000            0.0      0.000000   
pol_14    0.000000      0.053543            0.0      0.000000   
pol_15    0.000000      0.000000            0.0      0.000000   
pol_16    0.000000      0.000000            0.0      0.000000   
pol_17    0.000000      0.000000            0.0      0.000000   
pol_18    0.000000      0.000000            0.0      0.053453   
pol_19    0.000000      0.058344            0.0      0.000000   
pol_20    0.054677      0.000000            0.0      0.000000

这是我的代码:

array = df.values
X = array[:,0:3]
Y = array[:,3]
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = 
model_selection.train_test_split(X, Y, test_size=validation_size, 
random_state=seed)

seed = 7
scoring = 'accuracy'

kfold = model_selection.KFold(n_splits=10, random_state=seed)
cv_results = model_selection.cross_val_score(LogisticRegression(), X_train, Y_train, cv=kfold, scoring=scoring)
print(cv_results)

这给了我以下错误:

ValueError: Unknown label type: 'continuous'

如何解决这个问题?

另外,我查看了某些链接,发现问题可能与数据类型有关,在我的情况下是:

print(df.dtypes)
print(X_train.dtype)

pol_1     float64
pol_2     float64
pol_3     float64
pol_4     float64
pol_5     float64
pol_6     float64
pol_7     float64
pol_8     float64
pol_9     float64
pol_10    float64
pol_11    float64
pol_12    float64
pol_13    float64
pol_14    float64
pol_15    float64
pol_16    float64
pol_17    float64
pol_18    float64
pol_19    float64
pol_20    float64
Length: 20, dtype: object
float64

我尝试将X_trainY_train 的数据类型转换为string,但遇到了同样的错误。

谢谢!

【问题讨论】:

  • LogisticRegression 实际上是一个分类器。您是在解决分类问题还是回归问题?您要预测哪个值?预测是否与某些固定类别相关(即数据必须属于这些类别之一)或者您想预测一个数字(如股票价格、降雨、工资等)??

标签: python machine-learning scikit-learn logistic-regression valueerror


【解决方案1】:

Y 的类型应该是int。也就是说,它应该由表示类标签的整数组成。但是,在您的数据框中,Y 列由浮点数组成,因此您会收到此错误。

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 2019-02-27
    • 2018-02-15
    • 2021-09-23
    • 2017-08-29
    • 2018-12-10
    • 2021-07-17
    • 2020-05-11
    • 2018-07-22
    相关资源
    最近更新 更多