Module3_Ch1
Module3_Ch1
import numpy as np
import matplotlib as plt
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
plt.plot(X, y, "b.")
Set θ=1
X_b = np.c_[np.ones((100, 1)), X] # add x0 = 1 to each instance
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
• The LinearRegression class is based on the scipy.linalg.lstsq() function (the name stands
for “least squares”)
• The pseudoinverse itself is computed using a standard matrix factorization technique
called Singular Value Decomposition (SVD) that can decompose the training set matrix X
into the matrix multiplication of three matrices U Σ VT
Computational Complexity
• The Normal Equation computes the inverse of XT X, which is an (n + 1) × (n + 1)
matrix.
• from sklearn.linear_model
• import LinearRegression
• lin_reg = LinearRegression()
• lin_reg.fit(X_poly, y)
• lin_reg.intercept_, lin_reg.coef_ #a0,a1,a2 y = ax2 + bx + c.
• yhat = 0 . 56x1 2 + 0 . 93x1 + 1 . 78
• Original y = 0 . 5x1 2 + 1 . 0x1 + 2 . 0 + Gaussian noise
log_reg.predict([[1.7], [1.5]])
Softmax Regression
• The Logistic Regression model can be generalized to support multiple
classes directly, without having to train and combine multiple binary
classifiers.
• Softmax score function
Predictor
In scikit learn
• X = iris["data"][:, (2, 3)] # petal length, petal
• widthy = iris["target"]
• softmax_reg = LogisticRegression(multi_class="multinomial",solver="lbfgs",
C=10)
• softmax_reg.fit(X, y)
• softmax_reg.predict([[5, 2]])
• softmax_reg.predict_proba([[5, 2]])