Logistic regression
需要安装的包:(1)sklearn(2)pandas(3)numpy(4)matplotlib.
Set up dataset
from collections import OrderedDict
import pandas as pd
#dataset
examDict = {
'学习时间':[0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.50,
2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50],
'通过考试':[0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
}
examOrderedDict = OrderedDict(examDict)
examDf = pd.DataFrame(examOrderedDict)
examDf.head(5)
学习时间 | 通过考试 | |
---|---|---|
0 | 0.50 | 0 |
1 | 0.75 | 0 |
2 | 1.00 | 0 |
3 | 1.25 | 0 |
4 | 1.50 | 0 |
extract features and labels
#features
exam_X = examDf.loc[:,'学习时间']
#labels
exam_Y = examDf.loc[:,'通过考试']
draw scatter plot
import matplotlib.pyplot as plt
# scatter plot
plt.scatter(exam_X,exam_Y,color = 'blue',label = 'exam data')
#add icon label
plt.xlabel('Hours')
plt.ylabel('Pass')
#show image
plt.show()
Establish train data set and test data set
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(exam_X,
exam_Y,
train_size = 0.8,
test_size = 0.2)
#output data size
print('原始数据特征:',exam_X.shape ,
'训练数据特征:',X_train.shape ,
'测试数据特征:',X_test.shape)
print('原始数据标签:',exam_Y.shape ,
'训练数据标签:', Y_train.shape ,
'测试数据标签:' ,Y_test.shape)
原始数据特征: (20,) 训练数据特征: (16,) 测试数据特征: (4,)
原始数据标签: (20,) 训练数据标签: (16,) 测试数据标签: (4,)
#draw scatter plot
import matplotlib.pyplot as plt
#scatter plot
plt.scatter(X_train,Y_train,color ='blue',label = "train data")
plt.scatter(X_test,Y_test,color = 'red',label = "test data")
#add icon label
plt.legend(loc=2)
plt.xlabel('Hours')
plt.ylabel('Pass')
#show image
plt.show()
training model(using train data)
#'sklearn'requires that the features of the input must be a two-dimensional array type,
# otherwise an error will be reported
#convert the training data features into a two-dimensional array XX rows * 1 column
X_train = X_train.reshape(-1,1)
#convert the training data features into a two-dimensional array XX rows * 1 column
X_test = X_test.reshape(-1,1)
#step1:improt logistic regression
from sklearn.linear_model import LogisticRegression
#step2:create model logistic regression
model =LogisticRegression()
model.fit(X_train,Y_train)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
verbose=0, warm_start=False)
model evaluation(using test data)
#ecaluation model:accuracy
model.score(X_test,Y_test)
1.0
Futher understanding,what’s the logical function
# get the probability value
#the first value is the probability that the label is 0
#the second value is the probability that the labei is 1
#when the learning time is 3 hours
model.predict_proba(3)
array([[ 0.44687025, 0.55312975]])
'''
decision surface
y >= 0.5,label=1
y < 0.5,label=0
when the study time is 3 hours,the student was able to pass exam
'''
#forecast data
pred =model.predict([[3]])
print(pred)
[1]
import numpy as np
'''
understand logistic regression functions
intercept
slope
'''
#step1: get 'z' value of the regression equation
#regression equation: z=a+bx
#intercept
a = model.intercept_
#regression coeffcient
b = model.coef_
x=3
z=a+b*x
#step2: plug the z value into the logistic regression function to get the probability value
y_pred = 1/(1+np.exp(-z))
print('预测的概率值',y_pred)
预测的概率值 [[ 0.55312975]]