# Lecture: Learning Linear Regression and Machine Learning, python, R ver. 4

Last Updated on November 22, 2021 by shibatau

## I. What do you learn?

We will learn Linear Regression and Machine Learning and think about the different ways of traditional Statistics and Machine Learning to find a regression fit line.

## II. Linear Regression

The scripts are here:

Sample data

Showing the different losses between the two lines put casually.

Finding the best regression fit line.

## III. Least Squares

### 1.Searching regression fit lines with R

https://github.com/thomasp85/gganimate/issues/335

https://stackoverflow.com/questions/59592030/error-the-animation-object-does-not-specify-a-save-animation-method

### 2.Searching regression fit lines interactively

Least Squares Regression Line

## IV. Machine Learning

Refer to the following post for creating Gradient Descent animation with Python

Gradient Descent animation: 1. Simple linear Regression

I have created an animation according to the scripts above on Google Colaboratory. Scripts are here:

via GIPHY

via GIPHY

Splitting the dataset into the training set and the test set, Machine Learning  trains the former and test the latter.

Calculate an R squared value. This is a metric for prediction. The predominant task of Machine Learning is predictive modeling: the creation of models for predicting labels of new examples.

r_sq = regressor.score(X, y) print('coefficient of determination:', r_sq) # coefficient of determination: 0.9565349708076958

The value of R square ranges between [0, 1].
R2= 1- SSres / SStot
Here,
– SSres represents the sum of squares of the residual errors of the data model.
– SStot represents the total sum of the errors.

Higher is the R square value, better is the model and the results.