码迷,mamicode.com
首页 > 编程语言 > 详细

Python数据挖掘—回归—逻辑回归

时间:2018-10-04 18:28:29      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:column   end   mon   some   gen   buying   status   虚拟变量   oci   

概念

针对因变量为分类变量而进行回归分析的一种统计方法,属于概率型非线性回归

  优点:算法易于实现和部署,执行效率和准确度高

  缺点:离散型的自变量数据需要通过生成虚拟变量的方式来使用 

在线性回归中,因变量是连续性变量,那么线性回归能根据因变量和自变量存在的线性关系来构造回归方程,因变量变成分类变量后就不存在这种关系了,需通过对数变换来进行处理(Sigmoid函数)

 

步骤:

1、读取数据;

import pandas
from pandas import read_csv

data=read_csv(
    "C:\\Users\\Jw\\Desktop\\python_work\\Python数据挖掘实战课程课件\\4.4\\data.csv",
    encoding="utf-8")

data=data.dropna()

dummyColumns=()

data.shape

  

2、处理字符型和大小无关的字段,如果字段有可比性,可进行大小比较,然后调用map一一映射,将离散型数据转化为数值型数据

  首先处理字符类型和大小无关的字段

#首先处理字符类型和大小无关的字段
dummyColumns=[
    Gender,Home Ownership,
    Internet Connection, Marital Status,
    Movie Selector, Prerec Format, TV Signal]
    
for column in dummyColumns:
    data[column]=data[column].astype(category)
    

dummiesData=pandas.get_dummies(
        data,
        columns=dummyColumns,
        prefix=dummyColumns,
        prefix_sep=" ",  #列名和属性值之间的分割符号
        drop_first=True)  #根据特征列建模,为避免模型共轭,只选取一列

data.Gender.unique()   #去重

dummiesData.columns   #获取所有列

  处理字符类型和大小有关的字段,然后使用map一一映射

#有可比性,可进行大小比较
educationLevelDict={
    Post-Doc: 9,
    Doctorate: 8,
    Master\‘s Degree: 7,
    Bachelor\‘s Degree: 6,
    Associate\‘s Degree: 5,
    Some College: 4,
    Trade School: 3,
    High School: 2,
    Grade School: 1
    }
    
#调用map一一映射,将离散型数据转化为数值型数据
dummiesData["Education Level Map"]=dummiesData[Education Level].map(educationLevelDict)


freqMap={
    Never:0,
    Rarely: 1,
    Monthly: 2,
    Weekly: 3,
    Daily: 4}
    
dummiesData[PPV Freq Map]=dummiesData[PPV Freq].map(freqMap)
dummiesData[Theater Freq Map] = dummiesData[Theater Freq].map(freqMap)
dummiesData[TV Movie Freq Map] = dummiesData[TV Movie Freq].map(freqMap)
dummiesData[Prerec Buying Freq Map] = dummiesData[Prerec Buying Freq].map(freqMap)
dummiesData[Prerec Renting Freq Map] = dummiesData[Prerec Renting Freq].map(freqMap)
dummiesData[Prerec Viewing Freq Map] = dummiesData[Prerec Viewing Freq].map(freqMap)

 

 

3、选取自标量和因变量,县选取所有列,然后一一查看选择

 1 #选取自变量和因变量
 2 dummiesData.columns
 3 
 4 #先选取所有列,然后一一查看选择
 5 dummiesSelect = [
 6     Age, Num Bathrooms, Num Bedrooms, Num Cars, Num Children, Num TVs, 
 7     Education Level Map, PPV Freq Map, Theater Freq Map, TV Movie Freq Map, 
 8     Prerec Buying Freq Map, Prerec Renting Freq Map, Prerec Viewing Freq Map, 
 9     Gender Male,
10     Internet Connection DSL, Internet Connection Dial-Up, 
11     Internet Connection IDSN, Internet Connection No Internet Connection,
12     Internet Connection Other, 
13     Marital Status Married, Marital Status Never Married, 
14     Marital Status Other, Marital Status Separated, 
15     Movie Selector Me, Movie Selector Other, Movie Selector Spouse/Partner, 
16     Prerec Format DVD, Prerec Format Laserdisk, Prerec Format Other, 
17     Prerec Format VHS, Prerec Format Video CD, 
18     TV Signal Analog antennae, TV Signal Cable, 
19     TV Signal Digital Satellite, TV Signal Don\‘t watch TV
20 ]
21 
22 inputData=dummiesData[dummiesSelect]   #自变量
23 
24 
25 outputData=dummiesData[["Home Ownership Rent"]]   #因变量

 

 

4、建模、训练、评分   

1 #建模、训练
2 from sklearn import linear_model
3 
4 lrModel=linear_model.LogisticRegression()
5 
6 lrModel.fit(inputData,outputData)
7 
8 lrModel.score(inputData,outputData)

 

5、预测(因为逻辑回归所用的参数是经过虚拟变量处理过的,所以新数据也许通过处理才能进行预测)

 1 #因为逻辑回归所用的参数是经过虚拟变量处理过的,需对新的数据进行预测,要先处理新数据
 2 newData=read_csv(
 3     "C:\\Users\\Jw\\Desktop\\python_work\\Python数据挖掘实战课程课件\\4.4\\newData.csv",
 4     encoding="utf-8")
 5 
 6 for column in dummyColumns:
 7     newData[column]=newData[column].astype(
 8         "category",
 9         categories=data[column].cat.categories)
10     
11 newData=newData.dropna()
12 
13 
14 newData[Education Level Map] = newData[Education Level].map(educationLevelDict)
15 newData[PPV Freq Map] = newData[PPV Freq].map(freqMap)
16 newData[Theater Freq Map] = newData[Theater Freq].map(freqMap)
17 newData[TV Movie Freq Map] = newData[TV Movie Freq].map(freqMap)
18 newData[Prerec Buying Freq Map] = newData[Prerec Buying Freq].map(freqMap)
19 newData[Prerec Renting Freq Map] = newData[Prerec Renting Freq].map(freqMap)
20 newData[Prerec Viewing Freq Map] = newData[Prerec Viewing Freq].map(freqMap)
21 
22 
23 dummiesNewData=pandas.get_dummies (
24         newData,
25         columns=dummyColumns,
26         prefix=dummyColumns,
27         prefix_sep=" ",
28         drop_first=True)
29 
30 inputNewData = dummiesNewData[dummiesSelect]
31 
32 lrModel.predict(inputData)

 

Python数据挖掘—回归—逻辑回归

标签:column   end   mon   some   gen   buying   status   虚拟变量   oci   

原文地址:https://www.cnblogs.com/U940634/p/9742721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!