pandas Sklearn逻辑回归,绘制概率曲线图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46085762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:24:34  来源:igfitidea点击:

Sklearn logistic regression, plotting probability curve graph

pythonpandasscikit-learnscatter-plot

提问by Tony

I'm trying to create a logistic regression similar to the ISLR's example, but using python instead

我正在尝试创建一个类似于 ISLR 示例的逻辑回归,但使用 python

data=pd.read_csv("data/Default.csv")

#first we'll have to convert the strings "No" and "Yes" to numeric values
data.loc[data["default"]=="No", "default"]=0
data.loc[data["default"]=="Yes", "default"]=1
X = data["balance"].values.reshape(-1,1)
Y = data["default"].values.reshape(-1,1)

LogR = LogisticRegression()
LogR.fit(X,np.ravel(Y.astype(int)))

#matplotlib scatter funcion w/ logistic regression
plt.scatter(X,Y)
plt.xlabel("Credit Balance")
plt.ylabel("Probability of Default")

But I keep getting the graph on the left, when I want the one on the right:

但是当我想要右边的图时,我一直在左边的图:

enter image description here

在此处输入图片说明

Edit: plt.scatter(x,LogR.predict(x))was my second, and also wrong guess.

编辑:plt.scatter(x,LogR.predict(x))是我的第二个,也是错误的猜测。

采纳答案by chrisckwong821

you use predict(X)which gives out the prediction of the class. replace predict(X)with predict_proba(X)[:,1]which would gives out the probability of which the data belong to class 1.

你使用predict(X)它给出了类的预测。替换predict(X)predict_proba(X)[:,1]which 将给出数据属于第 1 类的概率。

回答by Woody Pride

You can use seaborn regplotwith the following syntax

您可以使用具有以下语法的seaborn regplot

import seaborn as sns
sns.regplot(x='balance', y='default', data=data, logistic=True)