吴恩达机器学习ex2 Logistic Regression (python)(上)

简介: 吴恩达机器学习ex2 Logistic Regression (python)

Introduction


In this exercise, you will implement logistic regression and apply it to two different datasets. Before starting on the programming exercise, we strongly recommend watching the video lectures and completing the review questions for the associated topics.


1 Logistic regression


   In this part of the exercise, you will build a logistic regression model to predict whether a student gets admitted into a university.


   Suppose that you are the administrator of a university department and you want to determine each applicant’s chance of admission based on their results on two exams. You have historical data from previous applicants that you can use as a training set for logistic regression. For each training example, you have the applicant’s scores on two exams and the admissions decision.


   Your task is to build a classication model that estimates an applicant’s probability of admission based the scores from those two exams.


简单来说,在这个练习中,我们需要建立一个逻辑回归模型去预测一个学生是否能被大学录取。现在假设你是一所大学的管理者,并且你可以根据每个申请人的两门成绩去决定他们是否被录取。你还有以前申请人的历史数据,你可以将其作为逻辑回归模型的训练集,对于每个例子,您都有申请人在两项考试中的分数和录取结果。

现在你的任务就是建立一个基于两项考试的分数的分类模型去评估申请人被录取的可能性。


1.1 Visualizing the data


建议无论打算用什么算法,如果可能的话,都最好将数据可视化,有时候数据可视化以后,你能更加清晰用什么模型更加好

首先导入包Package


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

再读入数据

path = 'data/ex2data1.txt'
data = pd.read_csv(path,header=None,names=['Exam 1','Exam 2','Admitted']) # 给data设置title
data.head()


20210129202557989.png

接着就画出散点图,⚪ 表示的是Admitted,X 表示的是Not Admitted,一个是正一个是负

positive = data[data['Admitted'].isin([1])] # 1
negative = data[data['Admitted'].isin([0])] # 0
fig,ax = plt.subplots(figsize=(12,8))
ax.scatter(x = positive['Exam 1'],y = positive['Exam 2'],s = 50,color = 'b',marker = 'o',label = 'Admitted')
ax.scatter(x = negative['Exam 1'],y = negative['Exam 2'],s = 50,color = 'r',marker = 'x',label = 'Not Addmitted')
plt.legend() # 显示label
ax.set_xlabel('Exam 1 Score') # set x_label
ax.set_ylabel('Exam 2 Score') # set y_label
plt.show()

2021012920325410.png


可以粗略的看出来,在这两者之间,可能存在一个决策边界进行分来,接着就可以建立逻辑回归模型解决这个分类模型


1.2 Implementation


1.2.1 Warmup exercise: sigmoid function


Before you start with the actual cost function, recall that the logistic regression hypothesis is defined as:

image.png

where function g is the sigmoid function. The sigmoid function is defined as:


image.png]]



结合起来,获得逻辑回归的假设:]


image.png]


以上我们回顾了逻辑回归模型里面的sigmod函数,接下来就开始定义他


def sigmoid(z):
    return 1 / (1 + np.exp(-z))


为了让我们更加对sigmod函数有一个更加清晰的认识,可视化一部分


x1 = np.arange(-10, 10, 0.1)
plt.plot(x1, sigmoid(x1), c='r')
plt.show()


20210129204236945.png


1.2.2 Cost function


Now you will implement the cost function and gradient for logistic regression.

logistic regression中的cost function与线性回归不同,因为这是一个凸函数 convex function


5c01d18b66e49.png

2021012921062434.png


def cost(theta, X, y):
    first = (-y) * np.log(sigmoid(X @ theta.T))
    second = (1-y) * np.log(1 - sigmoid(X @ theta.T))
    return np.mean(first - second)

如果为了得到cost的值,我们还需要对原有的训练集data进行一些操作

# add a ones column - this makes the matrix multiplication work out easier
if 'Ones' not in data.columns:
    data.insert(0,'Ones',1)
# set X (training data) and y (target variable)
X = data.iloc[:, :-1] # Convert the frame to its Numpy-array representation.
y = data.iloc[:,-1] # Return is NOT a Numpy-matrix, rather, a Numpy-array.
theta = np.zeros(X.shape[1])
X = np.array(X.values)
y = np.array(y.values)

让我们最好检验一下矩阵的维度


X.shape, theta.shape, y.shape 
# ((100, 3), (3,), (100,))


一切良好

最好就可以计算初始数据的cost值了


cost(theta, X, y) 
# 0.6931471805599453


代价大约是0.6931471805599453


1.2.3 Gradient


the gradient of the cost is a vector of the same length as θ where the jth element (for j = 0; 1; : : : ; n) is defined as follows:

20210129205856295.png


与线性回归的相比,公式没有很大的区别,只是函数改为了sigmod函数

def gradient(theta, X, y):
    return (X.T @ (sigmoid(X @ theta.T) - y))/len(X)
# the gradient of the cost is a vector of the same length as θ where the jth element (for j = 0, 1, . . . , n)
gradient(theta, X, y)
# array([ -0.1       , -12.00921659, -11.26284221])


1.2.4 Learning θ parameters


现在要试图找出让 J ( θ ) J(\theta)J(θ)取得最小值的参数θ \thetaθ。



5c01d34b3ee5e.png



反复更新每个参数,用这个式子减去学习率 α 乘以后面的微分项。求导后得到:


5c01d3aa903c7.png

计算得到等式:

image.png

来它同时更新所有θ \thetaθ的值。

这个更新规则和之前用来做线性回归梯度下降的式子是一样的, 但是假设的定义发生了变化。即使更新参数的规则看起来基本相同,但由于假设的定义发生了变化,所以逻辑函数的梯度下降,跟线性回归的梯度下降实际上是两个完全不同的东西。


