如何使用 TensorFlow 將資料匯入到 Auto MPG 資料集中以預測燃油效率(基本回歸)?


TensorFlow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用,可以實現演算法、深度學習應用程式等等。它用於研究和生產目的。

可以使用以下程式碼行在 Windows 上安裝 “tensorflow” 包:

pip install tensorflow

張量是 TensorFlow 中使用的一種資料結構。它有助於連線資料流圖中的邊。此資料流圖稱為“資料流圖”。張量只不過是多維陣列或列表。

迴歸問題的目標是預測連續或離散變數的輸出,例如價格、機率、是否會下雨等等。

我們使用的資料集稱為“Auto MPG”資料集。它包含 20 世紀 70 年代和 80 年代汽車的燃油效率。它包括重量、馬力、排量等屬性。透過這些,我們需要預測特定車輛的燃油效率。

我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並可免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。

以下是使用 Auto MPG 資料集預測燃油效率的程式碼:

示例

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

np.set_printoptions(precision=3, suppress=True)

import tensorflow as tf

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
print("The version of tensorflow is ")
print(tf.__version__)

url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data'
column_names = ['MPG', 'Cylinders', 'Displacement', 'Horsepower', 'Weight', 'Acceleration', 'Model Year', 'Origin']
print("The data is being loaded")
print("The column names have been defined")
raw_dataset = pd.read_csv(url, names=column_names, na_values='?', comment='\t', sep=' ', skipinitialspace=True)

dataset = raw_dataset.copy()
print("A sample of the dataset")
dataset.head(2)

程式碼來源 − https://www.tensorflow.org/tutorials/keras/regression

輸出

The version of tensorflow is
2.4.0
The data is being loaded
The column names have been defined
A sample of the dataset


序號MPG氣缸數排量馬力重量加速車型年份產地
018.08307.0130.03504.012.0701
115.08350.0165.03693.011.5701

解釋

  • 匯入併為所需的包設定別名。

  • 載入資料,併為其定義列名。

  • 在控制檯上顯示資料集的樣本。

更新於:2021年1月20日

407 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.