Zero-based Beginners use Python to build click-through rate prediction model

This article will start from scratch, using only basic numpy library, use Python to achieve a simple neural network (or a simple LR, LR is because a single layer neural network), to solve the problem of a click-through rate estimated. Interested friends to follow the White see it Here Insert Picture Description
hits prediction model

0. Introduction

Benpian machine learning is a basic introductory article, familiarize ourselves with the neural network architecture with the use of machine learning.

Python accustomed to using every day in all kinds of sophisticated machine learning toolkits, such as sklearn, TensorFlow etc., to quickly build a wide variety of machine learning models to solve business problems.

This article will start from scratch, using only basic numpy library, use Python to achieve a simple neural network (or a simple LR, LR is because a single layer neural network), to solve the problem of a click-through rate estimated.

1. Assume a business scenario

Disclaimer: For simplicity, the following settings from simple ... everything.

Define the problem to be solved:

Boss: Mike, there are a number of microblogging log data click on this machine, you take to analyze, and then engage in what's predicted CTR ...

Yes, it is to predict whether the tweet is clicking (probability of being clicked) user ...... predict the future, looks like a very magical way!

hot Weibo

A brief look at deepening business data

Each has a micro-blog data consists of three parts: Twitter {id, Twitter wherein X, Y} Twitter click flag

