python数据分析005—逻辑回归

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)
学习时间通过考试
00.500
10.750
21.000
31.250
41.500

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]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值