0% found this document useful (0 votes)
5 views10 pages

Abstract: y F X X X, X, X

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Abstract: y F X X X, X, X

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Abstract parameters of area, number of


bedrooms and distance to the
In Machine Learning, linear centre is available, can we predict
regression is a supervised the price of that house? If so, the
algorithm in which the relation prediction function y=f ( x )what will
between input and output is it look like? Here x=[ x 1 , x 2 , x 3 ] is a
described by a linear function. row vector containing
This algorithm is also known as input information, y is a scalar
linear fitting or linear least representing the output (the price
square(І). This report discusses of the house in this example).
Note on mathematical
the application of linear
notation: in my articles, scalars
regression problems in calculating
are represented by letters written
colour mixing based on L, a, b in non-bold, possibly uppercase,
values. In addition, a comparison for example ( x 1 , N , y , k ). Vectors
is indicated to test the are represented by bold
effectiveness of this algorithm lowercase letters, for example
compared to the previously ( y , x 1 ). Matrices are represented
studied algorithm(ІІ). by bold capital letters, for
example ( X ,Y , W ) .
2. Introduction In the simplest way, we can see
that: i) the larger the house area,
Specifically, this report will reuse the higher the house price; ii) the
the data of the combined colours larger the number of bedrooms,
from Cyan and Medium, Magenta the higher the house price; iii) the
and Medium, Yellow and Medium farther from the centre, the lower
(the change rate is 10% the house price. The simplest
function that can describe the
respectively) as the database for
relationship between house price
the new algorithm (linear
and the 3 input quantities is:
regression) (І) to predict the
percentage of the mass of the y ≈ f ( x )=^y
mixed colours. The results
f ( x )=w1 x1 + w2 x 2 +w 3 x 3 +w 0 ≈ (1 )
returned by the new algorithm
performed on Python are
compared with the old algorithm
In there, ( w1 , w2 , w3 , w0 ) are
performed on Excel(ІІ), hence, the
constants, w 0 also known as bias.
effectiveness of the new
The relationship y ≈ f ( x ) above is a
algorithm was evaluated.
linear relationship. The problem
evaluate the simple example we are working on is a regression
given in the previous post: a large problem. The problem is to find
house x 1 m 2, Have x 2 bedroom and the optimal coefficients y ≈ f ( x )
city centre x 3 km how much does it that's why it's called the Linear
cost? Suppose we have statistics Regression problem.
from 1000 houses in that city, Note 1: y is the actual value
when a new house with of the outcome (based on the
statistics we have in the training beginning is added to simplify the
data set ), while ^y is the value calculation and make it
predicted by the Linear convenient to calculate. Then,
Regression model. In general, y equation (1) can be rewritten as:
[ y ≈ x w= ^y ]
and ^y are two different values
x
Note that is a row vector.
due to model error, however, we
would like this difference to be
very small. 3.2. Forecast error
Note 2: Linear or linear simply
means straight, or flat. In two- We wish that the difference e
dimensional space, a function is between real value y and
called linear if its graph is in the predicted value ^y (pronounced y
form of a straight line. In three- hat in English) is smallest. In
dimensional space, a function is other words, we want the
called linear if its graph is in the following value to be as small as
form of a plane. In a space of possible:
more than 3 dimensions, the 1 2 1 2 1 2
e = ( y− ^y ) = ( y −x w )
concept of a plane is no longer 2 2 2
suitable, instead, another concept 1
in which ( again ) is for
is born 2
called a hyperplane. Linear convenience of calculation (when
functions are the simplest calculating derivatives, the
functions because they are 1
number will be eliminated). We
convenient in visualization and 2
calculation. We will see in the need e 2 because e= y −^y can be a
following articles, linearity is very negative number, speaking e
important and useful in Machine smallest will not be correct
Learning problems. My personal because when e=−∞ is very small
experience shows that, before but the deviation is very large.
understanding non-linear Readers may ask
algorithms, we need to master themselves: why not use the
the techniques for linear models. absolute value |e| but use
square e 2 here? The answer will
be in the next section.
3. Mathematical
analysis 3.3. Loss function
The same happens for all (input,
3.1. Form of Linear
outcome) pairs. ( x i , y i ) , i=1 , 2, … , N ,
Regression with N is the amount of observed
In the equation (1) above, if we data. What we want, the total
T error to be the smallest, is
put w=[ w0 , w1 , w2 , w3 ] is the vector equivalent to finding ( w ) for the
(column) of coefficients that need following function to have a
to be optimized and x=[ 1 , x1 , x 2 , x 3 ] minimum value:
(read as x bar in English) is 1
N
L ( w ) = ∑ ( y i−x i w ) ( 2 )
2
the extended input data vector 2 i=1
(row) . Number(1)at the
Function L ( w ) is called the loss Derivative by ( w ) of the loss
function of the Linear Regression function is:
problem. We always want the loss ∂ L(w )
=X T ( X w− y )
(error) to be minimal, which ∂w
means finding the coefficient
vector w so that the value of this You can refer to the table of
loss function is as small as derivatives of a function in terms
of vectors or matrices in D.2 of
possible. The value of w The point
machinelearingcoban.com. Now I
at which the loss function reaches
would like to return to the
its minimum value is called the
question in the Prediction
optimal point , denoted by:
¿ Error section above about why
[ w =arg min L ( w ) ]
w not use absolute value but use
Before looking for the solution, square. The answer is that the
let's simplify the math in the loss square function has derivatives
function equation(2). Put everywhere, while the absolute
y= [ y 1 ; y 2 ; … ; y N ] is a column vector value function does not (the
containing all the outputs of the derivative is not defined at 0).
training data X =[ x 1 ; x 2 ; … ; x N ] is The derivative equation equal to 0
the input data matrix (extended) is equivalent to X T X w=X T y ≜b ( 4 )
whose each row is a data point. (symbol T
( X y≜ b) means
Then the loss function L ( w ) written T
to put X y equal b).
in simpler matrix form:
N
1 1 Heigh Weigh Heigh Weigh
L ( w ) = ∑ ( y i−x iw ) ] [= | y−X w |22 ( 3 )
2

2 i=1 2 t (cm) t (kg) t (cm) t (kg)


| z |
with 2 is the Euclidean norm (or 147 49 168 60
Euclidean distance), in other 150 50 170 72
words| z |2 is the sum of the squares 153 51 173 63
of each element of the vector. 155 52 175 64
Now, we have a simple form of 158 54 178 66
the loss function written as the 160 56 180 67
equation(3). 163 58 183 68
165 59
If the matrix is square A ≜ X T X
3.4. Solution for Linear non-singular or invertible then the
Regression problem equation (4) has a unique
solution: w= A−1 b.
The most common way to find
So if the matrix A what about
a solution to an optimization
non-invertible (determinant equal
problem (we've known this
to 0)? If you still remember the
since high school) is to solve
knowledge about linear systems
the gradient equation to
of equations, in this case either
zero! Of course, that's when
the equation (4) has no solution,
calculating the derivative and
or it has infinitely many solutions.
solving the gradient equation to
Then we use the concept
zero aren't too complicated.
of pseudoinverse A+¿ ¿(read as A
Luckily, with linear models, both
dagger in English). ( Pseudo
of these are possible.
inverse is a general case of
inversion when the matrix is not We can see that weight is
invertible or even not square. In proportional to height (the taller
the scope of this article, I would the heavier), so we can use Linear
like to skip this part, if you are Regression model for this
really interested, I will write prediction. To check the accuracy
another article just talking about of the found model, we will keep
pseudo inverse. See also: Least the 155 and 160 cm columns for
Squares, Pseudo-Inverses, PCA & testing, the remaining columns
SVD . ) are used to train the model.
With the concept of pseudo-
inverse, the optimal point of the
Linear Regression problem has 4.2. Display data on
the form: graph
T
+ ¿ X y (5 ) ¿ First, we need two
w= A+ ¿=( X X)
T
¿
libraries numpy for linear algebra
and matplotlib for plotting.
4. Example on
# To support both python 2 and python
Python 3

from __future__ import division,


4.1. Problem print_function, unicode_literals

import numpy as np
In this section, I will choose a
simple example of solving Linear import matplotlib.pyplot as plt
Regression problem in Python. I
will also compare the solution of Next, we declare and represent
the problem when solved by the data on a graph.
equation (5) and the solution is
found using Python's scikit-learn # height (cm)
library . This is a widely used
Machine Learning library in X = np.array([[147, 150, 153, 158, 163,
165, 168, 170, 173, 175, 178, 180,
Python ). In this example, the
183]]).T
input data has only 1 value (1
dimension) for the convenience of # weight (kg)
illustration in the plane. y = np.array([[ 49, 50, 51, 54, 58, 59,
We have a data table about 60, 62, 63, 64, 66, 67, 68]]).T
height and weight of 15 people as
below: # Visualize data

