gh-149244: Support iterator inputs in covariance, correlation, and linear_regression#149245
Closed
htjworld wants to merge 1 commit intopython:mainfrom
Closed
gh-149244: Support iterator inputs in covariance, correlation, and linear_regression#149245htjworld wants to merge 1 commit intopython:mainfrom
htjworld wants to merge 1 commit intopython:mainfrom
Conversation
…and linear_regression
Documentation build overview
|
Contributor
Author
|
Hi @rhettinger, could you share your thinking on closing this? I'd genuinely like to understand — whether it's the approach, a design decision I missed, or something else entirely. |
Contributor
|
It is fine for these functions to just support sequences. Also, running |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #149244.
statistics.covariance(),statistics.correlation(), andstatistics.linear_regression()previously calledlen()directly on theirinputs, raising
TypeErrorfor iterators and generators. This was inconsistentwith the rest of the module —
mean(),variance(), andstdev()all acceptany iterable.
The fix adds
x = list(x)andy = list(y)at the start of each function.This is in line with the internal
_ss()helper, whose docstring states"Calculations are done in a single pass, allowing the input to be an iterator."
The list conversion also correctly handles repeated iteration:
covariance()iterates each input twice (fsum()thensumprod()), andlinear_regression()already notes in a comment thatxmust be a list"because used three times below."
Tests for
iter()and generator expression inputs are added toTestCorrelationAndCovarianceandTestLinearRegression. The documentationfor all three functions is updated to reflect that sequences or iterables are
accepted.