07-- logistic regression

Introduction

We see here logistic regression term, but note that it is not the same as the previous regression for the sake of a certain value; it is actually a classic binary classification algorithm
machine learning algorithm selection: first complex logistic regression then, can simple or with a simple
decision boundary logistic regression: it may be nonlinear

Sigmoid function

Task: to shift from input probability; is a classification task
Here Insert Picture Description
Here Insert Picture Description

Logistic regression to solve

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

real python

Case: the establishment of a logistic regression model to predict whether a student is admitted to the University. Suppose you are the administrator of a university department, wants to determine each applicant's chance of admission based on the results of two examinations. Have you ever application of historical human data, you can use it as a training set of logistic regression. For each training example, you have scores of applicants and admissions decisions two exams. To do this, the establishment of a classification model, based on school test scores to estimate the probability of
Here Insert Picture Description
Here Insert Picture Description
the code above will be able to draw the following scatter plot
Here Insert Picture Description
after the graphics With the above, you need to build a model of the
goal: the establishment of a classifier
set threshold the threshold determination admission results
module to be completed:
Sigmoid: is mapped to the function of the probability
model: return prediction result value
cost: calculated loss according to the parameter
gradient: calculates a gradient direction for each parameter
descent: parameter update
accuracy: accuracy

这里首先定义好一个sigmoid函数:
Here Insert Picture Description
对于上面的sigmoid函数,自变量的取值范围是任意实数,值域为[0,1],当z等于0的时候,值为0.5,当z的值趋近于负无穷的时候,值趋近于0,当z的值趋近于正无穷的的时候,值趋近于1
Here Insert Picture Description
在上面的计算中,传入的值是x和theta,在计算中使用到的是dot(就是进行矩阵的乘法),theta.T表示就是theta的转置。并且在最后return的时候交给了sigmoid函数进行数据的整合
下面使用insert的方法在pdData中间的第0列加上一列内容(列名为Ones,数值为1)
Here Insert Picture Description
因为后面进行公式的输入的时候,需要有theta值,目前暂时不知theta的值,所以,这里可以先使用0进行占位
Here Insert Picture Description
如上,就创建了一个1行3列的数据
Here Insert Picture Description
通过使用values,就能够将DataFrame中间的数据转换为一个ndarray的格式,之后使用shape[1]就能够获取到这个ndarray的列数,如果是shape[0]获取的就是行数,使用数据切分的方式就能够获取到中间对应的自变量X和因变量Y
完成上面的工作之后,就能够开始定义损失函数了
Here Insert Picture Description
首先还是找到对数似然函数,这个函数中间可以分成两个部分,这里使用left和right进行定义,使用np.multiply进行乘法的操作;最后转为求平均损失,使用到了sum的方法进行数据累加
Here Insert Picture Description
在进行正式的梯度下降算法的时候,首先我们需要考虑在一种什么样的情况下我们的梯度下降算法可以停止
1、按照一个迭代次数,当迭代次数到达某一个数值的时候,就停止
2、当损失值达到一个数量的时候,就停止
3, when the gradient changes when the changes are not large, is stopped
as shown below:
Here Insert Picture Description
In addition, when we conducted a data analysis, we first need to perform a data shuffle (disrupted data)
Here Insert Picture Description
as described above, since the intermediate numpy with a shuffle operation method is used for shuffling of operation are as follows:
Here Insert Picture Description
has the above sequence data were disrupted
when performing a gradient descent, more usually we choose the sample, and the learning rate adjustment is small so that such a case would be the final result of convergence, but also increase the modeling time; there is also a prior embodiment, performing a gradient descent, a first data pre-processing, after this treatment re-modeling, convergence is quicker

Guess you like

Origin blog.csdn.net/Escid/article/details/90726731