> For the complete documentation index, see [llms.txt](https://www.nhasachso.io.vn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.nhasachso.io.vn/truc-tuyen/interactive-blocks-1/tac-gia/nguyen-hong-phuong/sach-cong-cu-ai-python/phan-1-xu-ly-du-lieu/scikit-learn-dung-de-tien-xu-ly-du-lieu-chuan-hoa-ma-hoa-chia-tach-du-lieu.md).

# Scikit-learn: Dùng để tiền xử lý dữ liệu (chuẩn hóa, mã hóa, chia tách dữ liệu

#### **Scikit-learn**

**Mục tiêu:** Cung cấp các công cụ mạnh mẽ cho học máy (machine learning) và tiền xử lý dữ liệu.

* Hỗ trợ tiền xử lý: chuẩn hóa (scaling), mã hóa nhãn (label encoding), xử lý giá trị thiếu, chia tập train/test,...
* Cung cấp các thuật toán học máy: hồi quy, phân loại, phân cụm, giảm chiều,...
* Giao diện đơn giản, dễ dùng, tích hợp tốt với NumPy & Pandas.
* Là một trong những thư viện học máy phổ biến nhất trong Python.

**Ví dụ cơ bản:**

```python
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
import pandas as pd

# Tạo dữ liệu mẫu
df = pd.DataFrame({
    'age': [25, 30, 35, 40],
    'income': [3000, 4000, 5000, 6000],
    'label': ['yes', 'no', 'yes', 'no']
})

# Mã hóa nhãn
le = LabelEncoder()
df['label_encoded'] = le.fit_transform(df['label'])

# Chuẩn hóa dữ liệu
scaler = StandardScaler()
df[['age', 'income']] = scaler.fit_transform(df[['age', 'income']])

# Chia tập train/test
X = df[['age', 'income']]
y = df['label_encoded']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
```

***