plt.plot(X, y, 'ro')
The question is: is it possible to
predict a person's weight based plt.axis([140, 190, 45, 75])
on their height? ( In reality, of
plt.xlabel('Height (cm)')
course not, as weight depends on
many other factors, such as plt.ylabel('Weight (kg)')
volume ). Since this blog is about
plt.show()
simple Machine Learning
algorithms, I'll assume that we
can predict it.
x0 = np.linspace(145, 185, 2)

y0 = w_0 + w_1*x0

# Drawing the fitting line

plt.plot(X.T, y.T, 'ro') # data

plt.plot(x0, y0) # the fitting line

plt.axis([140, 190, 45, 75])


From this graph, we can see that plt.xlabel('Height (cm)')
the data is arranged almost in a
straight line, so the Linear plt.ylabel('Weight (kg)')
Regression model is likely to give plt.show()
good results:
(weight) = w_1*(height) +w_0
w = [[-33.73541021]

[ 0.55920496]]
4.3. Solution according
to formula
Next, we will calculate the
coefficients w_1 and w_0 based on
the formula(5)(5). Note: the
pseudo-inverse of a matrix A in
Python is computed
using numpy.linalg.pinv(A), pinv
which stands for pseudo inverse.
# Building Xbar

one = np.ones((X.shape[0], 1)) From the above graph, we can


