Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Machine Learning - Logistic Regression

Back


Logistic Regression

Example: Predict Tumor

DataSet

import numpy

# X_LIST: represents the size of a tumor in centimeters.
# reshape(): convert 1-D array into 2-D array
# -1,1: 每行1列, 行数由numpy计算
# Note: X has to be reshaped into a column from a row for the LogisticRegression() function to work.
X_LIST = numpy.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)
print("X_LIST", X_LIST)
# X_LIST [[3.78]
#  [2.44]
#  [2.09]
#  [0.14]
#  [1.72]
#  [1.65]
#  [4.92]
#  [4.37]
#  [4.96]
#  [4.52]
#  [3.69]
#  [5.88]]

# Y_LIST: represents whether or not the tumor is cancerous (0 for "No", 1 for "Yes").
Y_LIST = numpy.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

print("Y_LIST", Y_LIST)
# Y_LIST [0 0 0 0 0 0 1 1 1 1 1 1]

Create Logistic Regression Model

from sklearn import linear_model

predict_model = linear_model.LogisticRegression()

predict_model.fit(X_LIST, Y_LIST)

Predict

# predict if tumor is cancerous where the size is 3.46mm:

target_value = 3.46
target_list = numpy.array(target_value).reshape(-1, 1)

predicted_value = predict_model.predict(target_list)
print(predicted_value)

# We have predicted that a tumor with a size of 3.46mm will not be cancerous.

Coefficient

log_odds = predict_model.coef_
# numpy.exp(): Calculate the exponential of all elements in the input array.
odds = numpy.exp(log_odds)

print(odds)
# [[4.03541657]]
# This tells us that as the size of a tumor increases by 1mm the odds of it being a tumor increases by 4x.

Probability

def logit2prob(logr, x):
    log_odds = logr.coef_ * x + logr.intercept_
    odds = numpy.exp(log_odds)
    probability = odds / (1 + odds)
    return (probability)


print(logit2prob(predict_model, X_LIST))
# Results Explained
# 3.78 0.61: The probability that a tumor with the size 3.78cm is cancerous is 61%.
# 2.44 0.19: The probability that a tumor with the size 2.44cm is cancerous is 19%.
# 2.09 0.13: The probability that a tumor with the size 2.09cm is cancerous is 13%.

Top