Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sklearn logistic regression, plotting probability curve graph

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

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.

like image 217
Tony Avatar asked Sep 06 '17 23:09

Tony


People also ask

How do you graph a logistic regression curve?

To plot the logistic regression curve in base R, we first fit the variables in a logistic regression model by using the glm() function. The glm() function is used to fit generalized linear models, specified by giving a symbolic description of the linear predictor.

How do you visualize logistic regression?

To visualize the logistic regression fit, we first use the predict function to generate the model predictions about probability of survival as a function of age. Having generated the predicted probabilities of survival we can then add these prediction lines to our previous plot using geom_line .


2 Answers

You can use seaborn regplot with the following syntax

import seaborn as sns
sns.regplot(x='balance', y='default', data=data, logistic=True)
like image 148
Woody Pride Avatar answered Sep 29 '22 03:09

Woody Pride


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.

like image 20
chrisckwong821 Avatar answered Sep 29 '22 04:09

chrisckwong821