Hướng dẫn calculate r-squared python
I originally posted the benchmarks below with the purpose of recommending
To further take advantage of
Below is my original Answer where I benchmarked various linear regression r^2 methods... The
corrcoef function used in the Question calculates the correlation coefficient,
These were my timeit results from comparing a bunch of methods for 1000 random (x, y) points:
The corrcoef method narrowly beats calculating the r^2 "manually" using numpy methods. It is >5X faster than the polyfit method and ~12X faster than the scipy.linregress. Just to reinforce what
numpy is doing for you, it's 28X faster than pure python. I'm not well-versed in things like numba and pypy, so someone else would have to fill those gaps, but I think this is plenty convincing to me that Here's my benchmarking code. I copy-pasted from a Jupyter Notebook (hard not to call it an IPython Notebook...), so I apologize if anything broke on the way. The %timeit magic command requires IPython.
7/28/21 Benchmark results. (Python 3.7, numpy 1.19, scipy 1.6, statsmodels 0.12) |