#https://www.w3schools.com/python/python_ml_logistic_regression.asp

import numpy
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

def logit2prob(logr, x):
    log_odds = logr.coef_ * x + logr.intercept_ # (logr: logistic_regression_model, x:input)
    odds = np.exp(log_odds) #calculation of ratio of an event happening to an event not happening. Odds->[0,+oo)
    probability = odds / (1 + odds) # probability -> [0,1]
    return probability

# Reshaped for Logistic function.
X = np.array([3.78, 2.44, 2.09, 0.14, 1.72, 1.65, 4.92, 4.37, 4.96, 4.52, 3.69, 5.88]).reshape(-1,1)
y = np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

logr = linear_model.LogisticRegression()
logr.fit(X,y)

predicted = logr.predict(numpy.array([1.7]).reshape(-1,1)) #tumor size=1.7
print("prediction: ", predicted)
log_odds = logr.coef_
odds = numpy.exp(log_odds)

print("logistic regression's coefficient",odds)

#predict if tumor is cancerous where the size is 3.46mm:

print(logit2prob(logr, numpy.array([1.7]).reshape(-1,1)))


# Plotting the data points
plt.scatter(X, y, color='black')

# Plotting the logistic regression curve
X_test = np.linspace(np.min(X), np.max(X), 300)
probabilities = logit2prob(logr, X_test.reshape(-1,1))
plt.plot(X_test, probabilities, color='red', linewidth=3)

# Predicting and plotting a single point
point = np.array([1.7]).reshape(-1,1)
plt.scatter(point, logr.predict(point), color='blue', marker='x', s=100)

plt.xlabel('Size')
plt.ylabel('Probability of being cancerous')
plt.title('Logistic Regression')
plt.show()


