Book Image

Data Cleaning and Exploration with Machine Learning

By : Michael Walker
Book Image

Data Cleaning and Exploration with Machine Learning

By: Michael Walker

Overview of this book

Many individuals who know how to run machine learning algorithms do not have a good sense of the statistical assumptions they make and how to match the properties of the data to the algorithm for the best results. As you start with this book, models are carefully chosen to help you grasp the underlying data, including in-feature importance and correlation, and the distribution of features and targets. The first two parts of the book introduce you to techniques for preparing data for ML algorithms, without being bashful about using some ML techniques for data cleaning, including anomaly detection and feature selection. The book then helps you apply that knowledge to a wide variety of ML tasks. You’ll gain an understanding of popular supervised and unsupervised algorithms, how to prepare data for them, and how to evaluate them. Next, you’ll build models and understand the relationships in your data, as well as perform cleaning and exploration tasks with that data. You’ll make quick progress in studying the distribution of variables, identifying anomalies, and examining bivariate relationships, as you focus more on the accuracy of predictions in this book. By the end of this book, you’ll be able to deal with complex data problems using unsupervised ML algorithms like principal component analysis and k-means clustering.
Table of Contents (23 chapters)
1
Section 1 – Data Cleaning and Machine Learning Algorithms
5
Section 2 – Preprocessing, Feature Selection, and Sampling
9
Section 3 – Modeling Continuous Targets with Supervised Learning
13
Section 4 – Modeling Dichotomous and Multiclass Targets with Supervised Learning
19
Section 5 – Clustering and Dimensionality Reduction with Unsupervised Learning

Using KNN imputation

KNN is a popular machine learning technique because it is intuitive, easy to run, and yields good results when there are not a large number of features and observations. For the same reasons, it is often used to impute missing values. As its name suggests, KNN identifies the k observations whose features are most similar to each observation. When it's used to impute missing values, KNN uses the nearest neighbors to determine what fill values to use.

We can use KNN imputation to do the same imputation we did in the previous section on regression imputation:

  1. Let's start by importing KNNImputer from scikit-learn and loading the NLS data again:
    import pandas as pd
    import numpy as np
    from sklearn.impute import KNNImputer
    nls97 = pd.read_csv("data/nls97b.csv")
    nls97.set_index("personid", inplace=True)
  2. Next, we must prepare the features. We collapse degree attainment into three categories – less than...