Machine learning three lines (series of six) ---- Logistic regression and Softmax combat analysis (with code)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/x454045816/article/details/79009748
Original link: Read the original text
Welcome to the concern of public micro-channel number "smart algorithm", we learn together and progress together.
For the previous series, please reply "in the number of public machine learning " to view!
The last series we systematically study the normal equation method for solving linear regression parameters, as well as three common gradient descent. Then how to explain complex nonlinear polynomial regression for data, the last models studied three common methods to reduce the regularization of over-fitting model, details stamp the following link:
No public concern "smart algorithm" we can learn a whole series of articles together
In this paper, practical application and Logistic regression softmax return on iris data sets, through the article, I hope that we can grasp the knowledge of this aspect together. Welcome to the end of the text to view the downloaded keywords, number of public responses to combat free download code.

1. Logistic regression
We mentioned in a series of before, some of the regression algorithm can be used for classification, as well as some of the classification algorithm can predict regression, Logistic regression is such an algorithm. General Logistic regression by estimating a probability value to indicate a probability sample belongs to a particular class. If the probability of a sample belonging to a class of more than 50%, then it is judged that the sample belongs to this category. So Logistic is how to estimate the probability of it?
1.1 How to classify
Logistic estimate is similar to the Fifth series we are talking about a linear regression on a probability sample, but also the right to a series of calculated weight of the sample, and then enter the weight and linear acceleration after the sigmoid function, then calculate a probability value. The following formula:
Wherein θ is the vector of weights mentioned above, the function [sigma] is the sigmoid function, as follows:
sigmoid函数是怎么能够输出一个概率的呢?我们来看一下sigmoid函数长什么样子就明白是怎么回事了。通过代码我们画出了sigmoid曲线,如下图:
通过上图,我们可以很easy的看出,该函数的定义域是(-∞,+∞ ),而值域却是(0,1),所以我们可以根据计算出来的概率值来进行对样本进行分类:
既然我们知道了Logistic回归的工作原理,那么该模型是怎么训练出来的呢?
1.2 模型训练和损失函数
我们既然是通过sigmoid函数的值来进行概率预测的,那么我们的目标就应该是找出一组权重参数θ ,能够对于正样本使得sigmoid函数有一个高的输出值,而对于负样本有一个低的输出。我们可以通过计算损失函数来逐步达到这一的目标。对于单个样本来说,损失函数如下公式:
从上面公式可以看出,当t接近于0的时候-log(t)将会变得很大,也就是说,当一个正样本被判为负样本的时候,损失函数的值将会很大,反之一样,当一个负样本被预测为正样本的时候该损失函数也会很大,只有正样本预测为正样本和负样本判为负样本的时候,损失函数才很小,甚至没有损失。
既然我们知道单个样本的损失函数了,那么将之应用到整个数据集中,也就是说整个数据集的损失函数如下:
很遗憾,要使得上面的损失函数值最小并没有一个闭式解能进行优化θ,但是由于该函数是凸的,所以我们可以很easy的用上个系列中所介绍的三种梯度下降法来进行寻优(详情见系列五)。
求出损失函数的最小值之后,我们就可以根据上一小结中的方法来将权重参数和新样本进行加权代入sigmoid函数中进行概率值求解。通过上面的理论学习,我们基本上明白logistic的工作原理,但是究竟该如何用呢?
1.3 实战以及决策边界
这里我们用总所周知的iris数据集来进行算法的实战
iris的数据集包括三个类别的样本,分别是Setosa, Versicolor, Virginica,每个样本有四个特征分别是花萼的长宽以及花瓣的长宽。如下:
我们先来一个简单的练习,因为该数据集中有多个类别,而我们的Logistic回归只能进行二分类,所以,我们先通过花瓣的宽度进行判断一朵花是否为Virginica, 如下:
我们一起看一下,模型在花瓣宽度为0到3cm的范围内概率估计的效果如何?由于下图代码比较长,所以请移步文末查看代码关键字,然后公众号回复关键字进行代码下载。
上图Virginica花(以三角形表示)的花瓣宽度范围从1.4cm到2.5cm,而非Virginica花(以正方形表示)的花瓣宽度范围从0.1cm到1.8cm。我们从上图很清晰的看到两个类别在花瓣宽度这个特征上有一些重叠区域。从上面的图中能清楚的看到在大于2cm的时候,分类器对Virginica有一个高的概率值,而在小于1cm的时候,对非Virginica有一个高的预测值。而对于。1.6cm附近的竖直虚线则称为分类器的决策边界,也就是说,当花瓣宽度大于1.6cm的时候,分类器将样本分为Virginica,小于1.6cm的时候分为非Virginica。
上面的图片显示的是一维特征进行分类,接下来我们更进一步,通过显示两个特征来进行查看这个决策边界是什么样的,这里的特征选花瓣的长度和宽度,一旦模型训练成功,分类器将在这两个特征上进行概率估计。
如上图(如何画出来的,请查看代码),中间的虚线是模型的决策边界,图中的平行线表示具体的预测概率值,比方说,在最右上方的平行线以上的样本预测的概率值为大于0.9。当然和其他线性模型一样,我们也可以对Logistic进行正则化(正则化相关知识详情见系列五)。
上面我们用的数据集是有三个类别,然而我们的logistic却只能处理二分类的问题,对多分类情况,logistic该如何处理呢?

