-
Book Overview & Buying
-
Table Of Contents
Python Feature Engineering Cookbook - Second Edition
By :
In this chapter, we will use the pandas, NumPy, and Matplotlib Python libraries, as well as scikit-learn and Feature-engine. For guidelines on how to obtain these libraries, visit the Technical requirements section of Chapter 1, Imputing Missing Data.
We will also use the open-source Category Encoders Python library, which can be installed using pip:
pip install category_encoders
To learn more about Category Encoders, visit the following link: https://contrib.scikit-learn.org/category_encoders/.
We will also use the Credit Approval dataset, which is available in the UCI Machine Learning Repository at https://archive.ics.uci.edu/ml/datasets/credit+approval.
To prepare the dataset, follow these steps:
crx.data to download the data:
Figure 2.1 – The index directory for the Credit Approval dataset
crx.data to the folder where you will run the following commands.After downloading the data, open up a Jupyter Notebook and run the following commands.
import random import numpy as np import pandas as pd
data = pd.read_csv("crx.data", header=None)varnames = [f"A{s}" for s in range(1, 17)]data.columns = varnames
data = data.replace("?", np.nan)float data types:data["A2"] = data["A2"].astype("float")
data["A14"] = data["A14"].astype("float")data["A16"] = data["A16"].map({"+": 1, "-": 0})data.rename(columns={"A16": "target"}, inplace=True)cat_cols = [ c for c in data.columns if data[c].dtypes=="O"] num_cols = [ c for c in data.columns if data[c].dtypes!= "O"]
data[num_cols] = data[num_cols].fillna(0)
data[cat_cols] = data[cat_cols].fillna("Missing")data.to_csv("credit_approval_uci.csv", index=False)You can find a Jupyter Notebook that contains these commands in this book’s GitHub repository at https://github.com/PacktPublishing/Python-Feature-Engineering-Cookbook-Second-Edition/blob/main/ch02-categorical-encoding/donwload-prepare-store-credit-approval-dataset.ipynb.
Note
Some libraries require that you have already imputed missing data, for which you can use any of the recipes from Chapter 1, Imputing Missing Data.