Multivariate Linear Regression

Multivariate Linear Regression

Multiple Features

Multivariate linear regression: Linear regression with mutiple variables.

Notation

n n n= number of features

x ( i ) x^{(i)} x(i)= input (features) of i t h i^{th} ith training example.

x j i x_j^{i} xji= value of feature j j j in i t h i^{th} ith training example.

h θ ( x ) h_\theta(x) hθ(x)= θ 0 + θ 1 x 1 + . . . θ n x n \theta_0+\theta_1x_1+...\theta_nx_n θ0+θ1x1+...θnxn

For convenience of notation, define x 0 x_0 x0=1.(That is, x 0 ( i ) x_0^{(i)} x0(i)=1)
x = [ x 0 x 1 ⋯ x n ] ∈ R n + 1 x=\begin{bmatrix} x_0\\x_1\\\cdots\\x_n \end{bmatrix}\in\R^{n+1} x=x0x1xnRn+1

θ = [ θ 0 θ 1 ⋯ θ n ] ∈ R n + 1 \theta=\begin{bmatrix} \theta_0\\\theta_1\\\cdots\\\theta_n\end{bmatrix}\in\R^{n+1} θ=θ0θ1θnRn+1

h θ ( x ) h_\theta(x) hθ(x)= θ T x \theta^Tx θTx

Gradient Descent for Multiple Variable

The gradient descent equation is generally the same form, we just have to repeat it for ‘n’ features:

在这里插入图片描述

Repeat until convergence:{

θ j \theta_j θj:= θ j \theta_j θj- α 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) ⋅ x j ( i ) \alpha\frac{1}{m}\sum^m_{i=1}{(h_\theta(x^{(i)} )-y^{(i)})\cdot x_j^{(i)} } αm1i=1m(hθ(x(i))y(i))xj(i)

}

Gradient Descent in Practice I- Feature Scaling

Idea: Make sure features are on similar scale.

Modify the ranges of our input variables: Speed up gradient descent by having each of input values in roughly the same range.(make the contour of cost function J J J can become less skewed)

Because θ \theta θ will descend quickly on small ranges and slowly on large ranges. This will oscillate inefficiently down to the optimum when the variables are very uneven.

No exact requirements.

Two techniques:

  • feature scaling :Dividing the input values by the range of the input variables

    Get every feature into approximately a − 1 ≤ x i ≤ 1 -1\leq x_i\leq1 1xi1 range.

  • mean normalization: Subtracting the average value for an input variable from the values for that input variable.

    Replace x i x_i xi with x i − μ i x_i-\mu_i xiμi to make features have approximately zero mean (Do not apply to x 0 = 1 x_0=1 x0=1)

Adjust input values in the formula:

x i : = x i − μ i s i x_i :=\frac{x_i-\mu_i}{s_i} xi:=sixiμi

μ \mu μ is the average of all the values for features(i) and s i s_i si is the range of values (max-min), or s i s_i si is the standard deviation.

  • Dividing by the range, or dividing by the standard deviation, give different results.

Gradient Descent in Practice II- Learning Rate

  • Debugging gradient descent

Make a plot with number of iterations on the x-axis. Now plot the cost function, J(θ) over the number of iterations of gradient descent. If J( θ \theta θ) ever increases, then we need to increase α \alpha α.

( J θ J_\theta Jθ should decrease after every iteration)

  • Automatic convergence test

Declare convergence if J(θ) decreases by less than E in one iteration, where E is some small value such as 1 0 − 3 10^{−3} 103.

If learning rate α is sufficiently small, then J(θ) will decrease on every iteration.

If α is too small: slow convergence.

If α is too large: may not decrease on every iteration and thus may not converge.

Features and Polynomial Regression

Improve features and the form of hypothesis

  • combine multiple features into one

Polynomial Regression

  • change the behavior or curve

(making it a quadratic, cubic or square root function (or any other form))

Keep in mind that, if you choose your features this way then feature scaling becomes very important.

多元回归(Multivariate Regression)是一种统计模型,它适用于预测一个因变量如何依赖于两个或更多的自变量。其基本原理是基于线性假设,即因变量与每个自变量之间的关系可以用一条直线来近似描述,且各自变量之间相互独立影响。 流程主要包括以下几个步骤: 1. **数据准备**:收集包含因变量(目标变量)和多个自变量的数据集,并确保数据质量,如清洗缺失值、异常值,归一化或标准化数值特征。 2. **模型设定**:在Python中,可以使用诸如`sklearn.linear_model.LinearRegression`这样的库来创建多元线性回归模型。需要指定因变量(通常是y列)和自变量(通常是X列)。 ```python from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # 创建并拟合模型 model = LinearRegression() model.fit(X_train, y_train) ``` 3. **模型评估**:训练完成后,使用交叉验证(如`cross_val_score`)或在测试集上评估模型性能,如计算均方误差(MSE)、R²分数等。 4. **参数理解**:查看模型系数(coef_)和截距(intercept_),它们分别代表了各个自变量对因变量的影响程度以及截距项。 5. **预测**:利用模型的`predict`方法对新数据进行预测。 6. **模型优化**:可能的话,调整模型参数,如正则化(L1或L2惩罚)来防止过拟合,或尝试其他回归模型(如岭回归或弹性网络)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值