Logistic Regression with continuous data using sklearn in python

0 votes

How can I describe these points with a regression? In the example the LinearRegression doesn't fit the logistic distribution of the points. The LogisticRegression() from sklearn just accept binary data. My y-values are continuous from 0 to 1. Do I have to transform the data or how do I get a appropriate model?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression

a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14])
b = [0,0,0.01,0.08,0.16,00.28,0.5,0.66,0.8,0.9,0.95,0.99,1,1]
data = pd.DataFrame({'x': a, 'y':b})

LM = LinearRegression()

plt.plot([1,14], LM.predict([[1],[14]]), color = "red")

LogM = LogisticRegression()
LogM.fit(data[["x"]],data[["y"]])  # doesn't work

scatter plot with linear model
enter image description here

Mar 22, 2022 in Machine Learning by Nandini
• 5,480 points

1 answer to this question.

0 votes

Despite the fact that it produces a real between 0 and 1, a logistic regression is commonly used to classify labels. This is why sklearn requires binary data in y: so that the model can be trained
You have a sigmoid function s(x)=1/(1+exp(alpha*x + beta)) and you want to find alpha and beta in your scenario. The simplest method to achieve this, in my opinion, is to first change your data:

ne_a = a[2:-2]
ne_b = np.array(b[2:-2]) # Getting rid of 0 and 1 values
ne_b = np.log((1 / ne_b) - 1)

Because ne_b is now an array with values in the form alpha*ne_a + beta, you can use it to train a LinearRegression model to find alpha:

model = LinearRegression()
model.fit(ne_a.reshape(-1, 1), ne_b.reshape(-1, 1))
alpha = model.coef_[0, 0]
beta = l.predict([[0]])[0, 0]

Finally, you may see if this matches to your expectations:

predicted = 1 / (1 + np.exp(alpha * a + beta))
plt.plot(a, b)
plt.plot(a, predicted)

enter image description here

Elevate Your Expertise with Our Machine Learning Certification Program!

answered Mar 23, 2022 by Dev
• 6,000 points

Related Questions In Machine Learning

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Can we change the sigmoid with tanh in Logistic regression transforms??

Hi@Deepanshu, Yes, you can use tanh instead of ...READ MORE

answered May 12, 2020 in Machine Learning by MD
• 95,440 points
0 votes
1 answer
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
0 votes
1 answer

Plotting logistic regression in R with the Smarket dataset

The first, third, and fourth methods of ...READ MORE

answered Apr 12, 2022 in Machine Learning by Dev
• 6,000 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP