Section 1. What is a linear regression?

  • It tries to find a linear function y=wx+b
  • The linear function tries to fit a collection of number pairs, such as house sales, (sqft, sale price)
  • There is no linear function that can accurately fit all number pairs. So, we have to find the 'best' one.
  • By being best, we design a so-called cost function, such as mean square error invented by the great Carl Gauss.
  • This is what machine learning is doing.
    Linear Regression Image

Section 2. The javascript libraries to use

We will use TensorFlow.js to train the model.

Click the 'Run' button below to import two JavaScript libraries.

Output:

Section 3. Using the dataset from Google

We will use a dataset from Google, https://storage.googleapis.com/tfjs-tutorials/carsData.json

Output:

Section 4. Extract the Miles_per_Gallon and Horsepower

We will use only Miles_per_gallon:value and Horsepower:value for our training.

Section 5. Visualize the training dataset

Section 6. Define the model

We will define a TensorFlow sequential model with one input x and one output y.

Section 7. Prepare the training set

We will prepare the extracted data.

  • Convert the extracted data to Tensor
  • Shuffle the data
  • Normalize to the range between 0 and 1 for fast computation

Section 8. Visualize the inputs and labels for training

Section 9. Train the model

Section 10. Generate the dataset for testing

Section 11. Visualize the predictions