Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([3, 5, 7, 9])
w = ((x - x.mean()) * (y - y.mean())).sum() / ((x - x.mean()) ** 2).sum()
b = y.mean() - w * x.mean()
print(w, b)2.0 1.0
- Expecting
2.0 6.0: the intercept isy.mean() - w * x.mean(), so the slope times the mean ofxis subtracted:6.0 - 2.0 * 2.5 = 1.0. - Expecting
2 1becausexandyhold integers:.mean()always returns a float, sowandbare floats and print as2.0and1.0.
- What would
wandbbecome if every value ofywere shifted up by 10? - Why does this slope formula break when every value of
xis the same?