示例 1 資料
TensorFlow 資料收集
示例 1 中使用的資料,是一個汽車物件列表,如下所示:
{
"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"
},
該資料集是一個 JSON 檔案,儲存在:
https://storage.googleapis.com/tfjs-tutorials/carsData.json
Cleaning Data
在為機器學習做準備時,始終重要的是:
- 移除不需要的資料
- 清理資料中的錯誤
移除資料
移除不必要資料的一種聰明方法是**僅提取你需要的資料**。
這可以透過使用 **map 函式** 迭代(迴圈)你的資料來完成。
下面的函式接收一個物件,並僅返回物件中 Horsepower 和 Miles_per_Gallon 屬性的 **x 和 y** 值:
function extractData(obj) {
return {x:obj.Horsepower, y:obj.Miles_per_Gallon};
}
移除錯誤
大多數資料集都包含某種型別的錯誤。
移除錯誤的一種聰明方法是使用 **filter 函式** 來過濾掉錯誤。
如果任一屬性(x 或 y)包含 null 值,下面的程式碼將返回 false:
function removeErrors(obj) {
return obj.x != null && obj.y != null;
}
獲取資料
當你準備好 map 和 filter 函式後,就可以編寫一個函式來獲取資料。
async function runTF() {
const jsonData = await fetch("cardata.json");
let values = await jsonData.json();
values = values.map(extractData).filter(removeErrors);
}
繪製資料
這裡有一些可以用來繪製資料的程式碼:
function tfPlot(values, surface) {
tfvis.render.scatterplot(surface,
{values:values, series:['Original','Predicted']},
{xLabel:'Horsepower', yLabel:'MPG'});
}