【问题标题】:Logistic Regression - Machine Learning逻辑回归 - 机器学习
【发布时间】:2018-07-03 07:18:17
【问题描述】:

使用“Machine Learning.csv”文件输入的逻辑回归。

#Import Libraries

import pandas as pd

#Import Dataset
dataset = pd.read_csv('Machine Learning Data Set.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 10]

#Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.2, random_state = 0)

#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

#Fitting Logistic Regression to the Training Set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0)
classifier.fit(X_train,y_train)

#Predicting the Test set results
y_pred = classifier.predict(X_test)

#Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test,y_pred)

我有一个机器学习/逻辑回归代码 (python) 如上所述。它已经正确训练了我的模型,并且与测试数据非常匹配。但不幸的是,当我使用其他一些随机值进行测试时,它只会给我 0/1(二进制)结果。 (训练集只有 0/1 - 如失败/成功)

如何在此算法中获得概率结果而不是二进制结果?我尝试了非常不同的一组数字,并想找出失败的概率 - 而不是 0 和 1。

非常感谢任何帮助 :) 非常感谢!

【问题讨论】:

    标签: python machine-learning regression


    【解决方案1】:

    只需替换

    y_pred = classifier.predict(X_test)
    

    y_pred = classifier.predict_proba(X_test)
    

    详情请咨询Logistic Regression Probability

    【讨论】:

    • 高度赞赏!
    【解决方案2】:

    predict_proba(X_test) 将为您提供每个类的每个样本的概率。即,如果 X_test 包含 n_samples 并且您有 2 个类,上述函数的输出将是“n_samples X 2”矩阵。并且预测的两个类的总和将为 1。有关更多详细信息,请查看文档here

    【讨论】:

    • 高度赞赏!