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

时间:2017-09-06 23:38:36

标签: python pandas scikit-learn scatter-plot

我正在尝试创建类似于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")

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

enter image description here

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

3 个答案:

答案 0 :(得分:4)

您可以使用seaborn regplot语法

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

答案 1 :(得分:3)

你使用predict(X)来预测班级。 将predict(X)替换为predict_proba(X)[:,1],这将提供数据属于第1类的概率。

答案 2 :(得分:0)

x_range = 80
Xs = [i for i in range(x_range)]
Ys = [model.predict_proba([[value]])[0][1] for value in range(x_range)]

plt.scatter(df['X'], df['y'])
plt.plot(Xs, Ys, color='red')