首页 > 解决方案 > 逻辑回归中的概率校准错误:ValueError:无法将字符串转换为浮点数:'OLIFE'

问题描述

我已经建立了逻辑回归模型并使用 scikit 管道进行了预处理。我进行了训练和测试,一切都很好,但是当我尝试根据有效数据校准模型时,出现错误calib_clf.fit(Valid, labelValid)

ValueError: could not convert string to float: 'OLIFE'

这是我的代码:

column_trans = make_column_transformer(
                                        (OneHotEncoder(), ['PRODUCT_LINE_ID','SMOKING_STATUS','gender','Cover_Type']),
                                        remainder = StandardScaler()
                                       )

column_trans.fit_transform(train)

# Create a pipeline that scales the data then trains a support vector classifier
logreg = LogisticRegression()
model_pipeline = make_pipeline(column_trans, logreg)

# Fitting the model pipeline
model_pipeline.fit(train,labelTrain)

# Testing the model pipeline on new data/test data
predictions = model_pipeline.predict_proba(test)[:,1]


calib_clf = CalibratedClassifierCV(model_pipeline, method="sigmoid", cv="prefit")
calib_clf.fit(Valid, labelValid)

标签: pythonpython-3.xmachine-learningscikit-learnlogistic-regression

解决方案


https://github.com/dnishimoto/python-deep-learning/blob/master/Happiness%20and%20Depression%20Logistic%20Regression.ipynb

我使用幸福与抑郁作为数据集。对于交叉折叠的数量,我使用 3 而不是 prefit。

  calibrated_clf = CalibratedClassifierCV(base_estimator=pipeline['clf'], method="sigmoid", cv=3)
  calibrated_clf.fit(X, y)

  print(calibrated_clf.predict_proba(X)[:5, :])

输出:(发生和不发生的概率。

   [[0.97151521 0.02848479]
   [0.9953179  0.0046821 ]
   [0.01829911 0.98170089]
   [0.99405208 0.00594792]
   [0.82948843 0.17051157]]

概率输出表明数据行为在所有情况下都不一致。它可能是我结合创建二项式的中度抑郁类别。需要对中度抑郁症进行分层,并需要发现一个新的目标变量。

​</p>


推荐阅读