可是如果用代码实现怎么办呢,在exp2.pdf中,一个称为“fminunc”的Octave函数是用来优化函数来计算成本和梯度参数。由于我们使用Python,我们可以用SciPy的“optimize”命名空间来做同样的事情。


这里我们使用的是高级优化算法,运行速度通常远远超过梯度下降。方便快捷。

只需传入cost函数,已经所求的变量theta,和梯度。cost函数定义变量时变量tehta要放在第一个,若cost函数只返回cost,则设置fprime=gradient。

这里使用fimin_tnc或者minimize方法来拟合,minimize中method可以选择不同的算法来计算,其中包括TNC

import scipy.optimize as opt
result = opt.fmin_tnc(func=cost, x0=theta, fprime=gradient, args=(X, y))
result
#  (array([-25.16131878,   0.20623159,   0.20147149]), 36, 0)


下面是第二种方法,结果是一样的

res = opt.minimize(fun=cost, x0=theta, args=(X, y), method='TNC', jac=gradient)
res
# help(opt.minimize) 
# res.x  # final_theta

20210129211450228.png

cost(result[0], X, y)
# 0.20349770158947394


1.2.5 Evaluating logistic regression


After learning the parameters, you can use the model to predict whether a particular student will be admitted. For a student with an Exam 1 score of 45 and an Exam 2 score of 85, you should expect to see an admission probability of 0.776.

我们现在已经学号了参数,我们需要利用这个模型去预测是否能被录取,比如一个Exam1得分为45,而Exam2得分为85的,他被录取的可能性大约是0.776

我们可以测试45分和85分的

# 实现hθ
def hfunc1(theta, X):
    return sigmoid(np.dot(theta.T, X))
hfunc1(result[0],[1,45,85])


0.7762906256930321


看来大约是77.6%没错,nice

我们定义:

当h θ {{h}_{\theta }}h

θ

大于等于0.5时,预测 y=1

当h θ {{h}_{\theta }}h

θ

 小于0.5时,预测 y=0 。

# 定义预测函数
def predict(theta, X):
    probability = sigmoid(X * theta.T)
    return [1 if x >= 0.5 else 0 for x in probability]
theta_min = np.matrix(result[0])
predictions = predict(theta_min, X)
correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for (a, b) in zip(predictions, y)]
accuracy = (sum(map(int, correct)) % len(correct))
print ('accuracy = {0}%'.format(accuracy))

accuracy = 89%


在整个数据集上进行测试,发现我们的accuracy大约达到了89%,还是挺不错的

当然,也可以利用sklearn库来得到准确率

from sklearn.metrics import classification_report
print(classification_report(predictions, y))

20210129215633524.png

相关文章
|
机器学习/深度学习 算法 Python
机器学习特征筛选:向后淘汰法原理与Python实现
向后淘汰法(Backward Elimination)是机器学习中一种重要的特征选择技术,通过系统性地移除对模型贡献较小的特征,以提高模型性能和可解释性。该方法从完整特征集出发,逐步剔除不重要的特征,最终保留最具影响力的变量子集。其优势包括提升模型简洁性和性能,减少过拟合,降低计算复杂度。然而,该方法在高维特征空间中计算成本较高,且可能陷入局部最优解。适用于线性回归、逻辑回归等统计学习模型。
704 7
|
机器学习/深度学习 人工智能 算法
Scikit-learn:Python机器学习的瑞士军刀
想要快速入门机器学习但被复杂算法吓退?本文详解Scikit-learn如何让您无需深厚数学背景也能构建强大AI模型。从数据预处理到模型评估,从垃圾邮件过滤到信用风险评估,通过实用案例和直观图表,带您掌握这把Python机器学习的'瑞士军刀'。无论您是AI新手还是经验丰富的数据科学家,都能从中获取将理论转化为实际应用的关键技巧。了解Scikit-learn与大语言模型的最新集成方式,抢先掌握机器学习的未来发展方向!
1570 12
Scikit-learn:Python机器学习的瑞士军刀
|
机器学习/深度学习 数据可视化 算法
Python与机器学习:使用Scikit-learn进行数据建模
本文介绍如何使用Python和Scikit-learn进行机器学习数据建模。首先,通过鸢尾花数据集演示数据准备、可视化和预处理步骤。接着,构建并评估K近邻(KNN)模型,展示超参数调优方法。最后,比较KNN、随机森林和支持向量机(SVM)等模型的性能,帮助读者掌握基础的机器学习建模技巧,并展望未来结合深度学习框架的发展方向。
Python与机器学习:使用Scikit-learn进行数据建模
|
机器学习/深度学习 数据可视化 TensorFlow
Python 高级编程与实战:深入理解数据科学与机器学习
本文深入探讨了Python在数据科学与机器学习中的应用,介绍了pandas、numpy、matplotlib等数据科学工具,以及scikit-learn、tensorflow、keras等机器学习库。通过实战项目,如数据可视化和鸢尾花数据集分类,帮助读者掌握这些技术。最后提供了进一步学习资源,助力提升Python编程技能。
|
机器学习/深度学习 数据可视化 算法
Python 高级编程与实战:深入理解数据科学与机器学习
在前几篇文章中,我们探讨了 Python 的基础语法、面向对象编程、函数式编程、元编程、性能优化和调试技巧。本文将深入探讨 Python 在数据科学和机器学习中的应用,并通过实战项目帮助你掌握这些技术。
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1923 102
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
643 104
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
521 103
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
573 82
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
688 3

热门文章

最新文章

推荐镜像

更多