tensorflow 做多元线性回归时怎样对非数据型数据(分类型数据)进行处理(编码)

代码如下:

def read_data(file_queue):
    '''
    the function is to get features and label (即样本特征和样本的标签)
    数据来源是csv的文件,采用tensorflow 自带的对csv文件的处理方式
    :param file_queue:
    :return: features,label
    '''
    # 读取的时候需要跳过第一行
    reader = tf.TextLineReader(skip_header_lines=1)
    key, value = reader.read(file_queue)
    # 对于数据源中空的值设置默认值
    record_defaults = [[''], [''], [''], [''], [0.], [0.], [0.], [0.], [''],[0], [''], [0.], [''], [''], [0]]
    # 定义decoder,每次读取的执行都从文件中读取一行。然后,decode_csv 操作将结果解析为张量列表
    province, city, address, postCode, longitude,latitude, price, buildingTypeId, buildingTypeName, tradeTypeId, tradeTypeName, expectedDealPrice, listingDate, delislingDate, daysOnMarket = tf.decode_csv(value, record_defaults)
    #对非数值数据进行编码:buildingTypeName
    preprocess_buildingTypeName_op = tf.case({
        tf.equal(buildingTypeName, tf.constant('Residential')): lambda: tf.constant(0.00),
        tf.equal(buildingTypeName, tf.constant('Condo')): lambda: tf.constant(1.00),
        tf.equal(buildingTypeName, tf.constant('Mobile Home')): lambda: tf.constant(2.00),
        tf.equal(buildingTypeName, tf.constant('No Building')): lambda: tf.constant(3.00),
        tf.equal(buildingTypeName, tf.constant('Row / Townhouse')): lambda: tf.constant(4.00),
        tf.equal(buildingTypeName, tf.constant('Duplex')): lambda: tf.constant(5.00),
        tf.equal(buildingTypeName, tf.constant('Manufactured Home')): lambda: tf.constant(6.00),
        tf.equal(buildingTypeName, tf.constant('Commercial')): lambda: tf.constant(7.00),
        tf.equal(buildingTypeName, tf.constant('Other')): lambda: tf.constant(8.00),
    }, lambda: tf.constant(-1.00), exclusive=True)
    # 对tradeTypeName 进行编码 Sale,Lease
    preprocess_tradeTypeName_op = tf.case({
        tf.equal(tradeTypeName, tf.constant('Sale')): lambda: tf.constant(0.00),
        tf.equal(tradeTypeName, tf.constant('Lease')): lambda: tf.constant(1.00),
    }, lambda: tf.constant(-1.00), exclusive=True)
    features = tf.stack([latitude,longitude,price, preprocess_buildingTypeName_op, preprocess_tradeTypeName_op,expectedDealPrice])

    return features, daysOnMarket

也就是通过:tf.case ,tf.equal和lambda 函数来实现

原文地址:https://www.cnblogs.com/bluesl/p/9215802.html