大数据分析 在线学习


在线学习是机器学习的一个子领域,它允许将监督学习模型扩展到海量数据集。基本思想是我们不需要读取内存中的所有数据来拟合模型,我们只需要一次读取每个实例。

在这种情况下,我们将展示如何使用逻辑回归实现在线学习算法。与大多数监督学习算法一样,有一个最小化的成本函数。在逻辑回归中,代价函数定义为:

$$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 ]$$

where 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 竞赛。原始数据可以在 bda/part3/大众 文件夹。在这里,我们有两个文件:

  • 我们有训练数据(train_titanic.csv),并且
  • 未标记的数据以进行新的预测(test_titanic.csv)。

为了将 csv 格式转换为 vowpal wabbit 输入格式使用 csv_to_vowpal_wabbit.py Python脚本。你显然需要为此安装 python。导航到 bda/part3/大众 文件夹,打开终端并执行以下命令:

python csv_to_vowpal_wabbit.py

请注意,对于本节,如果你使用的是 Windows,则需要安装 Unix 命令行,输入 cygwin 网站。

打开终端,也在文件夹中 bda/part3/大众 并执行以下命令:

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 means.

  • -f 模型.vw :表示我们将模型保存在model.vw文件中,供以后进行预测

  • --binary : 将损失报告为带有-1,1标签的二进制分类

  • --通过 20 : 使用数据20次学习权重

  • -c :创建缓存文件

  • -q ff :在f命名空间中使用二次特征

  • --sgd :使用regular/classic/simple stochastic gradient descent update,即非自适应、非归一化、非不变。

  • --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