benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 4f4b3cde-4bd8-5c94-892e-12f060c81a4f

Problem

Answer published by the source. Consult the official source to check your work against its answer.

complete prompt

import pandas as pd
from sklearn.preprocessing import LabelEncoder

def task_func(df, dct, columns=None):
    """
    This function preprocesses a pandas DataFrame by replacing specified values, encoding categorical attributes, 
    and standardizing numerical attributes. It's designed to be flexible for data preprocessing in machine learning tasks.

    Parameters:
    - df (DataFrame): The input DataFrame to be preprocessed.
    - dct (dict): A dictionary for replacing values in the DataFrame. Keys are existing values, and values are new values.
    - columns (list of str, optional): Specific column names to be encoded. If None, all object-type columns in the DataFrame are encoded.

    Returns:
    - DataFrame: The preprocessed DataFrame with encoded categorical attributes and standardized numerical attributes.

    Requirements:
    - pandas
    - sklearn.preprocessing.LabelEncoder

    Example:
    >>> df = pd.DataFrame({'col1': ['a', 'b', 'c'], 'col2': [1, 2, 3]})
    >>> dct = {'a': 'x', 'b': 'y'}
    >>> result = task_func(df, dct)
    >>> result.shape == df.shape
    True
    >>> result['col1'].mean() == 0.0
    True

    Note:
    - The function assumes that the DataFrame and the dictionary are well-formed and relevant to each other.
    - The encoding of categorical columns is done using LabelEncoder, which encodes labels with value between 0 and n_classes-1.
    - Numerical standardization is performed by subtracting the mean and dividing by the standard deviation of each column.

    Raises:
    - The function will raise a ValueError is input df is not a DataFrame.
    """

instruct prompt

This function preprocesses a pandas DataFrame by replacing specified values, encoding categorical attributes, and standardizing numerical attributes. It's designed to be flexible for data preprocessing in machine learning tasks.
Note that: The function assumes that the DataFrame and the dictionary are well-formed and relevant to each other. The encoding of categorical columns is done using LabelEncoder, which encodes labels with value between 0 and n_classes-1. Numerical standardization is performed by subtracting the mean and dividing by the standard deviation of each column.
The function should raise the exception for: The function will raise a ValueError is input df is not a DataFrame.
The function should output with:
    DataFrame: The preprocessed DataFrame with encoded categorical attributes and standardized numerical attributes.
You should write self-contained code starting with:

Code

import pandas as pd
from sklearn.preprocessing import LabelEncoder
def task_func(df, dct, columns=None):

code prompt

Code

import pandas as pd
from sklearn.preprocessing import LabelEncoder
def task_func(df, dct, columns=None):

entry point

task_func

libs

  • pandas
  • sklearn

Discussion

Discussion

No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

Source and history

Official source

initial import