16

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.

Gambit1614
  • 8,547
  • 1
  • 25
  • 51
Tony
  • 1,318
  • 1
  • 14
  • 36

3 Answers3

14

You can use seaborn regplot with the following syntax

import seaborn as sns
sns.regplot(x='balance', y='default', data=data, logistic=True)
Woody Pride
  • 13,539
  • 9
  • 48
  • 62
10

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.

chrisckwong821
  • 1,133
  • 12
  • 24
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')
  • 5
    Please add some descriptions of your code to give context to your answer – William Baker Morrison Jan 11 '21 at 14:41
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/28052645) – double-beep Jan 11 '21 at 21:19