【问题标题】:How to make predictions on a logistic regression model with a separate df for train and test data如何使用单独的 df 对训练和测试数据的逻辑回归模型进行预测
【发布时间】:2021-06-22 13:52:08
【问题描述】:

我正在研究逻辑回归模型。我从两个单独的 CSV 文件开始,一个用于训练数据,一个用于测试数据。我创建了两个单独的数据框,每个数据集一个。我能够很好地拟合和训练模型,但是当我尝试使用测试数据进行预测时出现错误。

我不确定我是否正确设置了我的 y_train 变量,或者是否还有其他问题。我在运行预测时收到以下错误消息。

这是模型的设置和代码"

#Setting x and y values
X_train = clean_df_train[['account_length','total_day_charge','total_eve_charge', 'total_night_charge', 
            'number_customer_service_calls']]
y_train = clean_df_train['churn']

X_test = clean_df_test[['account_length','total_day_charge','total_eve_charge', 'total_night_charge', 
            'number_customer_service_calls']]
y_test = clean_df_test['churn']
#Fitting / Training the Logistic Regression Model
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='warn',
          n_jobs=None, penalty='l2', random_state=None, solver='warn',
          tol=0.0001, verbose=0, warm_start=False)
#Make Predictions with Logit Model
predictions = logreg.predict(X_test)

#Measure Performance of the model
from sklearn.metrics import classification_report

#Measure performance of the model
classification_report(y_test, predictions)
  1522     """
   1523 
-> 1524     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
   1525 
   1526     labels_given = True

E:\Users\davidwool\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
     79     if len(y_type) > 1:
     80         raise ValueError("Classification metrics can't handle a mix of {0} "
---> 81                          "and {1} targets".format(type_true, type_pred))
     82 
     83     # We can't have more than one value on y_type => The set is no more needed

ValueError: Classification metrics can't handle a mix of continuous and binary targets

这是我正在处理的数据的头部。流失列是完全空白的,因为这是我想要预测的。

clean_df_test.head()

    account_length  total_day_charge    total_eve_charge    total_night_charge  number_customer_service_calls   churn
0               74             31.91               13.89                 8.82                               0     NaN
1               57             30.06               16.58                 9.61                               0     NaN
2              111             36.43               17.72                 8.21                               1     NaN
3               77             42.81               17.48                12.38                               2     NaN
4               36             47.84               17.19                 8.42                               2     NaN

这里也是数据类型。

clean_df_test.dtypes
account_length                     int64
total_day_charge                 float64
total_eve_charge                 float64
total_night_charge               float64
number_customer_service_calls      int64
churn                            float64
dtype: object

主要问题是我习惯于在一个数据集上使用 sklearn 的 train_test_split() 函数,在这里我有 2 个单独的数据集,所以我不确定将我的 y-test 设置为什么。

【问题讨论】:

  • 该错误描述了 y_test 或预测中的类型不一致。尝试打印 y_test 和预测的类型以及它们的形状。
  • 请更新您的问题以包含您的y_testpredictions 的示例。
  • 谢谢大家,我已经用一些额外的信息更新了帖子。
  • 如果您正在生成分类报告,您的 y_test 应该是您的测试集的标签。看起来它们都是您数据中的 NaN。
  • 所以标签要么是 1(表示是)要么是 0,我该如何显示呢?抱歉,我知道这可能是一个基本问题,但我之前没有使用 sklearn train、test、split 来完成 logit。

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


【解决方案1】:

通过查看clean_df_test.head(),问题变得显而易见。我可以看到churn 列中有空值。

因此,y_test 包含空值,并且通过将其作为 y_true 传递给 classification_report(),您正在使函数将空值与整数进行比较,这会引发错误。

要解决此问题,请尝试删除 churnNaN 的行,然后像以前一样运行其余代码。

# Drop records where `churn` is NaN
clean_df_test.dropna(axis=0, subset=['churn'], inplace=True)

# Carry on as before
X_test = clean_df_test[['account_length','total_day_charge','total_eve_charge', 'total_night_charge', 
            'number_customer_service_calls']]
y_test = clean_df_test['churn']

发现此问题的另一种方法是查看clean_df_test 的数据类型。从输出来看,churn 的类型是 float,如果它只填充了 1 和 0,则不应该是这种情况!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 2015-04-10
    • 2019-10-22
    • 2018-05-01
    • 2012-12-25
    • 1970-01-01
    • 2022-09-18
    相关资源
    最近更新 更多