【问题标题】:sklearn.linear_model.RandomizedLogisticRegression : Handle Categorical Valuesklearn.linear_model.RandomizedLogisticRegression :处理分类值
【发布时间】:2015-06-05 14:00:58
【问题描述】:

我想使用RandomizedLogisticRegression 为我的数据集选择变量。但问题是,我的数据集中的特征之一是性别。所以它的值是'F'或'M'而不是数值。结果我得到了以下错误:

Traceback (most recent call last):
  File "main.py", line 84, in Customer_Acquisition_Binary_Logistics
    self.randomized_Logistic_regression()
  File "main.py", line 92, in randomized_Logistic_regression randomized_logistic.fit(X,y)
  File "C:\Python27\lib\site-packages\sklearn\linear_model\randomized_l1.py", line 91, in fit
    X = as_float_array(X, copy=False)
  File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 112, in as_float_array
    return X.astype(np.float32 if X.dtype == np.int32 else np.float64) ValueError: could not convert string to float: F

如何处理非数字的分类值?谢谢。

【问题讨论】:

  • 您必须将它们编码为数值,sklearn 理解数值,而不是 str 值

标签: python scikit-learn regression


【解决方案1】:

您必须将 str 值编码为数值,为此您可以使用LabelEncoder

In [33]:

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
print(le.fit(["paris", "paris", "tokyo", "amsterdam"]))
​
print(list(le.classes_))
​
print(le.transform(["tokyo", "tokyo", "paris"]) )
​
print(list(le.inverse_transform([2, 2, 1])))
​
LabelEncoder()
['amsterdam', 'paris', 'tokyo']
[2 2 1]
['tokyo', 'tokyo', 'paris']

【讨论】:

    猜你喜欢
    • 2020-09-04
    • 2021-03-11
    • 2019-02-27
    • 2019-03-28
    • 2016-01-05
    • 2021-12-19
    • 2020-05-22
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多