邏輯迴歸中的係數的意義

PzLu發表於2018-11-27
#%%
import math
from sklearn import linear_model
#samples'features, there are 6 samples
X = [[20,3],
     [23,7],
     [31,10],
     [42,13],
     [50,7],
     [60,5]]
#relative tags
y = [0,
     1,
     1,
     1,
     0,
     0]
#create the object and train the model
# theta_x = theta_0 + theta_1*x_1 + theta_2*x_2
lr = linear_model.LogisticRegression()
# get the good value of the theta_0, theta_1 and theta_2
lr.fit(X,y)

#there is a test
testX = [[28,8]]
#predict the test
label = lr.predict(testX)
print("predicted label = ", label)
#and the probability
prob = lr.predict_proba(testX)
print("probability =  ", prob)

#the coefficient meaning
#%%
theta_0 = lr.intercept_
theta_1 = lr.coef_[0][0]
theta_2 = lr.coef_[0][1]
print("theta_0 =", theta_0)
print("theta_1 =", theta_1)
print("theta_2 =", theta_2)
#the previous data
testX = [[28,8]]
# get the probability of y=0 and y=1
prob = lr.predict_proba(testX)
ratio = prob[0][1]/prob[0][0]

# the value of the second feature + 1
# 8->9 then, relate the value of theta_2
testX = [[28,9]] 
prob_new = lr.predict_proba(testX)
ratio_new = prob_new[0][1]/prob_new[0][0]

#the ratio's ratio
ratio_of_ratio = ratio_new / ratio
print("ratio_of_ratio = ",ratio_of_ratio)

#test
theta_2_e = math.exp(theta_2)
print("e^theta _2 = ", theta_2_e)


相關文章