benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 7b01539d-779c-5915-aef8-cbd25d1e1c6a

Problem

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

complete prompt

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression


def task_func(df, target_column, target_values=None):
    """
    Replace all elements in DataFrame columns that are not present in the target_values array with zeros, and then perform a linear regression using the target column.

    Parameters:
        df (DataFrame): The input pandas DataFrame.
        target_column (str): The target column for the linear regression.
        target_values (array-like, optional): An array of target values to keep in the DataFrame. 
        All other values will be replaced with zeros. Defaults to None.


    Returns:
        LinearRegression: The trained Linear Regression model.

    Raises:
        ValueError: If df is not a DataFrame or if target_column is not a string or if target_values is not an array-like object

    Requirements:
        - numpy
        - pandas
        - sklearn.linear_model.LinearRegression

    Example:
        >>> rng = np.random.default_rng(seed=0)
        >>> df = pd.DataFrame(rng.integers(0, 100, size=(1000, 2)), columns=['A', 'predict'])
        >>> model = task_func(df, 'predict')
        >>> print(model.coef_)
        [-0.04934205]
        >>> print(model.intercept_)  
        53.67665840020308

        >>> rng = np.random.default_rng(seed=0)
        >>> df = pd.DataFrame(rng.integers(0, 100, size=(1000, 5)), columns=['A', 'B', 'C', 'D', 'predict'])
        >>> model = task_func(df, 'predict')
        >>> print(model.coef_)
        [-0.00173703 -0.02190392 -0.03304266  0.00759771]
        >>> print(model.intercept_)
        53.362739257681035
    """

instruct prompt

Replace all elements in DataFrame columns that are not present in the target_values array with zeros, and then perform a linear regression using the target column. >>> rng = np.random.default_rng(seed=0) >>> df = pd.DataFrame(rng.integers(0, 100, size=(1000, 5)), columns=['A', 'B', 'C', 'D', 'predict']) >>> model = task_func(df, 'predict') >>> print(model.coef_) [-0.00173703 -0.02190392 -0.03304266  0.00759771] >>> print(model.intercept_) 53.362739257681035
The function should raise the exception for: ValueError: If df is not a DataFrame or if target_column is not a string or if target_values is not an array-like object
The function should output with:
    LinearRegression: The trained Linear Regression model.
You should write self-contained code starting with:

Code

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
def task_func(df, target_column, target_values=None):

code prompt

Code

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
def task_func(df, target_column, target_values=None):

entry point

task_func

libs

  • pandas
  • numpy
  • 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