How to process categorical features in Python?

How to process categorical features in Python?

How to process categorical features in Python?

This recipe helps you process categorical features in Python

This python source code does the following: 1. Creates data dictionary and converts it into dataframe 2. Uses pandas "get_dummies" for encoding 3. Applies "label encoding" function for encoding
In [1]:
## How to process categorical features in Python 
def Kickstarter_Example_38():
    print(format('How to process categorical features in Python', '*^82'))

    import warnings

    # load libraries
    from sklearn import preprocessing

    #from sklearn.pipeline import Pipeline
    import pandas as pd

    # Create Data
    raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'age': [42, 52, 36, 24, 73],
                'city': ['San Francisco', 'Baltimore', 'Miami', 'Douglas', 'Boston']}
    df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'city'])
    print(); print(df)

    # Convert Nominal Categorical Feature Into Dummy Variables Using Pandas
    # Create dummy variables for every unique category in
    print(); print(pd.get_dummies(df["city"]))

    # Convert Nominal Categorical Data Into Dummy (OneHot) Features Using Scikit
    # Convert strings categorical names to integers
    integerized_data = preprocessing.LabelEncoder().fit_transform(df["city"])

    # View data
    print(); print(integerized_data)

    # Convert integer categorical representations to OneHot encodings
    output = preprocessing.OneHotEncoder().fit_transform(integerized_data.reshape(-1,1)).toarray()
    print(); print(output)

******************How to process categorical features in Python*******************

  first_name last_name  age           city
0      Jason    Miller   42  San Francisco
1      Molly  Jacobson   52      Baltimore
2       Tina       Ali   36          Miami
3       Jake    Milner   24        Douglas
4        Amy     Cooze   73         Boston

   Baltimore  Boston  Douglas  Miami  San Francisco
0          0       0        0      0              1
1          1       0        0      0              0
2          0       0        0      1              0
3          0       0        1      0              0
4          0       1        0      0              0

[4 0 3 2 1]

[[0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0.]]

Relevant Projects

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.