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

Làm sạch dữ liệu Auto MPG để dự đoán hiệu suất nhiên liệu với TensorFlow


TensorFlow (Google) framework học máy mã nguồn mở. 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ụ

print("Bắt đầu làm sạch dữ liệu")
dataset.isna().sum()
dataset = dataset.dropna()
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})
print("Hoàn thành làm sạch!")
dataset = pd.get_dummies(dataset, prefix='', prefix_sep='')
print("Mẫu dữ liệu sau làm sạch:")
dataset.head(4)

Nguồn mã: TensorFlow Regression Tutorial

Đầu ra

Bắt đầu làm sạch dữ liệu
Hoàn thành làm sạch!
Mẫu dữ liệu sau làm sạch -
MPGCylindersDisplacementhorsepowerweightAccelerationModel YearEuropeJapanUSA
018.08307.0130.03504.012.070001
115.08350.0165.03693.011.570001
218.08318.0150.03436.011.070001
316.08304.0150.03433.012.070001

Giải thích

  • Xóa giá trị NaN khỏi dataset.
  • Dùng map() ánh xạ cột Origin: 1→USA, 2→Europe, 3→Japan.
  • One-hot encoding bằng pd.get_dummies() tạo cột Europe, Japan, USA.
  • Hiển thị mẫu 4 dòng đầu sau xử lý.