benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf e940de93-38d3-5a6e-b368-ba9260c79805

Problem

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

complete prompt

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression


def task_func(num_samples=500, noise_strength=1, random_seed=None, test_size=0.2):
    """
    Generate a dataset with a single feature and a target variable. The target
    is computed from the feature using a linear relation.
    In addition some gaussian noise (random samples from normal distributioin), scaled by
    noise_strength, is added to the target. The dataset is split into training
    and test sets. Then a linear regression model is adjusted to the training
    set and the R-squared score is calculated on the test set.

    Parameters:
    - num_samples (int): The number of samples to generate for the dataset.
                   Defaults to 500
    - noise_strength (float): The strength (magnitude) of the noise that is
                              added to the dataset. Defaults to 1
    - random_seed (int): The seed used in generating the dataset, in performing
                   the train test split and in generating the random noise.
                   Defaults to None
    - test_size (float): The fraction of the test split. Defaults to 0.2

    Returns:
    float: The R-squared score of the fitted model on the test set.
    LinearRegression: The trained linear regression model.

    Raises:
    - ValueError: If test set size is smaller than 2.

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

    Example:
    >>> task_func(num_samples=10, noise_strength=23.5, random_seed=24, test_size=0.3)
    (-0.4892453918038726, LinearRegression())
    >>> task_func(noise_strength=0.1)
    (0.9658328575162494, LinearRegression())
    """

instruct prompt

Generate a dataset with a single feature and a target variable. The target is computed from the feature using a linear relation. In addition some gaussian noise (random samples from normal distributioin), scaled by noise_strength, is added to the target. The dataset is split into training and test sets. Then a linear regression model is adjusted to the training set and the R-squared score is calculated on the test set.
The function should raise the exception for: ValueError: If test set size is smaller than 2.
The function should output with:
    float: The R-squared score of the fitted model on the test set.
    LinearRegression: The trained linear regression model.
You should write self-contained code starting with:

Code

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
def task_func(num_samples=500, noise_strength=1, random_seed=None, test_size=0.2):

code prompt

Code

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
def task_func(num_samples=500, noise_strength=1, random_seed=None, test_size=0.2):

entry point

task_func

libs

  • 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