5-2 feature_column

    Feature column is used to converting category features into one-hot encoding, or creating bucketing feature from continuous feature, or generating cross features from multiple features, etc.

    Before creating feature column, please call the functions in the module . The nine most frequently used functions in this module are shown in the figure below. All these functions will return a Categorical-Column or a Dense-Column object, but will not return bucketized_column, since the last class is inhereted from the first two classes.

    • numeric_column, the most frequently used function.
    • bucketized_column, generated from numerical column, listing multiple features from a numerical clumn; it is one-hot encoded.
    • categorical_column_with_identity, one-hot encoded, identical to the case that each bucket is one interger.
    • categorical_column_with_vocabulary_list, one-hot encoded; the dictionary is specified by the list.
    • categorical_column_with_vocabulary_file, one-hot encoded; the dictionary is specified by the file.
    • categorical_column_with_hash_bucket, used in the case with a large interger or a large dictionary.
    • indicator_column, generated by Categorical-Column; one-hot encoded.
    • embedding_column, generated by Categorical Column; the embedded vector distributed parameter needs learning/training. The recommended dimension of the embedded vector is the fourth root to the number of categories.
    • crossed_column, consists of arbitrary category column except for categorical_column_with_hash_bucket

    2. Demonstration of feature column

    Here is a complete example that solves Titanic survival problmen using feature column.

    1. #================================================================================
    2. # 1. Constructing data pipeline
    3. #================================================================================
    4. printlog("step1: prepare dataset...")
    5. dftrain_raw = pd.read_csv("../data/titanic/train.csv")
    6. dftest_raw = pd.read_csv("../data/titanic/test.csv")
    7. dfraw = pd.concat([dftrain_raw,dftest_raw])
    8. def prepare_dfdata(dfraw):
    9. dfdata = dfraw.copy()
    10. dfdata.columns = [x.lower() for x in dfdata.columns]
    11. dfdata = dfdata.rename(columns={'survived':'label'})
    12. dfdata = dfdata.drop(['passengerid','name'],axis = 1)
    13. for col,dtype in dict(dfdata.dtypes).items():
    14. # See if there are missing values.
    15. if dfdata[col].hasnans:
    16. # Adding signs to the missing columns
    17. dfdata[col + '_nan'] = pd.isna(dfdata[col]).astype('int32')
    18. dfdata[col].fillna(dfdata[col].mean(),inplace = True)
    19. else:
    20. dfdata[col].fillna('',inplace = True)
    21. return(dfdata)
    22. dfdata = prepare_dfdata(dfraw)
    23. dftrain = dfdata.iloc[0:len(dftrain_raw),:]
    24. dftest = dfdata.iloc[len(dftrain_raw):,:]
    25. # Importing data from dataframe
    26. def df_to_dataset(df, shuffle=True, batch_size=32):
    27. dfdata = df.copy()
    28. if 'label' not in dfdata.columns:
    29. ds = tf.data.Dataset.from_tensor_slices(dfdata.to_dict(orient = 'list'))
    30. else:
    31. labels = dfdata.pop('label')
    32. ds = tf.data.Dataset.from_tensor_slices((dfdata.to_dict(orient = 'list'), labels))
    33. if shuffle:
    34. ds = ds.shuffle(buffer_size=len(dfdata))
    35. ds = ds.batch(batch_size)
    36. return ds
    37. ds_train = df_to_dataset(dftrain)
    38. ds_test = df_to_dataset(dftest)
    1. #================================================================================
    2. # 3. Defining the model
    3. #================================================================================
    4. printlog("step3: define model...")
    5. layers.DenseFeatures(feature_columns), # Placing the feature into tf.keras.layers.DenseFeatures
    6. layers.Dense(64, activation='relu'),
    7. layers.Dense(64, activation='relu'),
    8. layers.Dense(1, activation='sigmoid')
    9. ])
    1. #================================================================================
    2. # 5. Evaluating the model
    3. #================================================================================
    4. printlog("step5: eval model...")
    5. model.summary()
    6. %matplotlib inline
    7. %config InlineBackend.figure_format = 'svg'
    8. import matplotlib.pyplot as plt
    9. def plot_metric(history, metric):
    10. train_metrics = history.history[metric]
    11. val_metrics = history.history['val_'+metric]
    12. epochs = range(1, len(train_metrics) + 1)
    13. plt.plot(epochs, train_metrics, 'bo--')
    14. plt.plot(epochs, val_metrics, 'ro-')
    15. plt.title('Training and validation '+ metric)
    16. plt.xlabel("Epochs")
    17. plt.ylabel(metric)
    18. plt.legend(["train_"+metric, 'val_'+metric])
    19. plt.show()
    20. plot_metric(history,"accuracy")

    Please leave comments in the WeChat official account “Python与算法之美” (Elegance of Python and Algorithms) if you want to communicate with the author about the content. The author will try best to reply given the limited time available.

    You are also welcomed to join the group chat with the other readers through replying 加群 (join group) in the WeChat official account.