Microblogging feature X has three dimensions: X={x0="该微博有娱乐明星”,x1="该微博有图”,x2="该微博有表情”}
whether microblogging is clicked flags Y:

Y={y0=“点击”, y1=“未点击”}

With data, we need to design a model, after the data input into the train, in the forecast period, just type {id microblogging, microblogging feature X}, the output of each model will be a microblogging id is clicked probability
2. task analysis:

This is a supervised machine learning tasks

For a supervised machine learning tasks can be simply divided into classification and regression problems, we simply want to achieve here predict whether a micro-Bo will be the user clicks, the goal is to predict a binary category: Click, click, or not be apparent as a classification problem.

So, we need to build a classification model (click-through rate prediction model), which also decided that we need to build a supervised learning the training data set.

Select models

Choose the simplest neural network model, artificial neural networks have several different types of neural networks, such as feed-forward neural networks, convolution neural networks and recurrent neural networks. This will be perceived or simple feedforward neural network as an example, this type of artificial neural network is a data transmitted directly from the front to the rear, before the short propagation process.

3. Data preparation:

The whole process:

Data preprocessing (numerical code) -> feature selection -> Select Model (feed forward neural networks) - Model Predictive> -> Model Training

Suppose, for four micro-blog data numerically coded, it can be expressed as the following matrix format: Here Insert Picture Description
training data XY

Interpretation of a sample data:
the first sample data: X0 = [0 0 1] , correspond to the three-dimensional features, the final 4x1 matrix is Y, 0 indicates none, 1 indicates there is, it is understood that the corresponding feature is not Y0 click.

So, this sample can be translated as: [The microblogging no entertainment stars, no pictures, there are expressions], final y = 0, representatives of the article microblogging not clicked.

Business and data feature is not very simple ... simple series looks a little less reasonable. -!

4. The basic structure of the neural network:

1. The input layer: data input traffic characteristics

2. hidden layer: initialization parameter weights

3. Activate function: choose to activate the function

4. The output layer: the predicted target, the definition of loss function

We are going to use machine learning models: the Here Insert Picture Description
super-simple feed-forward neural networks

Machine learning models similar to a black box, enter the historical click data for training, then you can predict the future amount of data ... designed above us it is a super simple feedforward neural network, but can achieve the purpose of our above .

About activation function:

By introducing activation function, to achieve a non-linear transformation, the effect of enhancing the fit of the model.

Related to the activation function, see the previous article My Love NLP (2) - activation function analytic depth learning

In this tutorial, using simple Sigmoid activation function, but note that, in the deep neural network model, sigmoid activation function is generally not preferred because of its diffusion gradient phenomenon prone. Here Insert Picture Description
sigmoid formula

此函数可以将任何值映射到0到1之间,并能帮助我们规范化输入的加权和。Here Insert Picture Description
sigmoid图像

对sigmoid激活函数求偏导Here Insert Picture Description该偏导函数吗,等下写程序会用到,所以先放在这里!

模型的训练

训练阶段,模型的输入X已经确定,输出层的Y确定,机器学习模型确定,唯一需要求解的就是模型中的权重W,这就是训练阶段的目标。

主要由三个核心的流程构成:

前向计算—>计算损失函数—>反向传播

本文使用的模型是最简单的前馈神经网络,起始就是一个LR而已….所以整个过程这里就不继续介绍了,因为之前已经写过一篇关于LR的文章— 逻辑回归(LR)个人学习总结篇 ,如果对其中的细节以及公式的推导有疑问,可以去LR文章里面去寻找答案。

这里再提一下权重参数W更新的公式:![至此,所有的写代码需要的细节都已经交代结束了,剩下的就是代码了。
Here Insert Picture Description
至此,所有的写代码需要的细节都已经交代结束了,剩下的就是代码了。

5.使用Python代码构建网络

# coding:utf-8
import numpy as np 
class NeuralNetwork(): 
 # 随机初始化权重
 def __init__(self): 
  np.random.seed(1) 
  self.synaptic_weights = 2 * np.random.random((3, 1)) - 1
 # 定义激活函数:这里使用sigmoid
 def sigmoid(self, x): 
  return 1 / (1 + np.exp(-x)) 
 #计算Sigmoid函数的偏导数 
 def sigmoid_derivative(self, x): 
  return x * (1 - x)
 # 训练模型 
 def train(self, training_inputs, training_outputs,learn_rate, training_iterations): 
  # 迭代训练
  for iteration in range(training_iterations): 
   #前向计算 
   output = self.think(training_inputs) 
   # 计算误差 
   error = training_outputs - output 
   # 反向传播-BP-微调权重 
   adjustments = np.dot(training_inputs.T, error * self.sigmoid_derivative(output)) 
   self.synaptic_weights += learn_rate*adjustments 
 def think(self, inputs): 
  # 输入通过网络得到输出 
  # 转化为浮点型数据类型 
  inputs = inputs.astype(float) 
  output = self.sigmoid(np.dot(inputs, self.synaptic_weights)) 
  return output 
if __name__ == "__main__": 
 # 初始化前馈神经网络类 
 neural_network = NeuralNetwork() 
 print "随机初始化的权重矩阵W"
 print neural_network.synaptic_weights
 # 模拟训练数据X
 train_data=[[0,0,1], [1,1,1], [1,0,1], [0,1,1]]
 training_inputs = np.array(train_data) 
 # 模拟训练数据Y
 training_outputs = np.array([[0,1,1,0]]).T 
 # 定义模型的参数:
 # 参数学习率
 learn_rate=0.1
 # 模型迭代的次数
 epoch=150000
 neural_network.train(training_inputs, training_outputs, learn_rate, epoch) 
 print "迭代计算之后权重矩阵W: "
 print neural_network.synaptic_weights
 # 模拟需要预测的数据X
 pre_data=[0,0,1]
 # 使用训练的模型预测该微博被点击的概率
 print "该微博被点击的概率:"
 print neural_network.think(np.array(pre_data))
"""
终端输出的结果:
随机初始化的权重矩阵W
[[-0.16595599]
 [ 0.44064899]
 [-0.99977125]]
迭代计算之后权重矩阵W: 
[[12.41691302]
 [-0.20410552]
 [-6.00463275]]
该微博被点击的概率:
[0.00246122]
[Finished in 20.2s]
"""

推荐我们的Python学习扣qun:913066266 ,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等【PDF,实战源码】,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地
6.总结:

根据终端输出的模型训练以及预测的结果,针对预测数据pre_data=[0,0,1],模型输出该微博被点击的概率为0.00246,很显然被点击的概率比较小,可以认为简单认为该微博不会被点击!

是的,我们的业务目标初步实现了----输入任意一条微博的样本数据到我们的机器学习模型中,既可以输出该样本被点击的概率。

The above is a super simple model we have designed, assuming a super simple business scenarios, and super-simple random set of training data, if there are unreasonable places compiled bear with me! ! ! While this example may not help you solve real business problems, but for the novice understanding of the neural network learning machine, it might be a little help now!

Published 47 original articles · won praise 53 · views 50000 +

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104270355