"Machine Learning (Zhihua)" Note - linear model (1) - basic form convex function, loss of function, linear models, linear regression, w * code implements

First, prior knowledge

1, convex function

  Convex function: For a univariate function F ( X ), if for any T [epsilon] [0,1] satisfy the   F ( TX . 1 + (l- T ) X 2 ) ≤  TF ( X . 1 ) + (l- T ) F ( the X- 2 ).

  Convex function wherein:

    (1) convex function over the secant function curve.

    (2) a convex function has a unique minimum value, the minimum value is a minimum value . It means that the model we find a global optimum, does not fall into local optimum.

                      

         Figure 1.1.1 Figure 1.1.2

  The method determines whether a convex function:

  (1) For a univariate function   F ( X ) , which we can second derivative   F '' ( X ) symbol is determined. If the second order derivative of the function is always non-negative, i.e.,  F '' ( X ) ≧ 0 , then  F ( X ) is a convex function ; for example: F ( X ) = X 2F '' ( X ) = 2. 

  (2) For the multivariate function   F ( X- ) , through which we can Hessian matrix (the second derivative of the multivariate function phalanx) of the positive definiteness judged. If the Hessian matrix is a semi-definite matrix , it is F ( X- ) convex function . For example: F ( X, Y ) = X 2 + Y 2

   Hessian matrix:

                                        

 2, loss of function

  Loss function used to measure the quality of the fit of the model , that is to say when the larger model fitting error function values should be relatively large, should be relatively small and vice versa. Therefore, our goal is to make the minimum loss function value, then the learning algorithm is converted to an optimization problem.

  Linear regression of the loss function: the mean square error function

  Loss of function of logistic regression: a logarithmic loss function

  Can be shown that these are convex function, we found the extreme point loss function, to find the optimal solution. Corresponding to the extreme point is a point derivative is zero.

 3, derivative

   To seek loss function requires the use of derivative knowledge. Here are some commonly used machine learning derivation formula:

                                                         

 

Second, the basic form of a linear model

  给定由 个属性描述的示例  x={xx; ... x} , 线性模型试图学得一个通过属性的线性组合来进行预测的函数:

      f(x) = w1x+ w2x… + wdx+ b

  一般用向量形式进行表示:

      f(x) = wTx + b

  其中 w={ ww; ... ; w}和 学得之后,模型就得以确定。w 称为权值向量,b 称为偏置,如果只有一个属性,例如,由体重预测身高,f(x) = wx + b,w为斜率,b为截距。

  线性模型形式简单,易于建模,具有很好的解释性。例如:y=0.2x色泽+0.5x根蒂+0.3x敲声+1

  

三、线性回归

 1、简单线性回归

  简单线性回归中,一个变量跟另一个变量的变化而变化。

  那么,到底什么是线性回归呢?如青少年的身高与体重,他们存在一种近似的线性关系:身高/cm = 体重/kg +105 。假如我们将青少年的身高和体重值作为坐标,不同人的身高体重就会在平面上构成不同的坐标点,然后用一条直线,尽可能的去拟合这些点,这就是简单的线性回归。

  简单的线性回归模型如下:

    y = wb

  其中 x表示特征值(如:体重值),w表示权重b表示偏置y表示标签(如:身高值)。

2、多元线性回归

  简单线性回归中,一个变量跟另一个变量的变化而变化,但是生活中,还有很多变量,可能由多个变量的变化决定着它的变化,比如房价,影响它的因素可能有:房屋面积、地理位置等等。如果我们要给它们建立出近似的线性关系,这就是多元线性回归。

  多元线性回归模型如下

      y=w1xw2x2​ +...wnxn
  其中 xi​ 表示第 i个特征值wi​ 表示第 i个特征对应的权重b表示偏置y表示标签

 3、线性回归一般形式

   给定数据集 D = { (x1,y1) , (x2,y2) , ... ,(xm,ym) }, x= { xixi; ... xid },y是连续的量(实数),线性回归试图学得一个线性模型以尽可能准确预测实值输出标记。即

                                  

  其中     ,为增广向量形式。

                         

  其中 ŵ=(b;w) ,数据集 表示为一个 m*(d+1) 增广矩阵 X

  损失函数:均方误差,用于衡量真实值与预测值之间的差异,公式如下:

                 

                   

    令      θ求导得到:

             

  当XTX满秩:   

  其中,X为样本数据构成的× (d+1) 的矩阵,ym维标签列向量,上式即维线性回归的正规方程解。然而,现实任务中 XT是不满秩的,这时就会有多个 w*。

  说明:给定一个包含m个样本的数据集D,xi为第i个样本,每个样本包含d个属性,是一个d维的列向量,yi为第i个样本的标签,在回归任务中,yi是连续的实数。当权值向量w以及偏置确定后,模型就可以确定,这时我们将样本数据xi代入到模型中,就可以可到一个预测值y_hat,可以写成向量形式,其中是增广向量形式,在向量的最左端增加了一个常量1,变成了一个d+1维的行向量,这里1可以看成偏置b的系数,m个样本代入到模型中,可以得到m个预测值,将这些预测值用向量的形式表示为,每一行代表一个样本。w【0】对应的是偏置。

  回归的目标是使得预测值与真实值之间的差异尽量小,这里使用均方误差来衡量二者之间的差异,记作loss,称为损失函数或代价函数。y所有样本的真实值构成的列向量,y_hat为所有样本的预测值构成列向量。我们的目标转化为求loss最小时权值向量w,导数等于0的点对应极值点。

4、loss最小时权值向量w——w* 的代码实现

          XTX满秩时,  

  1、将样本数据转换为增广矩阵形式:

      import nump as np

      ones=np.ones((X.shape[0],1))

      np.hstack((ones,X))

  2、矩阵转置:

      XT=X.T

  3、矩阵相乘:

      XTX=np.dot(XT,X)

  4、矩阵的逆:

      np.linalg.inv(XTX)

 

 

 

Guess you like

Origin www.cnblogs.com/lsm-boke/p/12242352.html