【问题标题】:Logistic regression, second column of confusion matrix shows zeros逻辑回归,混淆矩阵的第二列显示零
【发布时间】:2018-10-31 16:09:21
【问题描述】:

我想使用逻辑回归来查看银行账户余额、人的年龄和买房能力之间的相关性。 实现我的回归模型后,我得到了类型的混淆矩阵:

array([[1006,    0],
   [ 125,    0]])

当我尝试对其他数据实施线性回归时就是这种情况。代码如下:

# importing dataset
dataset = pd.read_csv('/home/stayal0ne/Machine-learning/datasets/bank.csv', sep=';')
dataset['age'] = dataset['age'].astype(float)
dataset['balance'] = dataset['balance'].astype(float)
X = dataset.iloc[:, [0, 5]].values
y = dataset.iloc[:, -1].values

# splitting the dataset into the training and test sets
X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.25, random_state=42)

# encoding categorial data
label_encoder_y = LabelEncoder()
y = label_encoder_y.fit_transform(y)

# feature scaling
scale = StandardScaler()
X_train = scale.fit_transform(X_train)
X_test = scale.transform(X_test)

# Fitting classifier into the training set
classifier = LogisticRegression(random_state=42)
classifier.fit(X_train, y_train)

# Prediction
y_predicted = classifier.predict(X_test)

# Checking the accuracy
con_matrix = confusion_matrix(y_test, y_predicted)

任何帮助将不胜感激。

【问题讨论】:

  • 帮助什么?问题是什么?
  • @desertnaut 第二列不应该是零,不是吗?
  • 第二列可以为零,具体取决于数据集以及您解决问题的准确程度。看看提供的答案(我建议你接受它)

标签: python-3.x machine-learning logistic-regression


【解决方案1】:

con_matrix 中的数组如下,tn, fp, fn, tp。

你的真正否定是 1006,这意味着模型认为无法买房的人, 并且您的误报为 0,这意味着您的模型没有预测到有人能够买房,而实际上却不能。

你的假阴性是 125,这意味着这些人实际上可以买得起房子,但你的模型说他们可以。 并且你的真阳性也是 0,这意味着你的模型没有正确预测出有能力买房的人是真正有能力买房的人。

我的总体猜测是,与可以买房的人相比,您可能有很多人买不起房子,而且特征(银行余额、年龄)可能与两者相似。

如果您的数据集不平衡,我建议您添加 class_weight 参数,如果类别标签为 0 表示无法买房,则设置 {0:0.1} 以防您有 90 条无法购买的记录买房和能买房的10条记录

【讨论】:

    【解决方案2】:

    混淆矩阵的documentation是:

    根据定义,混淆矩阵中的条目 i、j 是实际在组 i 中但预测在组 j 中的观察数。

    因此,在您的示例中,您有 1006 个类别 0 的样本被预测为类别 0,而类别 1 的 125 个样本被预测为类别 0。

    这意味着您的模型可以预测您的 0 类测试集中的每个样本。

    【讨论】:

      【解决方案3】:

      添加这一行

      y_predicted = np.round(y_predicted)
      

      在此之前

      con_matrix = confusion_matrix(y_test, y_predicted)
      

      并再次运行它

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 2019-05-26
        • 2020-07-08
        • 2021-11-30
        • 2021-09-03
        • 2018-07-09
        • 2021-03-15
        • 2018-04-20
        • 2018-11-19
        相关资源
        最近更新 更多