Logistic回归-机器学习

时间:2018-07-03 07:18:17

标签: python machine-learning regression

输入“ 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。

非常感谢您的帮助:)非常感谢!

2 个答案:

答案 0 :(得分:2)

只需替换

y_pred = classifier.predict(X_test)

使用

y_pred = classifier.predict_proba(X_test)

有关详细信息,请参见Logistic Regression Probability

答案 1 :(得分:1)

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