• 模块
    • sigmoid函数
    • 预测函数
    • 损失函数
    • 梯度计算
    • 梯度下降
    • 精度
  • 完整代码
  • 附录

建立一个逻辑回归模型来预测一个学生是否被录取。
数据:LogiReg_data.txt

模块

  • sigmoid: 映射到概率的函数
  • model: 返回预测结果值(预测函数)
  • cost: 根据参数计算损失
  • gradient: 计算每个参数的梯度方向
  • descent: 梯度下降,参数更新
  • accuracy: 计算精度

sigmoid函数

g ( z ) = 1 1 + e − z g(z) = \frac{1}{1 + e^{-z}} g(z)=1+e−z1​

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

预测函数

h θ ( x ) = g ( θ T x ) = 1 1 + e − θ T x h_\theta(x) = g(\theta^Tx) = \frac{1}{1 + e^{-\theta^Tx}} hθ​(x)=g(θTx)=1+e−θTx1​

def model(X, theta):"""预测函数"""return sigmoid(np.dot(X, theta.T))

损失函数

损失函数,将对数似然函数去负号

D ( h θ ( x ) , y ) = − y log ⁡ ( h θ ( x ) ) − ( 1 − y ) log ⁡ ( 1 − h θ ( x ) ) D(h_\theta(x), y) = -y\log(h_\theta(x)) - (1-y)\log(1-h_\theta(x)) D(hθ​(x),y)=−ylog(hθ​(x))−(1−y)log(1−hθ​(x))

求平均损失

J ( θ ) = 1 n ∑ i = 1 n D ( h θ ( x i ) , y i ) J(\theta) = \frac{1}{n}\sum\limits_{i=1}^{n}D(h_\theta(x^i), y^i) J(θ)=n1​i=1∑n​D(hθ​(xi),yi)

def cost(X, y, theta):"""损失函数"""left = np.multiply(-y, np.log(model(X, theta)))right = np.multiply(1 - y, np.log(1 - model(X, theta)))return np.sum(left - right) / (len(X))

梯度计算

即求偏导

∂ J ∂ θ j = − 1 m ∑ i = 1 n ( y i − h θ ( x i ) ) x j i \frac{\partial J}{\partial \theta_j} = - \frac{1}{m}\sum\limits_{i=1}^n (y^i-h_\theta(x^i))x^i_j ∂θj​∂J​=−m1​i=1∑n​(yi−hθ​(xi))xji​

def gradient(X, y, theta):"""梯度函数"""# 梯度和θ参数一一对应grad = np.zeros(theta.shape)error = (model(X, theta) - y).ravel()for j in range(len(theta.ravel())):term = np.multiply(error, X[:, j])  # 都是一维grad[0, j] = np.sum(term) / len(X)return grad

梯度下降

import numpy.random
def shuffleData(data):"""洗牌,打乱数据"""np.random.shuffle(data)cols = data.shape[1]X = data[:, 0:cols-1]y = data[:, cols-1:]return X, y
import timedef descent(data, batchSize, stopType, thresh, alpha):"""梯度下降求解"""# 初始化init_time = time.time()  # 起始时间i = 0  # 迭代次数k = 0  # batchX, y = shuffleData(data)  # 洗牌theta = np.zeros([1, X.shape[1]])  # θ参数costs = [cost(X, y, theta)]  # 损失值while True:grad = gradient(X[k: k+batchSize], y[k: k+batchSize], theta)k += batchSizeif k >= n:k = 0X, y = shuffleData(data)  # 重新洗牌theta = theta - alpha * grad  # 参数更新costs.append(cost(X, y, theta))  # 计算新的损失i += 1# 三种停止策略if ((stopType == 'iter' and i > thresh) or(stopType == 'cost' and abs(costs[-1] - costs[-2]) < thresh) or(stopType == 'grad' and np.linalg.norm(grad) < threshold)):breakreturn theta, i-1, costs, grad, time.time()-init_time
def runExpe(data, batchSize, stopType, thresh, alpha):theta, iter, costs, grad, dur = descent(data, batchSize, stopType, thresh, alpha)# 以下可视化处理name = "Original" if (data[:, 1] > 2).sum() > 0 else "Scaled"name += " data - learning rate: {} - ".format(alpha)if batchSize == n:strDescType = "Gradient"elif batchSize == 1:strDescType = "Stochastic"else:strDescType = "Mini-batch ({})".format(batchSize)name += strDescType + " descent - Stop: "if stopType == 'iter':strStop = "{} iterations".format(thresh)elif stopType == 'cost':strStop = "costs change < {}".format(thresh)else:strStop = "gradient norm < {}".format(thresh)name += strStopprint("***{}\nTheta: {} - Iter: {} - Last cost: {:03.2f} - Duration: {:03.2f}s".format(name, theta, iter, costs[-1], dur))fig, ax = plt.subplots(figsize=(12, 4))ax.plot(np.arange(len(costs)), costs, 'r')ax.set_xlabel('iterations')ax.set_ylabel('Cost')ax.set_title(name.upper() + ' - Error vs. iterations')fig.show()return theta