see that the red data points are
Xbar = np.concatenate((one, X), axis =
1)
quite close to the blue prediction
line. So the Linear Regression
model works well with
# Calculating weights of the fitting line the training data set. Now, we use
this model to predict the weight
A = np.dot(Xbar.T, Xbar) of two people with heights of 155
b = np.dot(Xbar.T, y) and 160 cm which we did not use
when calculating the solution.
w = np.dot(np.linalg.pinv(A), b)

print('w = ', w) y1 = w_1*155 + w_0

# Preparing the fitting line y2 = w_1*160 + w_0

w_0 = w[0][0]

w_1 = w[1][0]
make any mistake in finding the
print( u'Predict weight of person with
height 155 cm: %.2f (kg), real number:
solution above )
52 (kg)' %(y1) )
Jupyter Notebook source code for
print( u'Predict weight of person with this post.
height 160 cm: %.2f (kg), real number:
56 (kg)' %(y2) )

Predict weight of person with height 155


5. Discussion
cm: 52.94 (kg), real number: 52 (kg)

Predict weight of person with height 160 5.1. Problems that can
cm: 55.74 (kg), real number: 56 (kg)
be solved by Linear
We see that the predicted results Regression
are quite close to the actual data.
Function y ≈ f ( x )=wT x is a linear
function of both w và x . In fact,
4.4. Solution using Linear Regression can be applied
to models that only need to be
scikit-learn library linear in w . For example:
2
Next, we will use Python's scikit- y ≈ w1 x 1+ w2 x2 + w3 x 1 + w4 si n ( x 2 ) +w 5 x 1 x2 + w0
learn library to find the solution. is a linear function of w and thus
can also be solved using Linear
from sklearn import datasets, Regression. For each input data
linear_model x=[ x 1 ; x 2 ] , we calculate new data
x=[ x1 , x2 , x 21 , sin ( x 2 ) , x 1 x 2 ]
~

# fit the model by Linear Regression (pronounced x tilde in English)


and then apply Linear Regression
regr = to this new data.
linear_model.LinearRegression(fit_interc See more examples of Quadratic
ept=False) # fit_intercept = False for
calculating the bias Regression .

regr.fit(Xbar, y)

# Compare two results

print( 'Solution found by scikit-learn : ',


regr.coef_ )

print( 'Solution found by (5): ', w.T)

Solution found by scikit-learn : [[ -


33.73541021 0.55920496]]

Solution found by (5): [[ -33.73541021


0.55920496 ]]

We see that the two results are


the same! ( That means I did not Quadratic Regression
(Source: Quadratic Regression )
- Linear regression for Parapol
5.2. Limitations of
Linear Regression - Reported data have been studied
(ІІ)
The first limitation of Linear
Regression is that it is
very sensitive to noise . In the 7. Motivating
example
between
of
height
the relationship
and weight
Example
above, if there is just This report provides a more efficient
one noisy data pair (150cm, algorithm than the old algorithm that
90kg), the result will be very was studied in the project (ІІ), this is
different. See the figure below: shown in detail in the evaluation
section

8. Method
Method: Applicate Linear regression
algorithm in expecting the
percentage of color element's weight
based on the data set of L, a, b
among Cyan, Magenta, Yellow,
Medium colors which is collected in
So, before performing Linear previously studied research(ІІ).
Regression, the noise ( outliers ) Tool Description: Calculating and
need to be removed. This step is visualization are shown by Python
called pre-processing. programming language instead of
using Excellent in previously studied
research(ІІ).
The second limitation of Linear
Regression is that it cannot
represent complex models . Although
in the above section we saw that this
method can be applied if the
relationship between the outcome
and the input is not necessarily
linear, this relationship is still much
simpler than in real models.
Furthermore, we will ask ourselves:
how to determine the functions
2
( x 1 , sin ( x2 ) , x 1 x 2 ) as above

9. Result
6. Background and
related work 9.1 Cyan and Medium
In this report, readers need to have database
knowledge about:
Bảng a1. Dữ liệu màu pha Cyan và đỏ), kết quả trả về sẽ là một điểm
Medium được dùng là cơ sở dữ liệu thuộc đường cong màu vàng nói trên.
cho đồ án từng nghiên cứu (ІІ)

* Ghi chú: Một hàng giá trị L,a,b thể


hiện cho màu được pha bởi tỉ lệ khối
lượng màu Cyan và Medium ở hàng
tương ứng.
The 3D chart shows the L, a, b values
for each colour between Cyan and
Medium( 10% for each change).
Biểu đồ 3 chiều mô tả dữ liệu L,a,b
cho từng mẫu màu theo pha giữa
Cyan và Medium theo bảng (a1)
6.1.3 Kết quả tính toán
Biểu đồ c1. Kết quả tính toán khi thử
6.1.2 Xử lý dữ liệu bằng Hồi quy các màu pha Cyan và Medium trên
tuyến tính thông qua Python đồ án nghiên cứu trước đó (2) và
Để đơn giản ta sẽ biểu thị thành biểu thực hiện ở báo cáo này, với tỉ lệ khối
đồ L và Chroma với Chroma = lượng Medium lần lượt là 15%, 35%,
sqrt(a*a+b*b). 55%, 75%, 95%, 58%, 72%.

Biểu đồ b1. Kết quả trả về là đường


màu vàng sau khi ứng dụng Linear Biểu đồ d1. Chêch lệch tỉ lệ phần
Regression dự đoán kết quả tỉ lệ trăm khối lượng màu Medium khi thử
phần trăm màu Cyan và Medium. các màu pha Cyan và Medium trên
(biểu đồ L và Chroma theo tỉ lệ đồ án nghiên cứu trước đó (2) và
Medium giảm dần 10%) thực hiện ở báo cáo này, với tỉ lệ khối
lượng Medium lần lượt là 15%, 35%,
55%, 75%, 95%, 58%, 72%.
Giải thích: các màu điểm dữ liệu từ
cơ sở dữ liệu ( các điểm xanh lá) 6.2.1 Cơ sở dữ liệu màu Magenta và
được mô phỏng lại trên đường cong Medium
hồi quy tuyến tính màu vàng, khi dự Bảng a2. Dữ liệu màu pha Magenta
đoán một màu bất kỳ được kết hợp và Medium được dùng là cơ sở dữ
từ màu Cyan và Medium( điểm màu liệu cho đồ án từng nghiên cứu (2)
* Ghi chú: Một hàng giá trị L,a,b thể
hiện cho màu được pha bởi tỉ lệ khối
lượng màu Magenta và Medium ở
hàng tương ứng.
Biểu đồ 3 chiều mô tả dữ liệu L,a,b 6.2.3 Kết quả tính toán
cho từng mẫu màu theo pha giữa
Magenta và Medium theo bảng (a2) Biểu đồ c2. Kết quả tính toán khi thử
các màu pha Magenta và Medium
trên đồ án nghiên cứu trước đó (2) và
thực hiện ở báo cáo này với tỉ lệ khối
lượng Medium lần lượt là 15%, 35%,
55%, 75%, 95%.
6.2.2 Xử lý dữ liệu bằng Hồi quy
tuyến tính thông qua Python
Để đơn giản ta sẽ biểu thị thành biểu
đồ L và Chroma với Chroma =
sqrt(a*a+b*b). Biểu đồ d2. Chêch lệch tỉ lệ phần
trăm khối lượng màu Medium khi thử
Biểu đồ b2. Kết quả trả về là đường
các màu pha Magenta và Medium
màu vàng sau khi ứng dụng Linear
trên đồ án nghiên cứu trước đó (2) và
Regression dự đoán kết quả tỉ lệ
thực hiện ở báo cáo này, với tỉ lệ khối
phần trăm màu Magenta và Medium.
lượng Medium lần lượt là 15%, 35%,
(biểu đồ L và Chroma theo tỉ lệ
55%, 75%, 95%.
Medium giảm dần 10%).

6.3.1 Cơ sở dữ liệu màu Yellow và


Giải thích: các màu điểm dữ liệu từ
Medium
cơ sở dữ liệu ( các điểm xanh lá)
được mô phỏng lại trên đường cong Bảng a3. Dữ liệu màu pha Yellow và
hồi quy tuyến tính màu vàng, khi dự Medium được dùng là cơ sở dữ liệu
đoán một màu bất kỳ được kết hợp cho đồ án từng nghiên cứu (2)
từ màu Magenta và Medium( điểm
màu đỏ), kết quả trả về sẽ là một
điểm thuộc đường cong màu vàng * Ghi chú: Một hàng giá trị L,a,b thể
nói trên. hiện cho màu được pha bởi tỉ lệ khối
lượng màu Yellow và Medium ở hàng
tương ứng.
Biểu đồ 3 chiều mô tả dữ liệu L,a,b
cho từng mẫu màu theo pha giữa
Yellow và Medium theo bảng (a3)
6.3.2 Xử lý dữ liệu bằng Hồi quy 6.3.3 Kết quả tính toán
tuyến tính thông qua Python
Biểu đồ c3. Kết quả tính toán khi thử
Để đơn giản hơn ta biểu thị thành các màu pha Yellow và Medium trên
biểu đồ L và Chroma với Chroma = đồ án nghiên cứu trước đó (2) và
sqrt(a*a+b*b). thực hiện ở báo cáo này với tỉ lệ khối
lượng Medium lần lượt là 15%, 35%,
For simplicity, we represent it as an L
55%, 75%, 95%.
and Chroma chart with Chroma =
sqrt(a*a+b*b).
Biểu đồ d3. Chêch lệch tỉ lệ phần
trăm khối lượng màu Medium khi thử
các màu pha Yellow và Medium trên
Biểu đồ b3. Kết quả trả về là đường đồ án nghiên cứu trước đó (2) và
màu vàng sau khi ứng dụng Linear thực hiện ở báo cáo này, với tỉ lệ khối
Regression dự đoán kết quả tỉ lệ lượng Medium lần lượt là 15%, 35%,
phần trăm màu Yellow và Medium. 55%, 75%, 95%.
(biểu đồ L và Chroma theo tỉ lệ
Medium giảm dần 10%).

Giải thích: các màu điểm dữ liệu từ


cơ sở dữ liệu ( các điểm xanh lá)
được mô phỏng lại trên đường cong
hồi quy tuyến tính màu vàng, khi dự
đoán một màu bất kỳ được kết hợp
từ màu Yellow và Medium( điểm màu
đỏ), kết quả trả về sẽ là một điểm
thuộc đường cong màu vàng nói trên.

Explanation: the data point colors


from the database (green points) are
simulated on the yellow linear
regression curve, when predicting
any color combined from Yellow and
Medium (red points), the returned
result will be a point on the above
yellow curve.

You might also like