如何使用TensorFlow将数据导入以预测Auto MPG数据集(基本回归)中的燃油效率?
TensorFlow是由Google提供的机器学习框架。它是一个开源框架,与Python一起使用以实现算法、深度学习应用等等。它被用于研究和生产目的。
‘tensorflow’软件包可以使用以下代码在Windows上安装 –
pip install tensorflow
Tensor是TensorFlow中使用的数据结构。它帮助连接流程图中的边缘。这个流程图被称为“数据流程图”。张量只是一个多维数组或列表。
回归问题的目的是预测连续或离散变量的输出,比如价格、概率、是否会下雨等等。
我们使用的数据集称为“Auto MPG”数据集。它包含1970年代和1980年代汽车的燃油效率。它包括重量、马力、排量等属性。我们需要使用这个数据集来预测特定车辆的燃油效率。
我们使用Google Colaboratory来运行下面的代码。Google Colab或Colaboratory可以在浏览器上运行Python代码,并且不需要任何配置,并且免费访问GPU(图形处理单元)。Colaboratory构建在Jupyter Notebook之上。
以下是使用Auto MPG数据集预测燃油效率的代码 –
更多Python相关文章,请阅读:Python 教程
例子
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'](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
sl. | MPG | Cylinders | Displacement | horsepower | weight | Acceleration | Model Year | Origin |
---|---|---|---|---|---|---|---|---|
0 | 18.0 | 8 | 307.0 | 130.0 | 3504.0 | 12.0 | 70 | 1 |
1 | 15.0 | 8 | 350.0 | 165.0 | 3693.0 | 11.5 | 70 | 1 |
解释
-
需要导入并别名化所需的包。
-
加载数据,并为它定义列名。
-
在控制台上显示数据集的一个样本。