精度

def predict(X, theta):"""预测值"""# 设定阈值xreturn [1 if x >= 0.5 else 0 for x in model(X, theta)]
# 数据准备
path = 'LogiReg_data.txt'
pdData = pd.read_csv(path, header=None, names=['Exam 1', 'Exam 2', 'Admitted'])
# 插入全为1的列
pdData.insert(0, 'Ones', 1)
# dataframe转换成array类型
orig_data = pdData.values
# 数据数量
n = orig_data.shape[0]# 数据标准化处理
from sklearn import preprocessing as pp
scaled_data = orig_data.copy()
scaled_data[:, 1:3] = pp.scale(orig_data[:, 1:3])# 执行逻辑回归
theta = runExpe(scaled_data, n, 'iter', 5000, 0.01)scaled_X = scaled_data[:, :3]
y = scaled_data[:, 3]
# 预测值
predictions = predict(scaled_X, theta)
# 计算准确率
correct = 0
for num, i in enumerate(predictions):if i == y[num]:correct += 1
print('accuracy = {}%'.format(correct / len(predictions)))

完整代码

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import numpy.random
import timedef show_data():"""显示原始数据"""# pdData.head()  # 显示头5条positive = pdData[pdData['Admitted'] == 1]negative = pdData[pdData['Admitted'] == 0]fig, ax = plt.subplots(figsize=(10, 5))ax.scatter(positive['Exam 1'], positive['Exam 2'], s=30, c='b', marker='o', label='Admitted')ax.scatter(negative['Exam 1'], negative['Exam 2'], s=30, c='r', marker='x', label='Not Admitted')ax.legend()ax.set_xlabel('Exam 1 Score')ax.set_ylabel('Exam 2 Score')fig.show()def sigmoid(z):"""sigmoid函数"""return 1 / (1 + np.exp(-z))def model(X, theta):"""预测函数"""return sigmoid(np.dot(X, theta.T))def cost(X, y, theta):"""损失函数"""left = np.multiply(-y, np.log(model(X, theta)))right = np.multiply(1 - y, np.log(1 - model(X, theta)))return np.sum(left - right) / (len(X))def gradient(X, y, theta):"""计算梯度函数"""# 梯度和θ参数一一对应grad = np.zeros(theta.shape)error = (model(X, theta) - y).ravel()for j in range(len(theta.ravel())):term = np.multiply(error, X[:, j])  # 都是一维grad[0, j] = np.sum(term) / len(X)return graddef shuffleData(data):"""洗牌,打乱数据"""np.random.shuffle(data)cols = data.shape[1]X = data[:, 0:cols-1]y = data[:, cols-1:]return X, ydef descent(data, batchSize, stopType, thresh, alpha):"""梯度下降求解"""# 初始化init_time = time.time()  # 起始时间i = 0  # 迭代次数k = 0  # batch   X, y = shuffleData(data)  # 洗牌Xtheta = np.zeros([1, X.shape[1]])  # θ参数costs = [cost(X, y, theta)]  # 损失值while True:grad = gradient(X[k: k+batchSize], y[k: k+batchSize], theta)k += batchSizeif k >= n:k = 0X, y = shuffleData(data)  # 重新洗牌theta = theta - alpha * grad  # 参数更新costs.append(cost(X, y, theta))  # 计算新的损失i += 1# 三种停止策略if ((stopType == 'iter' and i > thresh) or(stopType == 'cost' and abs(costs[-1] - costs[-2]) < thresh) or(stopType == 'grad' and np.linalg.norm(grad) < threshold)):breakreturn theta, i-1, costs, grad, time.time()-init_timedef runExpe(data, batchSize, stopType, thresh, alpha):theta, iter, costs, grad, dur = descent(data, batchSize, stopType, thresh, alpha)# 可视化处理name = "Original" if (data[:, 1] > 2).sum() > 0 else "Scaled"name += " data - learning rate: {} - ".format(alpha)if batchSize == n:strDescType = "Gradient"elif batchSize == 1:strDescType = "Stochastic"else:strDescType = "Mini-batch ({})".format(batchSize)name += strDescType + " descent - Stop: "if stopType == 'iter':strStop = "{} iterations".format(thresh)elif stopType == 'cost':strStop = "costs change < {}".format(thresh)else:strStop = "gradient norm < {}".format(thresh)name += strStopprint("***{}\nTheta: {} - Iter: {} - Last cost: {:03.2f} - Duration: {:03.2f}s".format(name, theta, iter, costs[-1], dur))fig, ax = plt.subplots(figsize=(12, 4))ax.plot(np.arange(len(costs)), costs, 'r')ax.set_xlabel('iterations')ax.set_ylabel('Cost')ax.set_title(name.upper() + ' - Error vs. iterations')fig.show()return thetadef predict(X, theta):"""预测值"""# 设定阈值xreturn [1 if x >= 0.5 else 0 for x in model(X, theta)]path = 'LogiReg_data.txt'
pdData = pd.read_csv(path, header=None, names=['Exam 1', 'Exam 2', 'Admitted'])
# 插入全为1的列
pdData.insert(0, 'Ones', 1)
# dataframe转换成array类型
orig_data = pdData.values
# 数据数量
n = orig_data.shape[0]# 数据标准化处理
from sklearn import preprocessing as pp
scaled_data = orig_data.copy()
scaled_data[:, 1:3] = pp.scale(orig_data[:, 1:3])theta = runExpe(scaled_data, n, 'iter', 5000, 0.01)scaled_X = scaled_data[:, :3]
y = scaled_data[:, 3]
# 预测值
predictions = predict(scaled_X, theta)
# 计算准确率
correct = 0
for num, i in enumerate(predictions):if i == y[num]:correct += 1
print('accuracy = {}%'.format(correct / len(predictions)))

