15-388/688 - Practical Data Science: Intro to Machine Learning & Linear Regression
- J. Zico Kolter
Carnegie Mellon University Fall 2019
1
15-388/688 - Practical Data Science: Intro to Machine Learning & - - PowerPoint PPT Presentation
15-388/688 - Practical Data Science: Intro to Machine Learning & Linear Regression J. Zico Kolter Carnegie Mellon University Fall 2019 1 Announcements HW 3 released, due 10/24 (not 10/17) Feedback on tutorial sent to everyone who
1
2
3
4
5
Da Date te Hi High Tempera rature (F) Peak k Demand (GW) 2011-06-01 84.0 2.651 2011-06-02 73.0 2.081 2011-06-03 75.2 1.844 2011-06-04 84.9 1.959 … … …
60 70 80 90 High Temperature (F) 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW)
6
푖∈days
2
7
푖∈days
2
푖∈days
2
8
푖∈days
2
푖∈days
2
푖∈days
푖∈days
푖∈days
9
푖∈days
푖∈days
10
60 70 80 90 High Temperature (F) 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW)
11
−4 −2 2 Normalized Temperature 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW)
12
−4 −2 2 Normalized Temperature 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW) θ = (0.00, 0.00) E(θ) = 1427.53 ( ∂E(θ)
∂θ1 , ∂E(θ) ∂θ2 ) = (−151.20, −1243.10)
Observed days Squared loss fit
13
−4 −2 2 Normalized Temperature 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW) θ = (0.15, 1.24) E(θ) = 292.18 ( ∂E(θ)
∂θ1 , ∂E(θ) ∂θ2 ) = (−67.74, −556.91)
Observed days Squared loss fit
14
−4 −2 2 Normalized Temperature 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW) θ = (0.22, 1.80) E(θ) = 64.31 ( ∂E(θ)
∂θ1 , ∂E(θ) ∂θ2 ) = (−30.35, −249.50)
Observed days Squared loss fit
15
−4 −2 2 Normalized Temperature 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW) θ = (0.25, 2.05) E(θ) = 18.58 ( ∂E(θ)
∂θ1 , ∂E(θ) ∂θ2 ) = (−13.60, −111.77)
Observed days Squared loss fit
16
−4 −2 2 Normalized Temperature 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW) θ = (0.26, 2.16) E(θ) = 9.40 ( ∂E(θ)
∂θ1 , ∂E(θ) ∂θ2 ) = (−6.09, −50.07)
Observed days Squared loss fit
17
−4 −2 2 Normalized Temperature 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW) θ = (0.27, 2.25) E(θ) = 7.09 ( ∂E(θ)
∂θ1 , ∂E(θ) ∂θ2 ) = (−0.11, −0.90)
Observed days Squared loss fit
18
50 60 70 80 90 100 High Temperature (F) 1.50 1.75 2.00 2.25 2.50 2.75 3.00 Peak Demand (GW) Observed days Squared loss fit
19
20
22
23
24
푗=1 푛
25
휃
푖=1 푚
26
27
28
푛
휃
푖=1 푚
푗=1 푛
푖 − 𝑧 푖 2
휃
29
푖=1 푚
푗=1 푛
푖 − 𝑧 푖 2
푖=1 푚
푗=1 푛
푖 − 𝑧 푖 2
푖=1 푚
푗=1 푛
푖 − 𝑧 푖
푗=1 푛
푖
푖=1 푚
푗=1 푛
푖 − 𝑧 푖
푖
30
푖=1 푚
푗=1 푛
푖 − 𝑧 푖
푖
31
32
33
푖=1 푚
푗=1 푛
푖 − 𝑧 푖
푖
푖=1 푚
34
푖=1 푚
푖=1 푚
푖=1 푚
푖=1 푚
−1
푖=1 푚
35
푖=1 푚
36
37
38
39
40
41
# initialize X matrix and y vector X = np.array([df["Temp"], df["IsWeekday"], np.ones(len(df))]).T y = df_summer["Load"].values # solve least squares theta = np.linalg.solve(X.T @ X, X.T @ y) print(theta) # [ 0.04747948 0.22462824 -1.80260016] # predict on new data Xnew = np.array([[77, 1, 1], [80, 0, 1]]) ypred = Xnew @ theta print(ypred) # [ 2.07794778 1.99575797]
42
43
from sklearn.linear_model import LinearRegression # don't include constant term in X X = np.array([df_summer["Temp"], df_summer["IsWeekday"]]).T model = LinearRegression(fit_intercept=True, normalize=False) model.fit(X, y) # predict on new data Xnew = np.array([[77, 1], [80, 0]]) model.predict(Xnew) # [ 2.07794778 1.99575797] print(model.coef_, model.intercept_) # [ 0.04747948 0.22462824] -1.80260016
44
class MyLinearRegression: def __init__(self, fit_intercept=True): self.fit_intercept = fit_intercept def fit(self, X, y): if self.fit_intercept: X = np.hstack([X, np.ones((X.shape[0],1))]) self.coef_ = np.linalg.solve(X.T @ X, X.T @ y) if self.fit_intercept: self.intercept_ = self.coef_[-1] self.coef_ = self.coef_[:-1] def predict(self, X): pred = X @ self.coef_ if self.fit_intercept: pred += self.intercept_ return pred