Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Nhập dữ liệu Auto MPG (hồi quy cơ bản) với TensorFlow


TensorFlow (Google) framework học máy mã nguồn mở, dùng trong nghiên cứu và sản xuất. Cài đặt: pip install tensorflow. Tensor: mảng đa chiều.

Bài toán hồi quy: dự đoán biến liên tục. Dữ liệu Auto MPG: xe hơi thập niên 70-80. Dự đoán MPG.

Chạy trên Google Colab (Python trên trình duyệt, GPU miễn phí).

Ví dụ

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("Phiên bản TensorFlow:", tf.__version__)

url = 'https://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("Đang tải dữ liệu...")
print("Đã định nghĩa tên cột")
raw_dataset = pd.read_csv(url, names=column_names, na_values='?', comment='\t', sep=' ', skipinitialspace=True)
dataset = raw_dataset.copy()

print("Mẫu dữ liệu:")
dataset.head(2)

Nguồn mã: TensorFlow Regression Tutorial

Đầu ra

Phiên bản TensorFlow: 2.4.0
Đang tải dữ liệu...
Đã định nghĩa tên cột
Mẫu dữ liệu
sl.MPGCylindersDisplacementhorsepowerweightAccelerationModel YearOrigin
018.08307.0130.03504.012.0701
115.08350.0165.03693.011.5701

Giải thích

  • Import các package: matplotlib, numpy, pandas, seaborn, tensorflow/keras.
  • Tải dữ liệu từ UCI repository, định nghĩa tên cột, xử lý giá trị thiếu (?).
  • Hiển thị 2 dòng đầu của dataset.