↖️ Show all posts

Simple Linear Regression with Python

In my case I had to parse an XML first, I used BeautifulSoup4 for that. To format the data the way I needed, I stumbled across a gorgeous feature of Python’s .zip() function. Of course it lets you do what you expect it to do:

a = [1, 2, 3]
b = ["a", "b", "c"]
zip(a, b)
# >>> [(1, 'a'), (2, 'b'), (3, 'c')]

Here is what kept my code short and concise:

x_y_parameters = [[1, 'a'], [2, 'b'], [3, 'c']]
x, y = zip(*x_y_parameters)
# >>> x
# (1, 2, 3)
# >>> y
# ('a', 'b', 'c')

Now, on to SciPy’s linear regression (at least)! Taken straight from the docs. pip install scipy - you need that package

from scipy import stats
import numpy as np
x = np.random.random(10)
y = np.random.random(10)
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)

Using a sexy list comprehension lets you use all results of the regression in an instance. Just one little thing to watch out is there though. stats.linregress(x, y) may need numpy formatted Lists/Arrays as input - this short note may keep your sanity at level one day.


⬅️ Read previous Read next ➡️