Skip to main content

Automatic preprocessing library for dataset cleaning

Project description

G4_PreprocessingLib

G4_PreprocessingLib is a Python library that provides a comprehensive toolkit for cleaning, preprocessing, and managing a pandas DataFrame.

Features

  1. CleanData Method:

This method cleans the dataset by handling NaN values based on column type (object or numerical). It will delete columns with NaN values exceeding a user-defined threshold (lim_nan) that by default is set at 65%. It also detects and handles outliers in numerical columns using the Z-score method. Finally, it supports multiple strategies for imputing missing values (mean, median, mode, bfill, ffill, max, and min). Bare in mind some of these methods can only be used for numerical values. Finally, threshold to discard outlier values can also be determined by the user but by default will have a value of 3 as it is the most common criteria to discard outlier values.

  1. EncodeColumn Method:

Encodes categorical data in a specified column to numeric values using LabelEncoder. It has an option to choose if the user ones to mantain the original column and add an additional encoded column at the end or if the user wants to simply replace the existing column with the encoded one.

  1. DropColumn Method:

Removes a specified column by its index, with error handling for out-of-range indices.

  1. HomogenizeData Method:

Standardizes object type columns by converting its values to lowercase and stripping whitespaces.

Installation

To use this library the user must ensure that the following libraries are installed and then use the package manager pip to install the library.

pip install pandas numpy scipy scikit-learn

pip install G4_PreprocessingLib

Usage

  1. Initialize the Class

After importing the library and pandas into a python file, the user must load their dataset by using one of the many pandas commands made for that purpose. After that an instance of the class must be created. In this instance the user can specify the parameters previously mentioned or leave the default ones. The only parameter that is mandatory to provide is the dataset here defined as df.

import G4_PreprocessingLib
import pandas as pd

# Load your dataset
df = pd.read_csv("your_dataset.csv")

# Create an instance of the class
data = DataStructure(df,65,'mode','mean',3)
  1. Clean the Dataset

The next step is to apply the CleanData method to do a preprocessing procedure on the provided dataset with the parameters set by the user or, in the absence of them, the default parameters.

data.CleanData()  # Cleans the DataFrame based on the provided parameters
  1. Encode and Decode Columns

After cleaning the dataset additional procedures like encoding acertain object type columns.

data.EncodeColumn(1,'Y')  # Encodes the column at index 1 and generates the encoded column at the end
  1. Drop a Column

Additional columns can be eliminated with the DropColumn method.

data.DropColumn(2)  # Removes the column at index 2
  1. Homogenize the categorical Data
data.HomogenizeData()  # Standardizes all object type columns

Parameters

  • df: The pandas DataFrame to process.
  • lim_nan: Threshold (in percentage) for dropping columns based on NaN values. Default is 65%.
  • m_obj: Strategy for imputing missing values in object-type columns. Choices are:
    • 'mode' (default)
    • 'bfill' (fills the column with the last value)
    • 'ffill' (fills the column with the first value)
  • m_num: Strategy for imputing missing values in numerical columns. Choices are:
    • 'mean' (default)
    • 'median'
    • 'mode'
    • 'bfill' (fills the column with the last value)
    • 'ffill' (fills the column with the first value)
    • 'max'
    • 'min'
  • threshold: Limit to determine if a value is an outlier.

Bare in mind that when using either 'bfill' or 'ffill' options, it will take the first non-NaN value that they find in the column an fill it with it starting from that value's position. Meaning that, if one of these methods is chosen and the last or first value, respectively, of the column is a NaN value, the program will not impute it.

Error Handling

This library has taken into account possible mistakes that the user may make, so different error handling methods have been applied.

  • InvalidMethodError: raised for invalid imputation methods in m_obj or m_num.
  • InvalidParameterError: raised if lim_nan is not a numeric value.
  • IndexError: raised for out-of-range column indices in methods like DropColumn.
  • ValueError: raised when a function receives an argument of the correct type but with an inappropriate or invalid value.
  • KeyError: raised when a key (or column name in this context) is not found or conflicts with existing keys in a dataFrame.

Example

import G4_PreprocessingLib as Plib
import pandas as pd

# Create a sample dataset
data_dict = {
    'Name': ['Alice', 'Bob', None, 'David'],
    'Age': [25, None, 23, 35],
    'Salary': [50000, 60000, None, 45000]
}

df = pd.DataFrame(data_dict)

# Initialize the DataStructure class
data = Plib.DataStructure(df,50,'mode','median',4)

# Clean the dataset
data.CleanData()

# Encode a column
data.EncodeColumn(0)

# Homogenize string data
data.HomogenizeData()

# View the cleaned DataFrame
print(data.df)

License

This project is licensed under the MIT License. Feel free to use and modify it as needed.

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

g4_lib-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

G4_Lib-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file g4_lib-0.1.1.tar.gz.

File metadata

  • Download URL: g4_lib-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for g4_lib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2bee88f78e074c2e6ddd665d3ffbe6dd2058d90cc8fa948f8a052e041c6693e4
MD5 524b7f1f6dd71ac5157ac466244bcf6c
BLAKE2b-256 4129e85fa7c0a8ddfe61117e221cf76f534bff7aeb21533c1d4b2ad9b7809465

See more details on using hashes here.

File details

Details for the file G4_Lib-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: G4_Lib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for G4_Lib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7294a11db8855b3295b3a0e8eb3f5d9fc8b269bbb21d1072e8dd3edf4efaed9b
MD5 5fb750b5b286a444245b1cb0247280a6
BLAKE2b-256 69365c3464a5895374482594536b650c069f59c3756c4836be6cf2afb90bba5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page