Return Slope, Intercept and r from scipy.stats.linregress

Quick Overview

Fit a simple linear regression with scipy.stats.linregress and return the slope, intercept and Pearson correlation as plain floats. It tests reading a SciPy result object correctly and knowing what each fitted number means, how it relates to R-squared, and which inputs break the fit.

Return Slope, Intercept and r from scipy.stats.linregress

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def linregress_summary(x: np.ndarray, y: np.ndarray) -> tuple[float, float, float]` that fits `y` on `x` with `scipy.stats.linregress(x, y)` and returns `(slope, intercept, rvalue)`. `rvalue` is the Pearson correlation coefficient. Requirements: - Call `scipy.stats.linregress(x, y)` exactly once. - Read `res.slope`, `res.intercept` and `res.rvalue` as attributes, with no tuple unpacking or indexing. - Return them as built-in `float` values in that order. For example: ```text Input: x = [1.0, 2.0, 3.0, 4.0, 5.0], y = [2.0, 4.0, 5.0, 4.0, 5.0] Output: (0.6, 2.2, 0.774597) # rvalue rounded to six decimals ``` Here the means are 3 and 4, the sum of squared deviations of `x` is 10, and the sum of cross products is 6, so the slope is 0.6 and the intercept is `4 - 0.6 * 3 = 2.2`. The sum of squared deviations of `y` is 6, so `rvalue = 6 / sqrt(10 * 6)`, which is about 0.774597. ```hint Read the result object Check what `linregress` returns and which of its fields you can reach by name. Then look at the type of each value you hand back. ``` ### Constraints & Assumptions - `x` and `y` are fixed 1-D float arrays of shape `(n,)` with an imperfect but clearly positive relationship. - Results are compared with `numpy.testing.assert_allclose`. ### What a Strong Answer Covers - A single `linregress` call with the three fields read by name and returned in the required order. - Conversion of NumPy scalars to built-in floats. - What the slope, intercept and `rvalue` are in terms of means, variances and covariance, and how `rvalue` relates to R-squared in simple regression. - Inputs that break the fit, such as a constant `x` or missing values. ### Follow-up Questions - How would you also return R-squared and the slope's standard error? - When would you switch from `linregress` to `np.polyfit` or `statsmodels`? - What null hypothesis does `res.pvalue` test, and what are its degrees of freedom?

Overview: Fit a simple linear regression with scipy.stats.linregress and return the slope, intercept and Pearson correlation as plain floats. It tests reading a SciPy result object correctly and knowing what each fitted number means, how it relates to R-squared, and which inputs break the fit.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def linregress_summary(x: np.ndarray, y: np.ndarray) -> tuple[float, float, float] that fits y on x with scipy.stats.linregress(x, y) and returns (slope, intercept, rvalue). rvalue is the Pearson correlation coefficient.

Requirements:

  • Call scipy.stats.linregress(x, y) exactly once.
  • Read res.slope , res.intercept and res.rvalue as attributes, with no tuple unpacking or indexing.
  • Return them as built-in float values in that order.

For example:

Input:  x = [1.0, 2.0, 3.0, 4.0, 5.0], y = [2.0, 4.0, 5.0, 4.0, 5.0]
Output: (0.6, 2.2, 0.774597)   # rvalue rounded to six decimals

Here the means are 3 and 4, the sum of squared deviations of x is 10, and the sum of cross products is 6, so the slope is 0.6 and the intercept is 4 - 0.6 * 3 = 2.2. The sum of squared deviations of y is 6, so rvalue = 6 / sqrt(10 * 6), which is about 0.774597.

Constraints & Assumptions

  • x and y are fixed 1-D float arrays of shape (n,) with an imperfect but clearly positive relationship.
  • Results are compared with numpy.testing.assert_allclose .

What a Strong Answer Covers Guidance

  • A single linregress call with the three fields read by name and returned in the required order.
  • Conversion of NumPy scalars to built-in floats.
  • What the slope, intercept and rvalue are in terms of means, variances and covariance, and how rvalue relates to R-squared in simple regression.
  • Inputs that break the fit, such as a constant x or missing values.

Follow-up Questions Guidance

  • How would you also return R-squared and the slope's standard error?
  • When would you switch from linregress to np.polyfit or statsmodels ?
  • What null hypothesis does res.pvalue test, and what are its degrees of freedom?