附录

梯度下降三种比较方法

目标函数 J ( θ ) = 1 2 m ∑ i = 1 m ( y i − h θ ( x i ) ) 2 J(\theta) = \frac{1}{2m}\sum\limits_{i=1}^{m} (y^i - h_\theta(x^i))^2 J(θ)=2m1​i=1∑m​(yi−hθ​(xi))2

  • 批量梯度下降: ∂ ∂ θ j J ( θ ) = − 1 m ∑ i = 1 m ( y i − h θ ( x i ) ) x j i \frac{\partial}{\partial\theta_j} J(\theta) = -\frac{1}{m} \sum\limits_{i=1}^{m} (y^i - h_\theta(x^i))x^{i}_{j} ∂θj​∂​J(θ)=−m1​i=1∑m​(yi−hθ​(xi))xji​

(容易得到最优解,每次考虑所有样本,速度慢)

  • 随机梯度下降: θ j ′ = θ j + ( y i − h θ ( x i ) ) x j i \theta_j^{'} = \theta_j + (y^i - h_\theta(x^i)) x^i_j θj′​=θj​+(yi−hθ​(xi))xji​

(每次一个样本,迭代速度快,但不一定每次朝收敛方向)

  • 小批量梯度下降: θ j : = θ j − α 1 10 ∑ k = i i + 9 ( h θ ( x ( k ) ) − y ( k ) ) x j ( k ) \theta_j:= \theta_j - \alpha \frac{1}{10} \sum\limits_{k=i}^{i+9} (h_\theta(x^{(k)}) - y^{(k)}) x^{(k)}_j θj​:=θj​−α101​k=i∑i+9​(hθ​(x(k))−y(k))xj(k)​

(每次更新选择一小部分数据来算,实用!)

似然函数与对数似然函数

似然函数: L ( θ ) = ∏ i = 1 m P ( y i ∣ x i ; θ ) = ∏ i = 1 m ( h θ ( x i ) ) y i ( 1 − h θ ( x i ) ) 1 − y i L(\theta) = \prod_{i=1}^{m}P(y^i | x^i; \theta) = \prod_{i=1}^{m}(h\theta(x^i))^{y^i}(1-h\theta(x^i))^{1-y^i} L(θ)=∏i=1m​P(yi∣xi;θ)=∏i=1m​(hθ(xi))yi(1−hθ(xi))1−yi

对数似然函数: l ( θ ) = log ⁡ L ( θ ) = ∑ i = 1 m ( y i log ⁡ h θ ( x i ) + ( 1 − y i ) log ⁡ ( 1 − h θ ( x i ) ) ) l(\theta) = \log L(\theta) = \sum\limits_{i=1}^m(y^i \log h_\theta(x^i) + (1-y^i) \log(1-h_\theta(x^i))) l(θ)=logL(θ)=i=1∑m​(yiloghθ​(xi)+(1−yi)log(1−hθ​(xi)))

机器学习项目实战——预测学生是否被录取相关推荐

  1. 机器学习项目实战(五) 住房价格预测

    机器学习项目实战系列   住房价格预测 目录 机器学习项目实战系列   住房价格预测 一.概述 二.分析数据 1.数据导入 2.基础统计运算 3.特征观察 4.建立模型 5.分析模型表现 (1)学习曲 ...

  2. python智慧城市_智慧城市背景下Python机器学习项目实战案例分享

    首先,何为智慧城市?智慧城市的"智慧"源自何处? 智慧城市的特征在于运用大数据和数字技术提高居民生活质量. 机构获得的数据越全面.越实时,它们就越有能力观测事件发生的详情.分析需求 ...

  3. 机器学习项目实战——02回归算法之葡萄酒质量与时间的关系

    数据集,机器学习项目实战--数据集与代码-机器学习文档类资源-CSDN文库 采用的是一元线性回归模型 import numpy as np import matplotlib.pyplot as pl ...

  4. 智慧城市背景下Python机器学习项目实战案例分享

    首先,何为智慧城市?智慧城市的"智慧"源自何处? 智慧城市的特征在于运用大数据和数字技术提高居民生活质量. 机构获得的数据越全面.越实时,它们就越有能力观测事件发生的详情.分析需求 ...

  5. 机器学习项目实战-能源利用率 Part-3(特征工程与特征筛选)

    博主前期相关的博客可见下: 机器学习项目实战-能源利用率 Part-1(数据清洗) 机器学习项目实战-能源利用率 Part-2(探索性数据分析) 这部分进行的特征工程与特征筛选. 三 特征工程与特征筛 ...

  6. 【机器学习项目实战】Python基于协同过滤算法进行电子商务网站用户行为分析及服务智能推荐

    说明:这是一个机器学习实战项目(附带数据+代码+文档+代码讲解),如需数据+代码+文档+代码讲解可以直接到文章最后获取. 1.项目背景 电子商务网站数量迅速上升,将电子商务网站浏览者变为实际消费者,满 ...

  7. 【机器学习项目实战案例目录】项目详解 + 完整源码

    前言 大家好,我是阿光. 本专栏整理了<机器学习项目实战案例>,内包含了各种不同的入门级机器学习项目,包含项目原理以及源码,每一个项目实例都附带有完整的代码. 正在更新中~ ✨

  8. 【机器学习项目实战10例目录】项目详解 + 数据集 + 完整源码

    前言 大家好,我是阿光. 本专栏整理了<机器学习项目实战10例>,内包含了各种不同的入门级机器学习项目,包含项目原理以及源码,每一个项目实例都附带有完整的代码+数据集. 正在更新中~ ✨

  9. 【机器学习项目实战】Python实现聚类(Kmeans)分析客户分组

    说明:这是一个机器学习实战项目(附带数据+代码),如需数据+完整代码可以直接到文章最后获取. 1.问题定义 在日常银行.电商等公司中,随着时间的推移,都会积累一些客户的数据.在当前的大数据时代.人工智 ...

最新文章

  1. c++函数返回值与引用
  2. 从零单排之玩转Python安全编程(II)
  3. 【算法系列之九】合并两个有序数组
  4. LeetCode 1615. 最大网络秩(出入度)
  5. php框架 事件,php框架Minor5事件(附代码)
  6. Theano 中文文档 0.9 - 7.2.6 Theano如何处理形状信息
  7. Pomelo:网易开源基于 Node.js 的游戏服务端框架
  8. hacker基础教程
  9. 当年资本家倒掉牛奶,到底是怎么回事?
  10. HDU1412 {A} + {B}【排序+集合合并】
  11. Warning: 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/' already解决
  12. ibm服务器硬盘谁生产,IBM硬盘-昔日的开山鼻祖为何家道中落?
  13. 数据库事务的一致性和原子性浅析
  14. VSCode画 UML 图
  15. 生成微信公众号二维码(动态,彩色)(python)
  16. iOS麦克风运用——腾讯微博“吹一吹”
  17. hulu dpp_什么是直播电视的Hulu,它可以代替您的有线电视订阅吗?
  18. 化工学matlab,化工和石化行业
  19. 自动化测试工程师面试简历参考
  20. 人类小行星探测任务回顾

热门文章

  1. uniapp 雷达图
  2. adb wifi 实现多设备无线连接Android手机
  3. disp语句怎么格式 matlab_Matlab中disp和sprintf函数使用方法和区别介绍
  4. 计数器的原理及VHDL实现
  5. Linux/Centos 安装oracle报错“调用makefile '/oracle/produc
  6. 前端后端的爱恨情仇--续集
  7. RepVGG网络中重参化网络结构解读【附代码】
  8. AI测肤,你今天真好看
  9. 上海博达高速网吧运营级接入方案(转)
  10. Phase transitions in information spreading on structured populations