2. Softmax回归
2.1 softmax分类原理
softmax其实是Logistic的推广到多类别分类应用中,不必像系列四中所学的建立多个二分类分类器来实现多类别分类。softmax分类器的思想很简单,对于一个新的样本,softmax回归模型对于每一类都先计算出一个分数,然后通过softmax函数得出一个概率值,根据最终的概率值来确定属于哪一类。
对于每一类都有一个权重参数θ,根据下式子计算样本的分数:
通过下公式来计算并归一化之后就是输出的概率值:
其中上公式中k为类别的个数,s(x)为样本在每一类上的分数,σ函数为softmax函数。类似于logistic回归,softmax也是从得到的各个概率概率值中选择最大的一个概率类别。如下公式:
现在我们知道了softmax回归的原理,那么这个模型是如何训练的呢?
和logistic回归一样,我们需要对所属类别有一个较高的概率输出,而对于其他类别的概率输出则较低,同样,我们通过损失函数来进行求取权重参数。如下:
该损失函数又称为交叉熵, 交叉熵常在机器学习中作为损失函数,比方说p表示真实类别的分布,q则为训练后的模型的预测类别分布,交叉熵损失函数可以衡量p与q的相似性。上式中i表示第i个样本,而k表示第k个类别,当样本的类别为k的时候y=1,其他时候y=0。其实我们可以看到,当k=2的时候,上式交叉熵的损失函数就是上面logistic回归中的损失函数。
同样,我们对于上式交叉熵的寻优依然可以用梯度下降法(详见系列五)。
2.2 softmax实战分类
对于多分类,我们前面也说过,可以用logistic回归像系列四种学的那样,通过OVA的方式,进行多个二分类分类器进行实现,当然这不是我们的目的,我们的目的是直接通过softmax实现多分类,为了可视化,我们还是采取两个特征来实现三分类。直接上代码,如下:
Softmax achieve regression is achieved by setting parameter logistic regression, as described above in FIG.
After the return of our model visualization (see the code to achieve), it can be clearly seen that the decision boundary where and how to classify the samples, as shown below:
Above we analyzed the decision boundary one and two dimensional model, the above chart please own analysis of how the classification decision boundary and, if in doubt, Welcome to the micro-channel group discussions or comments to discuss the article below.

3. Summary
At this point, we learned logistic regression and classification mechanism softmax return, as well as learning from a practical application point of view, to place the article in question, welcome to join the micro-channel discussion groups or message exchange.
(For a better understanding of the relevant knowledge, welcome to join the community of intelligent algorithm, the "smart algorithm" No. send public "community", to join the group and QQ micro-channel algorithm group)
Reply code for this article Keywords: LSC

Guess you like

Origin blog.csdn.net/x454045816/article/details/79009748