## Spark machine learning ALS principle (1)

1. Linear regression model Linear regression is the most commonly used algorithm in statistics. When you want to represent the mathematical relationship between two variables, you can use linear regression. When you use it, you first assume a linear relationship between the output variables (corresponding variables, dependent variables, labels) and predictors (independent variables, explanatory variables, features).
(Independent variables refer to factors or conditions that are actively manipulated by the researcher and cause the dependent variable to change. Therefore, the independent variable is considered the cause of the dependent variable.
The dependent variable is: In the functional relationship, a certain quantity will change with the change of one (or several) quantity. )
The linear model may be used for questions such as: you are studying the relationship between a company's sales and the company's investment in advertising, or the number of friends a person has on a social networking site and his daily presence on the social networking site The relationship between the time spent.
To understand a point of entry for linear regression, first determine the straight line. We know that a straight line can be completely determined by the slope and intercept.
Example 1:
Assumptions (number of users, profit value)
S = ((x, y) = (1,25), (10,250), (100,2500))

Example 2:
Assumptions (number of friends, online time)

Seeing the current picture, it is difficult to see the relationship between the two variables at a glance.
We assume that the graph is a linear relationship, and multiple lines can be drawn.

So which line is the optimal line we use? This is a fitting process

2.spark ALS
ALS Chinese masterpiece alternating least square method is an upgrade based on the least square method. In machine learning, ALS refers to a collaborative filtering algorithm that uses the least square method to solve, which is one of the collaborative filtering. The ALS algorithm is a relatively common collaborative filtering algorithm used since 2008. From the perspective of collaborative filtering classification, the ALS algorithm belongs to User-Item CF, also called hybrid CF, because it considers both User and Item, that is, it can be based on user recommendations and items. As shown in the following figure, u Indicates a user, v indicates a product, and the user rates the product, but not every user will rate each product. For example, user u6 did not give a score to product v3. We need to infer that this is the task of machine learning.

ALS 矩阵是低秩的，即一个 m*n 的矩阵，是由 m*k k*n 两个矩阵相乘得到的，其中 k<<m,n Since not every user scores every product, it can be assumed that the ALS matrix is ​​of low rank, that is, a m * n matrix is ​​obtained by multiplying two matrices , m * k and k * n , where k << m, n .

A m × n = U m × k × V k × n

This assumption is reasonable because both the user and the product contain some low-dimensional hidden features. For example, as long as we know that someone likes carbonated drinks, we can infer that he likes Best Cola, Coca-Cola, and Fanta without explicitly pointing out. He likes these three drinks. The carbonated drink here is equivalent to a hidden feature. U m×k 表示用户对隐藏特征的偏好， V k×n 表示产品包含隐藏特征的程度。 In the above formula, U m × k represents the user's preference for hidden features, and V k × n represents the degree to which the product contains hidden features. U m×k V k×n The task of machine learning is to find U m × k and V k × n . u i T v j 是用户 i 对商品 j 的偏好，使用 Frobenius 范数来量化重构 U V 产生的误差。 It can be known that u i T v j is the preference of user i for product j , and the Frobenius norm is used to quantify the errors generated by reconstructing U and V. ( 用户 , 商品 ) 集合 R Because many places in the matrix are blank, that is, the user does not rate the product, in this case we do not need to calculate the unknown element, only the observed ( user , product ) set R is calculated .

This turns the collaborative recommendation problem into an optimization problem. U V 相互耦合，这就需要使用交替二乘算法。 U and V are coupled to each other in the objective function , which requires the use of an alternate square algorithm. U 的初始值 U (0) ，这样就将问题转化成了一个最小二乘问题，可以根据 U (0) 可以计算出 V (0) ，再根据 V (0) 计算出 U (1) ，这样迭代下去，直到迭代了一定的次数，或者收敛为止。 That is, first assume the initial value U of U (0) , so that the problem is transformed into a least squares problem. V (0) can be calculated based on U (0) , and U (1 ) , Iterating this way until iterations a certain number of times, or until convergence. Although the global optimal solution for convergence cannot be guaranteed, it has little effect.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}