美文网首页我爱编程
python 逻辑回归——案例房屋用户画像是否拥有房屋预测

python 逻辑回归——案例房屋用户画像是否拥有房屋预测

作者: 正在充电Loading | 来源:发表于2017-09-17 14:28 被阅读0次

    import pandas;

    from pandas import read_csv;

    data = read_csv(

    'D:\\PDM\\4.4\\data.csv',

    encoding='utf8'

    )


    data = data.dropna()

    data.shape

    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

    """

    博士后    Post-Doc

    博士      Doctorate

    硕士      Master's Degree

    学士      Bachelor's Degree

    副学士    Associate's Degree

    专业院校  Some College

    职业学校  Trade School

    高中      High School

    小学      Grade School

    """

    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

    }

    dummiesData['Education Level Map'] = dummiesData['Education Level'].map(educationLevelDict)#利用map函数确定模糊变量的层次

    freqMap = {

    'Never': 0,

    'Rarely': 1,

    'Monthly': 2,

    'Weekly': 3,

    'Daily': 4

    }#确定等级,有点类似RMF中的分组分层。

    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)#确定以上这些属性的值的层次

    dummiesData.columns

    dummiesSelect = [

    'Age', 'Num Bathrooms', 'Num Bedrooms', 'Num Cars', 'Num Children', 'Num TVs',

    'Education Level Map', 'PPV Freq Map', 'Theater Freq Map', 'TV Movie Freq Map',

    'Prerec Buying Freq Map', 'Prerec Renting Freq Map', 'Prerec Viewing Freq Map',

    'Gender Male',

    'Internet Connection DSL', 'Internet Connection Dial-Up',

    'Internet Connection IDSN', 'Internet Connection No Internet Connection',

    'Internet Connection Other',

    'Marital Status Married', 'Marital Status Never Married',

    'Marital Status Other', 'Marital Status Separated',

    'Movie Selector Me', 'Movie Selector Other', 'Movie Selector Spouse/Partner',

    'Prerec Format DVD', 'Prerec Format Laserdisk', 'Prerec Format Other',

    'Prerec Format VHS', 'Prerec Format Video CD',

    'TV Signal Analog antennae', 'TV Signal Cable',

    'TV Signal Digital Satellite', 'TV Signal Don\'t watch TV'

    ]#自变量选择

    inputData = dummiesData[dummiesSelect]

    outputData = dummiesData[['Home Ownership Rent']]#因变量的确定

    from sklearn import linear_model#导入线性模型

    lrModel = linear_model.LogisticRegression()

    lrModel.fit(inputData, outputData)

    lrModel.score(inputData, outputData)#确定匹配得分

    newData = read_csv(

    'D:\\PDM\\4.4\\newData.csv',

    encoding='utf8'

    )#导入新的数据

    for column in dummyColumns:

    newData[column] = newData[column].astype(

    'category',

    categories=data[column].cat.categories

    )#确定模糊类别

    newData = newData.dropna()

    newData['Education Level Map'] = newData['Education Level'].map(educationLevelDict)

    newData['PPV Freq Map'] = newData['PPV Freq'].map(freqMap)

    newData['Theater Freq Map'] = newData['Theater Freq'].map(freqMap)

    newData['TV Movie Freq Map'] = newData['TV Movie Freq'].map(freqMap)

    newData['Prerec Buying Freq Map'] = newData['Prerec Buying Freq'].map(freqMap)

    newData['Prerec Renting Freq Map'] = newData['Prerec Renting Freq'].map(freqMap)

    newData['Prerec Viewing Freq Map'] = newData['Prerec Viewing Freq'].map(freqMap)

    dummiesNewData = pandas.get_dummies(

    newData,

    columns=dummyColumns,

    prefix=dummyColumns,

    prefix_sep=" ",

    drop_first=True

    )#处理模糊变量

    inputNewData = dummiesNewData[dummiesSelect]

    lrModel.predict(inputData)

    #输出预测的结果。

    虚拟变量(dummy variables)

    虚拟变量,也叫哑变量和离散特征编码,可用来表示分类变量、非数量因素可能产生的影响。

    ① 离散特征的取值之间有大小的意义

    例如:尺寸(L、XL、XXL)

    离散特征的取值有大小意义的处理函数map

    pandas.Series.map(dict)

    参数 dict:映射的字典

    ② 离散特征的取值之间没有大小的意义

    pandas.get_dummies

    例如:颜色(Red,Blue,Green)

    处理函数:

    get_dummies(data,prefix=None,prefix_sep="_",dummy_na=False,columns=None,drop_first=False)

    ① data   要处理的DataFrame

    ② prefix 列名的前缀,在多个列有相同的离散项时候使用

    ③ prefix_sep 前缀和离散值的分隔符,默认为下划线,默认即可

    ④ dummy_na 是否把NA值,作为一个离散值进行处理,默认为不处理

    ⑤ columns 要处理的列名,如果不指定该列,那么默认处理所有列

    ⑥ drop_first 是否从备选项中删除第一个,建模的时候为避免共线性使用

    相关文章

      网友评论

        本文标题:python 逻辑回归——案例房屋用户画像是否拥有房屋预测

        本文链接:https://www.haomeiwen.com/subject/eaupsxtx.html