Python学习笔记——《吴恩达Machine Learning》逻辑回归例程

news/2025/2/12 5:13:33/

文章目录

逻辑回归和线性回归的区别?

逻辑回归可以理解为线性回归的一个plus版,主要的区别在于逻辑回归使用的是sigmoid函数,而线性回归使用的是线性函数。逻辑回归中通过引入sigmoid函数将线性函数的输出映射到0到1之间的概率值,再加入阈值处理后,使其可以用于二分类问题的预测,如判断一粒肿瘤病理是良性还是恶性。
在这里插入图片描述

正则化逻辑回归

对于一些复杂的数据,用线性函数进行拟合容易造成欠拟合(underfit),而用过多的高次多项式拟合也可能会造成过拟合(overfit),因此除了学习率 α \alpha α外,通过引入另一个变量 λ \lambda λ,用于将与高次项式点乘的向量 w w w中引起过拟合的系数元素置零,可实现正则化的逻辑回归。

逻辑回归中的梯度下降:

在这里插入图片描述

模型预测案例

import numpy as np
import matplotlib.pylab as plt
import scipy.optimize as op# Load Data
data = np.loadtxt('ex2data1.txt', delimiter=',')
X = data[:, 0:2]
Y = data[:, 2]# ==================== Part 1: Plotting ====================
print('Plotting data with + indicating (y = 1) examples and o indicating (y = 0) examples.')# 绘制散点图像
def plotData(x, y):pos = np.where(y == 1)neg = np.where(y == 0)p1 = plt.scatter(x[pos, 0], x[pos, 1], marker='+', s=30, color='b')p2 = plt.scatter(x[neg, 0], x[neg, 1], marker='o', s=30, color='y')plt.legend((p1, p2), ('Admitted', 'Not admitted'), loc='upper right', fontsize=8)plt.xlabel('Exam 1 score')plt.ylabel('Exam 2 score')plt.show()plotData(X, Y)
# _ = input('Press [Enter] to continue.')# ============ Part 2: Compute Cost and Gradient ============
m, n = np.shape(X)
X = np.concatenate((np.ones((m, 1)), X), axis=1)
init_theta = np.zeros((n+1,))# sigmoid函数
def sigmoid(z):g = 1/(1+np.exp(-1*z))return g# 计算损失函数和梯度函数
def costFunction(theta, x, y):m = np.size(y, 0)h = sigmoid(x.dot(theta))if np.sum(1-h < 1e-10) != 0:return np.infj = -1/m*(y.dot(np.log(h))+(1-y).dot(np.log(1-h)))return jdef gradFunction(theta, x, y):m = np.size(y, 0)grad = 1 / m * (x.T.dot(sigmoid(x.dot(theta)) - y))return gradcost = costFunction(init_theta, X, Y)
grad = gradFunction(init_theta, X, Y)
print('Cost at initial theta (zeros): ', cost)
print('Gradient at initial theta (zeros): ', grad)
# _ = input('Press [Enter] to continue.')# ============= Part 3: Optimizing using fmin_bfgs  =============
# 注:此处与原始的情况有些出入
result = op.minimize(costFunction, x0=init_theta, method='BFGS', jac=gradFunction, args=(X, Y))
theta = result.x
print('Cost at theta found by fmin_bfgs: ', result.fun)
print('theta: ', theta)# 绘制图像
def plotDecisionBoundary(theta, x, y):pos = np.where(y == 1)neg = np.where(y == 0)p1 = plt.scatter(x[pos, 1], x[pos, 2], marker='+', s=60, color='r')p2 = plt.scatter(x[neg, 1], x[neg, 2], marker='o', s=60, color='y')plot_x = np.array([np.min(x[:, 1])-2, np.max(x[:, 1]+2)])plot_y = -1/theta[2]*(theta[1]*plot_x+theta[0])plt.plot(plot_x, plot_y)plt.legend((p1, p2), ('Admitted', 'Not admitted'), loc='upper right', fontsize=8)plt.xlabel('Exam 1 score')plt.ylabel('Exam 2 score')plt.show()plotDecisionBoundary(theta, X, Y)
# _ = input('Press [Enter] to continue.')# ============== Part 4: Predict and Accuracies ==============
prob = sigmoid(np.array([1, 45, 85]).dot(theta))
print('For a student with scores 45 and 85, we predict an admission probability of: ', prob)# 预测给定值
def predict(theta, x):m = np.size(X, 0)p = np.zeros((m,))pos = np.where(x.dot(theta) >= 0)neg = np.where(x.dot(theta) < 0)p[pos] = 1p[neg] = 0return p
p = predict(theta, X)
print('Train Accuracy: ', np.sum(p == Y)/np.size(Y, 0))  #计算训练准确率(p==Y的个数/Y的总个数)

