Original: http://tech.meituan.com/mt-mlinaction-how-to-ml.html
Objective
With the advent of the big Data age, machine learning has become an important and critical tool for solving problems. In both industry and academia, machine learning is a hot direction, but academia and industry focus on machine learning, academia focuses on the study of machine learning theory, and industry focuses on how to solve practical problems with machine learning. In conjunction with our practice in machine learning, we carry out an introduction to the inaction series (Articles with the "Machine Learning Inaction Series" tab), introducing the basic skills, experience and skills that machine learning needs to solve the practical problems of industry. In this paper, we introduce the whole process of machine learning to solve practical problems, including the key points of modeling the problem, preparing training data, extracting features, training model and optimizing model, and a few more in-depth introduction to these key links.
Below are 1) Overview of machine learning, 2) modeling problems, 3) Preparing training data, 4) extracting features, 5) training models, 6) optimization models, 7) summarizing a total of 7 chapters.
Machine Learning Overview: What is machine learning?
As machine learning continues to be applied in the field of real industry, the word has been given a variety of different meanings. The meaning of "machine learning" in this article is more in agreement with the wikipedia explanation, as follows:
Machine learning are a scientific discipline that deals with the construction and study of algorithms that can learn from D Ata.
Machine learning can be divided into unsupervised learning (unsupervised learning) and supervised learning (supervised learning), in industry, supervised learning is a more common and valuable way, as described below in this way. As shown in, supervised machine learning in solving practical problems, there are two processes, one is the offline training process (blue arrow), including data filtering and cleaning, feature extraction, model training and optimization model, and so on, the other process is the application process (green arrow), the need to estimate the data, extract features, The model obtained by the offline training is used to estimate the pre-valuation effect in the actual product. In both of these processes, offline training is the most technically challenging work (the online estimation process is a lot of work that can be reused for the offline training process), so the following focuses on the offline training process.
What is a model?
Model is an important concept in machine learning, which refers to the mapping of feature space to output space, and generally consists of the assumption function and the parameter w of the model (the following formula is an expression of the logistic regression model, which is explained in detail in the section of the training model) A hypothetical space for a model (hypothesis spaces), which refers to the set of all possible w-corresponding output spaces for a given model. The models commonly used in industry are logistic Regression (LR), Gradient boosting decision Tree (abbreviated GBDT), Support Vector Machine (SVM), deep neural Network (hereinafter referred to as DNN).
Model training is based on training data, to obtain a set of parameter w, so that the specific goal is optimal, that is, to obtain the characteristic space to the output space of the optimal mapping, how to achieve, see the training model chapter.
Why use machine learning to solve problems?
- At present in the big data age, everywhere have a t into P data, simple rules processing difficult to play the value of these data;
- Inexpensive high-performance computing makes learning time and cost reduction based on large-scale data;
- Cheap, large-scale storage that enables faster and more costly processing of large-scale data;
- There are a lot of high-value problems, which make a lot of effort to use machine learning to solve problems, can get rich benefits.
What problems should machine learning be used to solve?
- The goal problem needs great value, because the machine learns solves the problem to have the certain price;
- There is a lot of data available for the target problem, and there is a lot of data to make the machine learning to solve the problem better (relative to simple rules or labor);
- The problem of the goal is determined by many factors (characteristics), the advantage of machine learning to solve the problem can be embodied (relative to simple rules or labor);
- The goal problem needs to be continuously optimized because machine learning can continue to play its value based on data self-learning and iteration.
Modeling the problem
This article takes the deal (purchase order) turnover estimate problem as an example (that is, estimate how much money is sold for a given deal over a period of time), and describes how to solve the problem using machine learning. First, you need:
- Collect the information of the problem, understand the problem and become the expert of the problem;
- Disassemble the problem, simplify the problem, and transform the problem into a machine-predictable problem.
After an in-depth understanding and analysis of deal turnover, it can be broken down into several questions:
A single model? Multiple models? How to choose?
Following the disassembly, the estimated deal turnover has 2 possible models, one is the direct estimate of the turnover, the other is to estimate the sub-problems, such as the establishment of a user number model and the establishment of a purchase rate model (access to the deal users will purchase the number of orders), and then based on the pre-valuation of these sub-problems to calculate the transaction
- Different ways have different advantages and disadvantages, specific as follows:
| Mode |
Disadvantages |
Advantages |
| Single model |
1. Difficult to predict 2. High risk ratio |
1. Theoretically, the best estimate can be obtained (in fact it is difficult) 2. One-time problem solving |
| Multi-model |
1. Accumulation error may occur 2. High training and application costs |
1. A single sub-model makes it easier to achieve a more accurate estimate 2. The sub-model can be adjusted to achieve the best results |
Which mode do you choose?
1) The problem can be estimated difficulty, the difficulty is big, then consider using the multi-model;
2) The importance of the problem itself, the problem is very important, then consider using multiple models;
3) If the relationship between multiple models is clear and the relationship is clear, then multiple models can be used.
If multiple models are used, how can they be fused?
Can be based on the characteristics and requirements of the problem of linear fusion, or complex fusion. Take this question as an example, there are at least two of the following:
Model selection
For the problem of deal turnover, we think that the direct estimation is very difficult, and we hope to split the sub-problem to estimate the multi-model mode. That would require the creation of a model of user numbers and an acquisition rate model, as machine learning solves the problem in a similar way, with the purchase rate model as an example. To solve the purchase rate problem, we have to choose the model first, we have the following considerations:
Main considerations
1) Select a model that is consistent with the business objectives;
2) Select the model that matches the training data and features.
训练数据少,High Level特征多,则使用“复杂”的非线性模型(流行的GBDT、Random Forest等);训练数据很大量,Low Level特征多,则使用“简单”的线性模型(流行的LR、Linear-SVM等)。
Supplementary consideration
1) Whether the current model is widely used by industry;
2) Whether the current model has a more mature open source Toolkit (in-company or outside the company);
3) The current toolkit can handle the amount of data can meet the requirements;
4) Do you know whether the current model theory has been used to solve the problem before?
In order to select the model for practical problems, the business objective of transforming problem is the model evaluation goal, the transformation model evaluation target is the model optimization target, according to the different objectives of the business, the specific relationship is as follows:
In general, it is difficult to estimate the true value (regression), size order (sort), the correct interval (classification) of the target, and choose the less difficult target according to the need of the application. We need to know at least the size order or the real value for the application target of the estimated rate of purchase, so we can choose area under Curve (AUC) or mean Absolute Error (MAE) as the evaluation target to maximum The likelihood is the model loss function (i.e. optimization target). In summary, we choose Spark version GBDT or LR, based on the following considerations:
1) can solve the problem of sorting or regression;
2) We have implemented the algorithm, often used, the effect is very good;
3) Support massive data;
4) The industry is widely used.
Prepare training data
In-depth understanding of the problem, after selecting the appropriate model for the problem, the next need to prepare the data, the data is the machine learning to solve the problem, the data is not selected, the problem can not be solved, so the preparation of training data requires extra care and attention:
Note the point:
- The distribution of the data to be solved is as consistent as possible;
- The training set/test set distribution and the data distribution of the online prediction environment are as consistent as possible, where the distribution refers to the distribution of (x, y), not just the distribution of y;
- Y data noise as small as possible, to eliminate the y noise data;
- Sampling is unnecessary, sampling can often change the actual data distribution, but if the data is too large to train or a positive or negative ratio is severe (for example, more than 100:1), a sampling solution is required.
Frequently asked questions and solutions
- The data distribution of the problem to be resolved is inconsistent:
1) The deal data in the purchase rate problem may vary greatly, such as food deal and hotel deal influence factors or performance is very inconsistent, need to do special treatment, either to the data in advance normalization, or the distribution of inconsistent factors as a feature, or different types of deal training model alone.
- The data distribution has changed:
1) Use the data training model six months ago to predict the current data, as the data distribution may change over time and the effect may be poor. Try to use recent data training to predict the current data, historical data can be used to reduce the right to use the model, or do transfer learning.
- Y data is noisy:
1) When the CTR model is established, the user does not see the item as a negative example, these item is because the user did not see to not be clicked, not necessarily the user does not like without being clicked, so these item is noisy. Some simple rules can be used to eliminate these negative noise examples, such as the use of Skip-above thought, that is, the user points over the item, there is no point over the item as a negative example (assuming the user is viewed from the top down item).
- The sampling method is biased and does not cover the entire collection:
1) in the purchase rate problem, if only one store's deal is estimated, the deal of the multi-store can not be well estimated. Should ensure that the deal data of a store and multiple stores are available;
2) No objective data of the two classification problem, using rules to obtain positive/negative examples, the rule of positive/negative coverage is not comprehensive. Randomly sampled data should be manually annotated to ensure that the sampled data and the actual data are distributed consistently.
Training data on the rate of purchase
- Collect n months of deal data (x) and corresponding purchase rate (Y);
- Collect the last n months, excluding holidays and other unconventional time (keep the distribution consistent);
- Collect only the deal of the online duration >t and the number of users > U (reduce the noise of y);
- Consider deal sales life cycle (consistent distribution);
- Consider the differences between different cities, different shopping areas, and different categories (keep the distribution consistent).
Extracting features
After data filtering and cleaning, it is necessary to extract the characteristics of the data, that is, to complete the conversion of the input space to the feature space (see). For a linear model or a non-linear model, different feature extraction is required, and the linear model requires more feature extraction work and techniques, while the non-linear model requires relatively low feature extraction.
Typically, features can be divided into high-level and low-level,high level refers to the meaning of the more general features, low level refers to the meaning of the specific characteristics, for example:
DEAL A1属于POIA,人均50以下,访购率高; DEAL A2属于POIA,人均50以上,访购率高; DEAL B1属于POIB,人均50以下,访购率高; DEAL B2属于POIB,人均50以上,访购率底;
Based on the above data, you can draw two characteristics, poi (store) or per capita consumption, POI feature is the low level feature, per capita consumption is the high level feature; Assuming that the model learns, the following estimates are obtained:
如果DEALx 属于POIA(Low Level feature),访购率高;如果DEALx 人均50以下(High Level feature),访购率高。
Therefore, in general, the low level is more targeted, the individual feature coverage is small (the data containing this feature is not many), the number of features (dimensions) is very large. The higher level is more generalized, the individual feature coverage is large (there are many data with this feature), and the number of features (dimensions) is small. The predicted values of long tail samples are mainly influenced by the characteristics of high level. The predicted values of high frequency samples are mainly influenced by the characteristics of low level.
There are a number of high or low level features for the purchase rate issue, some of which are shown in:
Characteristics of non-linear models
1) can mainly use the high level characteristics, because the computational complexity is large, so the characteristics of the dimension is inappropriate;
2) It is possible to fit the target well by the high-level nonlinear mapping.
Characteristics of linear models
1) The feature system should be as comprehensive as possible, both high and low level;
2) You can convert the high level to low to enhance the model's fit capability.
Normalization of features
After feature extraction, if different characteristics of the range of values vary greatly, it is best to normalization of features to achieve better results, the common normalization is as follows:
- Rescaling:
Normalized to [0,1] or [ -1,1], in a similar way:
Standardization:
The mean value of the x distribution, which is the standard deviation of the x distribution;
Scaling to Unit length:
Normalized to unit length vector
Feature Selection
After feature extraction and normalization, if you find that there are too many features that can cause the model to be untrained, or that it is easy to cause the model to cross-fit, you need to select the feature and pick a valuable feature.
Filter:
Assuming that the effect of feature subset on model estimation is independent, select a subset of features, analyze the relationship between the subset and the data label, and if there is a positive correlation, the subset of features is considered to be valid. There are many algorithms for measuring feature subsets and data label relationships, such as Chi-square,information Gain.
Wrapper:
Select a feature subset to join the original feature set, train with the model, compare the effect of the subset before and after, if the effect is better, it is considered that the feature subset is valid, otherwise it is considered invalid.
Embedded:
Combining feature selection and model training, such as adding L1 Norm to the loss function, L2 Norm.
Training model
After the feature extraction and processing, you can start the model training, the following is a simple and commonly used logistic regression model (hereinafter referred to as the LR model) as an example, a brief introduction.
with M (x, y) training data, where x is the eigenvector, Y is label,;w as the parameter vector in the model, which is the object that needs to be learned in model training.
The so-called training model is to select the hypothesis function and loss function, based on the existing training data (x, y), continuously adjust w, so that the loss function is optimal, the corresponding W is the final learning result, and the corresponding model is obtained.
Model functions
1)假说函数,即假设x和y存在一种函数关系:
2)损失函数,基于上述假设函数,构建模型损失函数(优化目标),在LR中通常以(x,y)的最大似然估计为目标:
Optimization algorithm
- Gradient descent (Gradient descent)
That is, W is adjusted in the negative gradient direction of the loss function, see the gradient is the first derivative (see below), gradient drop has many types, such as random gradient descent or batch gradient decline.
Random gradient descent (Stochastic Gradient descent), each step randomly selects a sample, calculates the corresponding gradient, and completes the update of W, as follows,
Batch gradient descent (batch Gradient descent), each step calculates the corresponding gradient of all samples in the training data, and W iterates along this gradient, i.e.
- Newton's method (Newton ' s method)
The basic idea of Newton's method is to find the estimate of the minimum point of L (W) by doing the second-order Taylor expansion of the objective function near the minimum point. Figuratively speaking, the tangent is made at the WK, and the intersection of the tangent and the L (W) =0 is the next iteration point wk+1 (see below). The update formula for W is as follows, where the second derivative of the objective function is the famous Hessian matrix.
Quasi-Newton method (Quasi-Newton Methods): Computing the second derivative of the objective function is more difficult, more complicated is that the Hessian matrix of the objective function cannot maintain positive definite, and constructs a positive definite symmetric matrix which can approximate the inverse of Hessian matrices without the second derivative, thus in " Optimization of the objective function under the condition of quasi-Newton.
BFGS: Use the BFGS formula to approximate H (W), in memory need to put H (W), Memory needs O (m2) level;
L-BFGS: Storage of a finite number of times (such as K-times) update matrix, with these update matrices to generate a new H (W), the memory to the O (m) level;
OWLQN: If L1 regularization is introduced into the objective function, it is necessary to introduce a virtual gradient to solve the non-conductive problem of the objective function, OWLQN is to solve the problem.
- Coordinate descent
For W, each iteration, fixed other dimensions unchanged, only one dimension of the search, determine the optimal descent direction (below), the formula is expressed as follows:
Optimization Model
After the data filtering and cleaning, feature design and selection, and model training mentioned above, a model is obtained, but what if the results are not good? What to do?
First
Reflect whether the target can be estimated, and whether the data and features are bugs.
and then
The analysis of the model is overfitting or underfitting, from the data, features and models and other aspects of targeted optimization.
Underfitting & Overfitting
The so-called underfitting, that is, the model does not learn the intrinsic relationship of data, as shown in the left, the resulting classification surface can not be very good to distinguish between the X and O data, the underlying reason is that the model assumes that the space is too small or the model assumes that the space deviation.
The so-called overfitting, that is, the model transition fitting the intrinsic relationship of training data, as shown in the right, the resulting classification surface is too good to distinguish between the X and O two types of data, and the real classification surface may not be so, so that the non-training data performance is not good, the underlying causes, Is the contradiction between the huge model hypothesis space and the sparse data.
In actual combat, the model can be based on the performance of the training set and test set to determine whether the current model is underfitting or overfitting, the way to determine the following table:
| Training Set Performance |
Test Set Performance |
problem |
| < expected target value |
< expected target value |
Underfitting |
| > Expected target value |
Approaching or slightly less than training set |
Right |
| > Expected target value |
Far worse than training set |
Overfitting |
How to solve underfitting and overfitting problem?
| problem |
Data |
features |
Model |
| Underfitting |
Cleaning data |
1. Adding features 2. Delete noise characteristics |
1. Lowering the penalty parameters for regular items 2. Change to a more "complex" model (e.g., to replace a linear model with a non-linear model) 3. Cascading or combining multiple models |
| Overfitting |
Add data |
1. Make Feature Selection 2. dimensionality reduction (e.g. clustering of features, processing of thematic models, etc.) |
1. Increase the penalty parameters for regular items 2. Reduce the number of training iterations 3. For a more "simple" model (e.g., to replace a non-linear model with a linear model) |
Summarize
In summary, machine learning solves problems involving problem modeling, preparing training data, extracting features, training models, and optimizing models, with the following key points:
- Understand the business, decompose the business objectives, and plan the roadmap for the model to predict.
- Data:
Y data as realistic and objective as possible;
The training set/test set distribution is as consistent as possible with the data distribution of the on-line application environment.
- Characteristics:
Using domain knowledge for feature extraction and selection;
Design different features for different types of models.
- Model:
Choose different models for different business goals, different data and features;
If the model does not meet the expectations, be sure to check the data, features, models and other processing links whether there are bugs;
Consider model Underfitting and qverfitting, targeted optimization.
Examples of how machine learning solves problems