Data Analytics教程

数据分析 在线学习

在线学习是机器学习的一个子领域,允许将监督学习模型扩展到海量数据集。基本思想是我们不需要读取内存中的所有数据来拟合模型,我们只需要一次读取每个实例。
在这种情况下,我们将展示如何使用逻辑回归实现在线学习算法。与大多数监督学习算法一样,有一个最小化的成本函数。在逻辑回归中,成本函数定义为-
$$J(\theta) \: = \: \frac{-1}{m} \left [ \sum_{i = 1}^{m}y^{(i)}log(h_{\ theta}(x^{(i)})) + (1-y^{(i)}) log(1-h_{\theta}(x^{(i)})) \right ]$$
其中 J(θ)代表成本函数, hθ(x)代表假设。在逻辑回归的情况下,它用以下公式定义-
$$h_\theta(x) = \frac{1}{1 + e^{\theta^T x}}$$
既然我们已经定义了成本函数,我们需要找到一个算法来最小化它。实现这一目标的最简单算法称为随机梯度下降。算法对逻辑回归模型权重的更新规则定义为-
$$\theta_j : = \theta_j-\alpha(h_\theta(x)-y)x$$
以下算法有多种实现,但在 vowpal wabbit 中实现的一种 图书馆是迄今为止最发达的图书馆。该库允许训练大规模回归模型并使用少量 RAM。用创作者自己的话来说,它被描述为:"Vowpal Wabbit (VW) 项目是一个由 Microsoft Research 和(以前的)Yahoo! Research 赞助的快速核外学习系统。
我们将使用来自 kaggle 比赛的 Titanic 数据集。原始数据可以在 bda/part3/vw 文件夹中找到。在这里,我们有两个文件-
我们有训练数据 (train_titanic.csv),并且 未标记的数据以进行新的预测 (test_titanic.csv)。
为了将 csv 格式转换为 vowpal wabbit 输入格式,请使用 csv_to_vowpal_wabbit.py python 脚本。您显然需要为此安装python。导航到 bda/part3/vw 文件夹,打开终端并执行以下命令-
python csv_to_vowpal_wabbit.py
请注意,对于本节,如果您使用的是 Windows,则需要安装 Unix 命令行,请输入 cygwin 网站。
打开终端和文件夹 bda/part3/vw 并执行以下命令-
vw train_titanic.vw-f model.vw--binary--passes 20-c-q ff--sgd--l1 
0.00000001--l2 0.0000001--learning_rate 0.5--loss_function logistic
让我们分解 vw call 的每个参数的含义。
-f model.vw-表示我们将模型保存在 model.vw 文件中,以便稍后进行预测 --binary-将损失报告为带有-1,1 标签的二元分类 --passes 20-数据被使用 20 次来学习权重 -c-创建缓存文件 -q ff-在 f 命名空间中使用二次特征 --sgd-使用常规/经典/简单随机梯度下降更新,即非自适应、非标准化和非不变。 --l1--l2-L1 和 L2 范数正则化 --learning_rate 0.5-更新规则公式中定义的学习率α
以下代码显示了在命令行中运行回归模型的结果。在结果中,我们得到了平均对数损失和算法性能的小报告。
-loss_function logistic
creating quadratic features for pairs: ff  
using l1 regularization = 1e-08 
using l2 regularization = 1e-07 
final_regressor = model.vw 
Num weight bits = 18 
learning rate = 0.5 
initial_t = 1 
power_t = 0.5 
decay_learning_rate = 1 
using cache_file = train_titanic.vw.cache 
ignoring text input in favor of cache input 
num sources = 1 
average    since         example   example  current  current  current 
loss       last          counter   weight    label   predict  features 
0.000000   0.000000          1      1.0   -1.0000  -1.0000       57 
0.500000   1.000000          2      2.0     1.0000  -1.0000       57 
0.250000   0.000000          4      4.0     1.0000    1.0000       57 
0.375000   0.500000          8      8.0   -1.0000  -1.0000       73 
0.625000   0.875000         16     16.0   -1.0000    1.0000       73 
0.468750   0.312500         32     32.0   -1.0000  -1.0000       57 
0.468750   0.468750         64     64.0   -1.0000    1.0000       43 
0.375000   0.281250        128    128.0     1.0000  -1.0000       43 
0.351562   0.328125        256    256.0     1.0000  -1.0000       43 
0.359375   0.367188        512    512.0   -1.0000    1.0000       57 
0.274336   0.274336       1024   1024.0   -1.0000  -1.0000       57 h 
0.281938   0.289474       2048   2048.0   -1.0000  -1.0000       43 h 
0.246696   0.211454       4096   4096.0   -1.0000  -1.0000       43 h 
0.218922   0.191209       8192   8192.0     1.0000    1.0000       43 h 
finished run 
number of examples per pass = 802 
passes used = 11 
weighted example sum = 8822 
weighted label sum =-2288 
average loss = 0.179775 h 
best constant =-0.530826 
best constant’s loss = 0.659128 
total feature number = 427878
现在我们可以使用我们训练的 model.vw 用新数据生成预测。
vw-d test_titanic.vw-t-i model.vw-p predictions.txt 
在上一个命令中生成的预测未标准化以适应 [0, 1] 范围。为此,我们使用了 sigmoid 变换。
# Read the predictions
preds = fread('vw/predictions.txt')  
# Define the sigmoid function 
sigmoid = function(x) { 
   1 / (1 + exp(-x)) 
} 
probs = sigmoid(preds[[1]])  
# Generate class labels 
preds = ifelse(probs > 0.5, 1, 0) 
head(preds) 
# [1] 0 1 0 0 1 0 
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4