1. 数据预处理
数据预处理是机器学习流程中至关重要的一步:
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
# 加载数据集
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 数据标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
发表评论