在这里插入图片描述

解决二分类问题:

在这里插入图片描述

不同的 λ \lambda λ会产生不同的分类结果:

import numpy as np
import matplotlib.pylab as plt
import scipy.optimize as op# 加载数据
data = np.loadtxt('ex2data2.txt', delimiter=',')
X = data[:, 0:2]
Y = data[:, 2]def plotData(x, y):pos = np.where(y == 1)neg = np.where(y == 0)p1 = plt.scatter(x[pos, 0], x[pos, 1], marker='+', s=50, color='b')p2 = plt.scatter(x[neg, 0], x[neg, 1], marker='o', s=50, color='y')plt.legend((p1, p2), ('Admitted', 'Not admitted'), loc='upper right', fontsize=8)plt.xlabel('Exam 1 score')plt.ylabel('Exam 2 score')plt.show()plotData(X, Y)# =========== Part 1: Regularized Logistic Regression ============
# 向高维扩展
def mapFeature(x1, x2):degree = 6col = int(degree * (degree + 1) / 2 + degree + 1)out = np.ones((np.size(x1, 0), col))count = 1for i in range(1, degree + 1):for j in range(i + 1):out[:, count] = np.power(x1, i - j) * np.power(x2, j)count += 1return outX = mapFeature(X[:, 0], X[:, 1])
init_theta = np.zeros((np.size(X, 1),))
lamd = 10# sigmoid函数
def sigmoid(z):g = 1 / (1 + np.exp(-1 * z))return g# 损失函数
def costFuncReg(theta, x, y, lam):m = np.size(y, 0)h = sigmoid(x.dot(theta))j = -1 / m * (y.dot(np.log(h)) + (1 - y).dot(np.log(1 - h))) + lam / (2 * m) * theta[1:].dot(theta[1:])return j# 梯度函数
def gradFuncReg(theta, x, y, lam):m = np.size(y, 0)h = sigmoid(x.dot(theta))grad = np.zeros(np.size(theta, 0))grad[0] = 1 / m * (x[:, 0].dot(h - y))grad[1:] = 1 / m * (x[:, 1:].T.dot(h - y)) + lam * theta[1:] / mreturn gradcost = costFuncReg(init_theta, X, Y, lamd)
print('Cost at initial theta (zeros): ', cost)
# _ = input('Press [Enter] to continue.')# ============= Part 2: Regularization and Accuracies =============
init_theta = np.zeros((np.size(X, 1),))
lamd = 10
result = op.minimize(costFuncReg, x0=init_theta, method='BFGS', jac=gradFuncReg, args=(X, Y, lamd))
theta = result.xdef plotDecisionBoundary(theta, x, y):pos = np.where(y == 1)neg = np.where(y == 0)p1 = plt.scatter(x[pos, 1], x[pos, 2], marker='+', s=60, color='r')p2 = plt.scatter(x[neg, 1], x[neg, 2], marker='o', s=60, color='y')u = np.linspace(-1, 1.5, 50)v = np.linspace(-1, 1.5, 50)z = np.zeros((np.size(u, 0), np.size(v, 0)))for i in range(np.size(u, 0)):for j in range(np.size(v, 0)):z[i, j] = mapFeature(np.array([u[i]]), np.array([v[j]])).dot(theta)z = z.T[um, vm] = np.meshgrid(u, v)plt.contour(um, vm, z, levels=[0], lw=2)plt.legend((p1, p2), ('Admitted', 'Not admitted'), loc='upper right', fontsize=8)plt.xlabel('Microchip Test 1')plt.ylabel('Microchip Test 2')plt.title('lambda = 10')plt.show()plotDecisionBoundary(theta, X, Y)# 预测给定值
def predict(theta, x):m = np.size(X, 0)p = np.zeros((m,))pos = np.where(x.dot(theta) >= 0)neg = np.where(x.dot(theta) < 0)p[pos] = 1p[neg] = 0return pp = predict(theta, X)
print('Train Accuracy: ', np.sum(p == Y) / np.size(Y, 0))

