pandas - 读取csv文件并对tensorflow中的分类变量进行多热编码
问题描述
我正在从 csv 文件中读取数据。如果我的特征是分类的,我可以使用下面的代码对分类变量进行热编码。
import tensorflow as tf
import tensorflow.feature_column as fc
import pandas as pd
PATH = "/tmp/sample.csv"
tf.enable_eager_execution()
COLUMNS = ['education','label']
train_df = pd.read_csv(PATH, header=None, names = COLUMNS)
train_df['education'] = train_df['education'].str.split(" ").astype(str)
def easy_input_function(df, label_key, num_epochs, shuffle, batch_size):
label = df[label_key]
#ed = tf.string_split(df['education']," ")
#df['education'] = ed
ds = tf.data.Dataset.from_tensor_slices((dict(df),label))
if shuffle:
ds = ds.shuffle(10000)
ds = ds.batch(batch_size).repeat(num_epochs)
return ds
ds = easy_input_function(train_df, label_key='label', num_epochs=5, shuffle=False, batch_size=5)
for feature_batch, label_batch in ds.take(1):
print('Some feature keys:', list(feature_batch.keys())[:5])
print()
print('A batch of education :', feature_batch['education'])
print()
print('A batch of Labels:', label_batch )
print(feature_batch)
education_vocabulary_list = [
'Bachelors', 'HS-grad', '11th', 'Masters', '9th', 'Some-college',
'Assoc-acdm', 'Assoc-voc', '7th-8th', 'Doctorate', 'Prof-school',
'5th-6th', '10th', '1st-4th', 'Preschool', '12th']
education = tf.feature_column.categorical_column_with_vocabulary_list('education', vocabulary_list=education_vocabulary_list)
fc.input_layer(feature_batch, [fc.indicator_column(education)])
我的 sample.csv 文件数据看起来像
Bachelors,1
HS-grad,0
但是当我在分类特征中有多个值时,上面的代码无法对数据进行多热编码。
说我的 sample.csv 就像
Bachelors HS-grad,1
HS-grad,0
关于我应该如何读取或将变量放入 csv 文件的任何线索,以便我能够在我的模型中对它们进行多热编码。
解决方案
推荐阅读
- pandas - 匹配过程后消除行并保持最少数据
- python - 如何使用 Jupyter Widgets 创建依赖下拉列表以从 dict 获取数据?
- html - CSS 的行为很奇怪,特别是间距
- amazon-web-services - 自定义 AWS WorkMail 过滤
- java - BST 的删除函数不会从 BST 中删除任何元素
- python - 如何在类实例上正确执行算术?例如,对 House 类实例列表的 n_people 属性求和
- android - 如何在不同的线程中运行绑定服务
- python - 如何去除列表中每个项目的前导 u?
- c++ - 如何在 C++ 中正确解析 CSV 文件?
- gem5 - 关于Gem5全系统模式传输文件到ARM Linux的问题