2

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()
LM.fit(data[["x"]],data[["y"]])

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

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

scatter plot with linear model
enter image description here

1 Answer 1

5

A logistic regression is generally used to classify labels, even though it outputs a real between 0 and 1. This is why sklearn wants binary data in y: so that it can train the model.

In your case, you have a sigmoid function s(x)=1/(1+exp(alpha*x + beta)) and you want to find alpha and beta. I think the simplest way to do this is first of all to transform your data:

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

Now new_b is an array whose values are under the form alpha*new_a + beta, so you can train a LinearRegression model on it to find alpha:

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

Finally, you can see test whether this correesponds to what you expect:

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.