Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Example 1 Data


TensorFlow Data Collection

The data used in Example 1, is a list of car objects like this:

{
  "Name": "chevrolet chevelle malibu",
  "Miles_per_Gallon": 18,
  "Cylinders": 8,
  "Displacement": 307,
  "Horsepower": 130,
  "Weight_in_lbs": 3504,
  "Acceleration": 12,
  "Year": "1970-01-01",
  "Origin": "USA"
},
{
  "Name": "buick skylark 320",
  "Miles_per_Gallon": 15,
  "Cylinders": 8,
  "Displacement": 350,
  "Horsepower": 165,
  "Weight_in_lbs": 3693,
  "Acceleration": 11.5,
  "Year": "1970-01-01",
  "Origin": "USA"
},

The dataset is a JSON file stored at:

https://storage.googleapis.com/tfjs-tutorials/carsData.json


Cleaning Data

When preparing for machine learning, it is always important to:

  • Remove the data you don't need
  • Clean the data from errors

Remove Data

A smart way to remove unnecessary data, it to extract only the data you need.

This can be done by iterating (looping over) your data with a map function.

The function below takes an object and returns only x and y from the object's Horsepower and Miles_per_Gallon properties:

function extractData(obj) {
  return {x:obj.Horsepower, y:obj.Miles_per_Gallon};
}


Remove Errors

Most datasets contain some type of errors.

A smart way to remove errors is to use a filter function to filter out the errors.

The code below returns false if on of the properties (x or y) contains a null value:

function removeErrors(obj) {
  return obj.x != null && obj.y != null;
}

Fetching Data

When you have your map and filter functions ready, you can write a function to fetch the data.

async function runTF() {
  const jsonData = await fetch("cardata.json");
  let values = await jsonData.json();
  values = values.map(extractData).filter(removeErrors);
}

Try it Yourself »


Plotting the Data

Here is some code you can use to plot the data:

function tfPlot(values, surface) {
  tfvis.render.scatterplot(surface,
    {values:values, series:['Original','Predicted']},
    {xLabel:'Horsepower', yLabel:'MPG'});
}

Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.