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. | 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 |
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.