🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to AI Notes
Topic #28

Ex2 Data

Example 2 uses the same source code as Example 1.

But, because another dataset is used, the code must collect other data.

Data Collection

The data used in Example 2, is a list of house objects:

{
   "Avg. Area Income": 79545.45857,
   "Avg. Area House Age": 5.682861322,
   "Avg. AreaNumberofRooms": 7.009188143,
   "Avg. Area Number of Bedrooms": 4.09,
   "Area Population": 23086.8005,
   "Price": 1059033.558,
},
{
   "Avg. Area Income": 79248.64245,
   "Avg. Area House Age": 6.002899808,
   "Avg. AreaNumberofRooms": 6.730821019,
   "Avg. Area Number of Bedrooms": 3.09,
   "Area Population": 40173.07217,
   "Price": 1505890.915,
},

The dataset is a JSON file stored at:


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);
}

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:'Rooms', yLabel:'Price',});
}

Want to go beyond the notes?

Join CodingNow 2.0's AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Ex2 Data – FAQs

Quick answers about learning Ex2 Data in AI.

This free note from CodingNow 2.0 explains Ex2 Data in AI — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every AI topic on CodingNow 2.0, including Ex2 Data, is 100% free with no signup required.
With focused practice, most students grasp Ex2 Data in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now