python中tensorflow实现二分类_python – Tensorflow中的分类和连续交叉特征列

看到在Tensforflow图中实现相同结果的任何替代方法也会很棒.

import numpy as np

cont = np.array([1,2,3])

cat = np.array(['cat', 'dog', 'cat'])

cross_function(cat, cont) = np.array([[1,0],[0,2],[3,0]])

解决方法:

在这里回答我自己的问题.涉及的步骤是:

>对分类特征进行数字编码

>在图表中,因此可以在火车和服务范围内

>一个热编码数值结果

>将其与连续变量相乘

码:

import numpy as np

import tensorflow as tf

cont = np.array([1,2,3])

cat = np.array(['cat', 'dog', 'cat'])

categories = np.unique(cat)

def categorical_continuous_interaction(categorical_onehot, continuous):

cont = tf.expand_dims(continuous, 0)

return tf.transpose(tf.multiply(tf.transpose(categorical_onehot), cont))

def transformation_function(feature_dictionary, mapping_table):

continuous_feature = feature_dictionary['cont']

categorical_feature = mapping_table.lookup(feature_dictionary['cat'])

onehot = tf.one_hot(categorical_feature, categories.shape[0])

cross_feature = categorical_continuous_interaction(onehot, continuous_feature)

return {'feature_name': cross_feature}

def input_function(dataframe, label_key, ...):

# categorical mapping tables, these must be generated outside of the dataset

# transformation function but within the input function

mapping_table = tf.contrib.lookup.index_table_from_tensor(

mapping=tf.constant(categories),

num_oov_buckets=0,

default_value=-1

)

# Generate the dataset of a dictionary of all of the dataframes columns

dataset = tf.data.Dataset.from_tensor_slices(dict(dataframe))

# Convert to a dataset of tuples of dicts with the labels as one tuple

dataset = dataset.map(lambda x: split_label(x, label_key))

# Transform the features dict within the dataset

dataset = dataset.map(lambda features, labels: (transformation_function(

features, mapping_table=mapping_table), labels))

...

return dataset

def serving_input_fn():

# categorical mapping tables, these must be generated outside of the dataset

# transformation function but within the input function

mapping_table=tf.contrib.lookup.index_table_from_tensor(

mapping=tf.constant(categories),

num_oov_buckets=0,

default_value=-1

)

numeric_receiver_tensors = {

name: tf.placeholder(dtype=tf.float32, shape=[1], name=name+"_placeholder")

for name in numeric_feature_column_names

}

categorical_receiver_tensors = {

name: tf.placeholder(dtype=tf.string, shape=[1], name=name+"_placeholder")

for name in categorical_feature_column_names

}

receiver_tensors = {**numeric_receiver_tensors, **categorical_receiver_tensors}

features = transformation_function(receiver_tensors,

country_mapping_table=country_mapping_table)

return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

标签:python,python-3-x,tensorflow

来源: https://codeday.me/bug/20190705/1387727.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值