在这里插入图片描述
λ = 2 : \lambda=2: λ=2
在这里插入图片描述
λ = 10 : \lambda=10: λ=10
在这里插入图片描述


http://www.ppmy.cn/news/74071.html

相关文章

Google 广告投放实操,小白入门看这篇就够了!

相信很多人做跨境电商都迈不过Google广告这道坎&#xff0c;许多卖家尝试在Google上投放广告&#xff0c;但却发现效果并不理想。今天东哥和大家来讨论一下Google广告投放的一些策略&#xff0c;看看能不能帮助大家取得更好的效果。 Google 广告投放实操 在实操开始前&#xff…

UE5中如何新建C++类?

UE5 插件开发指南 前言0.如何在UE编辑器内创建C++类?1.如何在UE编辑器外创建C++类?前言 这个问题应该细分成两个问题: (1)如何在编辑器内创建C++类? (2)如何在编辑器外创建C++类? 问题(1)主要针对那些可以在编辑器内继承并创建的类,然而有些内是无法在编辑内继承的,必须在…

Python中的logging模块

logging是Python中常见的日志工具&#xff0c;能够把一次运行的关键信息记录成日志&#xff0c;以便debug。为了让读者更快掌握这个工具&#xff0c;咱们逐步深入&#xff1a; import logginglogging.basicConfig(levellogging.INFO) logger logging.getLogger("MyLogge…

C语言函数大全-- _w 开头的函数(3)

C语言函数大全 本篇介绍C语言函数大全-- _w 开头的函数 1. _wmkdir 1.1 函数说明 函数声明函数功能int _wmkdir(const wchar_t* dirname);用于创建指定路径名的新目录 参数&#xff1a; dirname &#xff1a; 指向以 null 结尾的宽字符数组&#xff0c;该数组包含要创建的目…

浏览器的进程和线程

浏览器是多进程多线程的应用程序 浏览器进程 主要负责界面显示、用户交互、子进程管理等。浏览器进程内部会启动多个线程处理不同的任务。 网络进程 负责加载网络资源。网络进程内部会启动多个线程来处理不同的网络任务。 渲染进程 渲染进程启动后&#xff0c;会开启一个染主线…

无公网IP,SSH远程连接Linux CentOS

转载自cpolar内网穿透的文章&#xff1a;无公网IP&#xff0c;SSH远程连接Linux CentOS【内网穿透】 本次教程我们来实现如何在外公网环境下&#xff0c;SSH远程连接家里/公司的Linux CentOS服务器&#xff0c;无需公网IP&#xff0c;也不需要设置路由器。 视频教程 【SSH远程…

整除分块学习笔记

整除分块学习笔记 文章目录 整除分块学习笔记前言整除分块code后记 前言 最近在学习 莫比乌斯反演 &#xff0c;好像要用到一个小小的知识点&#xff1a; 整除分块 所以为了让大家都能更好地学习莫比乌斯反演&#xff0c;我来水一篇博客 那么正片开始。 整除分块 现在要求…

【利用AI刷面试题】AI:十道Vue面试题巩固一下知识

文章目录 1. 你在Vue中遇到过哪些性能问题&#xff1f;如何优化这些性能问题&#xff1f;2. Vue生命周期钩子函数有哪些&#xff1f;在什么情况下使用它们&#xff1f;3. Vue组件通信方式有哪些&#xff1f;它们各自适用于哪些场景&#xff1f;4. 你了解diff算法吗&#xff1f;…