benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 4dda2536-7ca6-508b-b151-4727d8f39296

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 StandardScaler



# Constants
FEATURES = ['feature1', 'feature2', 'feature3', 'feature4', 'feature5']
TARGET = 'target'

def task_func(df, dict_mapping, plot_histogram=False):
    """
    Pre-processes a DataFrame by replacing values according to a dictionary mapping, standardizing specified features, 
    and optionally drawing a histogram of the target variable.

    Parameters:
    - df (DataFrame): The input DataFrame to be preprocessed. It should contain columns named as in FEATURES and TARGET.
    - dict_mapping (dict): A dictionary for replacing values in df. The keys should correspond to existing values in df.
    - plot_histogram (bool, optional): If True, a histogram of the target variable is displayed. Default is False.

    Returns:
    - DataFrame: The preprocessed DataFrame with standardized features and values replaced as per dict_mapping.
    - Axes: The histogram of the target variable if plot_histogram is True, otherwise None.

    Raises:
    - The function will raise ValueError if the FEATURES and TARGET columns not in the input DataFrame.
    - The function will raise ValueError if the input df is not a DataFrame.

    Requirements:
    - pandas
    - sklearn.preprocessing.StandardScaler

    Example:
    >>> df = pd.DataFrame({'feature1': [1, 2, 3], 'feature2': [4, 5, 6], 'feature3': [7, 8, 9],'feature4': [10, 11, 12], 'feature5': [13, 14, 15], 'target': [0, 1, 1]})
    >>> dict_mapping = {1: 11, 0: 22}
    >>> isinstance(task_func(df, dict_mapping, plot_histogram=True)[1], plt.Axes)
    True
    >>> plt.close()
    """

instruct prompt

Pre-processes a DataFrame by replacing values according to a dictionary mapping, standardizing specified features, and optionally drawing a histogram of the target variable.
The function should raise the exception for: The function will raise ValueError if the FEATURES and TARGET columns not in the input DataFrame. The function will raise ValueError if the input df is not a DataFrame.
The function should output with:
    DataFrame: The preprocessed DataFrame with standardized features and values replaced as per dict_mapping.
    Axes: The histogram of the target variable if plot_histogram is True, otherwise None.
You should write self-contained code starting with:

Code

import pandas as pd
from sklearn.preprocessing import StandardScaler
# Constants
FEATURES = ['feature1', 'feature2', 'feature3', 'feature4', 'feature5']
TARGET = 'target'
def task_func(df, dict_mapping, plot_histogram=False):

code prompt

Code

import pandas as pd
from sklearn.preprocessing import StandardScaler
# Constants
FEATURES = ['feature1', 'feature2', 'feature3', 'feature4', 'feature5']
TARGET = 'target'
def task_func(df, dict_mapping, plot_histogram=False):

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