Docsity

Data Preprocessing 1, Slides of Data Acquisition

Partial preview of the text

Download Data Preprocessing 1 and more Slides Data Acquisition in PDF only on Docsity!

Data Pre-processing in

Machine Learning

Objectives

  • (^) Discuss the importance of data pre-processing.
  • (^) Identify the tools required to pre-process the data.
  • (^) Select columns and rows in a dataframe using iloc.
  • (^) Identify missing values in the dataframe.
  • (^) Handle missing values in the dataframe.
  • (^) Demonstrate data pre-processing in machine learning.

What is data pre-processing?

  • (^) Data preprocessing is an integral step in Machine Learning as the quality of data and the useful information that can be derived from it directly affects the ability of our model to learn; therefore, we must preprocess our data before feeding it into our model.
  • (^) The goal of data preprocessing is to improve model performance and accuracy.
  • (^) One of the most important steps in data pre-processing is handling null values.

Select columns and rows using iloc

  • (^) Python iloc() function enables us to select a particular cell of the dataset; it helps us select a value that belongs to a particular row or column from a set of values of a data frame or dataset.
  • (^) With iloc() function, we can retrieve a particular value belonging to a row and column using the index values assigned to it.
  • (^) iloc() function accepts only integer type values as the index values for the values to be accessed and displayed.

Syntax:

dataframe.iloc[] we cannot pass boolean values as an index to retrieve the records. It is mandatory to pass integer values to it.

Sample Code and Output

Sample Code and Output

Syntax

dataframe.iloc[:,start_col:end_col] Explanation: Access all the data values of the columns 1 and 2 as shown on the next slide, data.iloc[:,1:3]

Important points when using iloc

  • (^) It can be used to fetch records based on the index values from the datasets.
  • (^) Multiple records can be fetched using the index as a parameter to the iloc() function.
  • (^) The iloc() function considers only integer indexes as parameters.

Handling Null Values

  • (^) The official documentation for pandas defines what most developers would know as null values as missing or missing data in pandas. Within pandas, NaN denotes a missing value.
  • (^) In Python, NULL is represented with NaN.
  • (^) NULL and Nan can be used interchangeably.

Strategies in SimpleImputer

  • (^) Mean (strategy='mean')
  • (^) Replaces missing values using the mean value along each column.
  • (^) Suitable for numerical data where the mean value is a good representative statistic.
  • (^) Sample Code: from sklearn.impute import SimpleImputer imputer = SimpleImputer(strategy='mean')

Strategies in SimpleImputer

  • (^) Median (strategy='median')
  • (^) Replaces missing values using the median value along each column.
  • (^) Useful for numerical data that may have outliers, as the median is less sensitive to extreme values than the mean.
  • (^) Sample Code: from sklearn.impute import SimpleImputer imputer = SimpleImputer(strategy='median')

When to Use Each Strategy

  • (^) Mean
    • (^) When data is symmetrically distributed without outliers.
  • (^) Median
    • (^) When data contains outliers or is skewed.
  • (^) Most Frequent
    • (^) For categorical data or when a majority value is a good representative.

Checking missing values in a

dataset

  • (^) We need to check whether we have null values in our dataset or not. We can do that using the isnull() method.
  • (^) df.isna() or df.isnull() is a function that shows “Not A Number” (NAN) or missing numbers.
  • (^) df.isna().sum() shows the number of NANs or missing numbers in each column.
  • (^) df.isna().sum().sum() shows the total number of NANs or missing numbers of the data.
  • (^) df.dropna() removes the NANs.