- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can Tensorflow be used with Estimators to create feature columns and input functions?
Tensorflow can be used with Estimators to create feature columns and input functions by using one-hot encoding method. The ‘feature_column.indicator_column’ is used to return the output of this one-hot encoding technique.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.
A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model.
We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.
We will train a logistic regression model using the tf.estimator API. The model is used as a baseline for other algorithms. We use the titanic dataset with the goal of predicting passenger survival, given characteristics such as gender, age, class, etc.
Estimators use feature columns to describe how the model would interpret the raw input features. An Estimator expects a vector of numeric inputs, and feature columns will help describe how the model should convert every feature in the dataset.
Example
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone'] NUMERIC_COLUMNS = ['age', 'fare'] print("The method to use one hot encoding technique") def one_hot_cat_column(feature_name, vocab): return tf.feature_column.indicator_column( tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocab)) feature_columns = [] print("One hot encode categorical features") for feature_name in CATEGORICAL_COLUMNS: vocabulary = dftrain[feature_name].unique() feature_columns.append(one_hot_cat_column(feature_name, vocabulary)) for feature_name in NUMERIC_COLUMNS: feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))
Code credit −https://www.tensorflow.org/tutorials/estimator/boosted_trees
Output
The method to use one hot encoding technique One hot encode categorical features
Explanation
The fields in the CATEGORICAL_COLUMNS are transformed from categorical columns to one-hot-encoded columns.
Before this, it is first converted into an indicator column.
The categorical features are obtained.
- Related Articles
- How can Tensorflow be used with Estimators for feature engineering the model?
- How can Tensorflow be used to define feature columns in Python?
- How can Tensorflow be used with Estimators to perform data transformation?
- How can Tensorflow be used with Estimators to optimize the model?
- How can Tensorflow be used to create a feature extractor using Python?
- How can Tensorflow be used with Estimators to split the iris dataset?
- How can Tensorflow be used with Estimators to explore the titanic data?
- How can Tensorflow be used with estimators to visualize the titanic data?
- How can Tensorflow be used with Estimators to return a two element tuple?
- How can Tensorflow be used with Estimators to evaluate the model using Python?
- How can Tensorflow be used with Estimators to display metadata about the dataset?
- How can Tensorflow be used with Estimator to transform the feature column?
- How can Tensorflow be used with Estimators to visualize the data, and the ROC curve?
- How can Tensorflow be used with Estimators to define a function that shuffles data?
- How can Tensorflow be used with Estimators to inspect the titanic dataset using Python?
