PCA ในการเตรียมข้อมูล: ความเร็วและการลดสัญญาณรบกวนในไปป์ไลน์
ผู้เรียนจะใส่ PCA ไว้ภายใน sklearn Pipeline ก่อนตัวจำแนก และเปรียบเทียบเวลาในการฝึกกับความแม่นยำของชุดทดสอบทั้งกรณีลดมิติและไม่ลดมิติ
PCA ในการเตรียมข้อมูล: ความเร็วและการลดสัญญาณรบกวนในไปป์ไลน์ เป็นบทเรียน Machine Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Machine Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Machine Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
PCA as a Preprocessing Step
Beyond visualisation, PCA serves as a practical preprocessing step that feeds compressed features into a downstream classifier or regressor. By discarding low-variance components that often encode noise, PCA can speed up training, reduce memory usage, and sometimes improve generalisation — especially when the original feature space is very high-dimensional.
Why PCA Can Reduce Noise
Random measurement noise typically spreads across many directions in feature space, but its variance is small in any single direction. PCA concentrates the meaningful signal in the top components and leaves noise in the low-variance tail. When you discard that tail, you effectively denoise the data. This is why PCA pre-processing sometimes helps algorithms like logistic regression that are sensitive to correlated or noisy features.
Embedding PCA in a sklearn Pipeline
The cleanest way to use PCA as preprocessing is inside a Pipeline. The pipeline ensures the scaler and PCA are fitted only on training data and then applied consistently to test data. This eliminates an entire class of data leakage bugs that occur when people forget to apply the same PCA transform to the test set.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
pipe = Pipeline([
('scaler', StandardScaler()),
('pca', PCA(n_components=0.95)),
('clf', LogisticRegression(max_iter=500))
])
pipe.fit(X_train, y_train)
print('Test accuracy:', pipe.score(X_test, y_test).round(4))Comparing Training Time With and Without PCA
On high-dimensional datasets PCA can dramatically reduce training time because the classifier sees far fewer features. Let us benchmark logistic regression on the digits dataset (64 features) with and without PCA pre-reduction.
import time
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# Without PCA
t0 = time.time()
pipe_full = Pipeline([('sc', StandardScaler()), ('clf', LogisticRegression(max_iter=1000))])
pipe_full.fit(X_train, y_train)
t_full = time.time() - t0
# With PCA
t0 = time.time()
pipe_pca = Pipeline([('sc', StandardScaler()), ('pca', PCA(n_components=0.95)),
('clf', LogisticRegression(max_iter=500))])
pipe_pca.fit(X_train, y_train)
t_pca = time.time() - t0
print(f'Without PCA: {t_full:.3f}s acc={pipe_full.score(X_test, y_test):.4f}')
print(f'With PCA: {t_pca:.3f}s acc={pipe_pca.score(X_test, y_test):.4f}')When PCA Helps and When It Does Not
PCA preprocessing helps most when: the number of features is large relative to the number of samples (high-dimensional, low-sample regime), features are correlated (redundant information), or the algorithm is slow with many features (e.g., SVM with RBF kernel). PCA tends NOT to help when: the dataset already has few, informative features, or you are using tree-based models (random forests, XGBoost) that handle redundant features natively and do not benefit from PCA's linear compression.
Tuning n_components in a Grid Search
Because PCA is a step inside a Pipeline, you can tune n_components alongside the classifier's hyperparameters using GridSearchCV. Use the double-underscore notation pca__n_components to refer to the PCA step's parameter. This lets the cross-validation loop find the optimal compression level simultaneously with the model's regularisation strength.
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_digits
X, y = load_digits(return_X_y=True)
pipe = Pipeline([
('sc', StandardScaler()),
('pca', PCA()),
('clf', LogisticRegression(max_iter=500))
])
param_grid = {
'pca__n_components': [10, 20, 30, 40],
'clf__C': [0.1, 1.0, 10.0]
}
grid = GridSearchCV(pipe, param_grid, cv=5, n_jobs=-1)
grid.fit(X, y)
print('Best params:', grid.best_params_)
print('Best CV score:', grid.best_score_.round(4))PCA Before SVM: A Classic Combination
SVMs with RBF kernels compute pairwise distances in the original feature space — expensive for high-dimensional data. Applying PCA first reduces dimensions while retaining signal, shrinking the distance computation. This combination was standard practice on image classification tasks before deep learning dominated: reduce image pixels with PCA, then classify with SVM.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_val_score
import numpy as np
X, y = load_digits(return_X_y=True)
pipe = Pipeline([
('sc', StandardScaler()),
('pca', PCA(n_components=30)),
('svm', SVC(kernel='rbf', C=10, gamma='scale'))
])
scores = cross_val_score(pipe, X, y, cv=5)
print(f'Accuracy: {np.mean(scores):.4f} +/- {np.std(scores):.4f}')PCA for Noise Reduction: Concrete Example
Let us add Gaussian noise to the digits dataset and compare classifier accuracy with and without PCA denoising. On noisy data, PCA often improves accuracy by discarding the noise-dominated low-variance components.
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_val_score
X, y = load_digits(return_X_y=True)
X_noisy = X + np.random.randn(*X.shape) * 5.0 # heavy noise
for nc in [None, 10, 20, 30, 40]:
steps = [('sc', StandardScaler())]
if nc:
steps.append(('pca', PCA(n_components=nc)))
steps.append(('clf', LogisticRegression(max_iter=500)))
pipe = Pipeline(steps)
acc = cross_val_score(pipe, X_noisy, y, cv=5).mean()
label = f'PCA({nc})' if nc else 'No PCA'
print(f'{label:10s} acc={acc:.4f}')Always Fit PCA on Training Data Only
A critical rule: never fit the PCA transform on the test set. Fitting on test data leaks test-set statistics into the preprocessing and gives overly optimistic performance estimates. Using a Pipeline enforces this rule automatically: when you call pipeline.fit(X_train, y_train), every step — including PCA — is fitted only on the training split.
Memory Savings from PCA
On datasets with millions of samples and thousands of features (e.g., text TF-IDF matrices, genomic data), PCA dramatically reduces memory footprint. A 1M×5000 matrix at float32 costs 20 GB; PCA to 100 components gives a 1M×100 matrix at 400 MB — a 50× reduction. For such cases, use IncrementalPCA from scikit-learn, which fits PCA in chunks and never needs to load the full matrix into RAM.
from sklearn.decomposition import IncrementalPCA
import numpy as np
# Simulate large dataset as batches
n_samples, n_features = 10000, 500
n_components = 50
batch_size = 500
ipca = IncrementalPCA(n_components=n_components)
for i in range(0, n_samples, batch_size):
batch = np.random.randn(batch_size, n_features)
ipca.partial_fit(batch)
print('Explained variance ratio sum:', ipca.explained_variance_ratio_.sum().round(4))Combining PCA with ColumnTransformer
In mixed-type datasets, you can apply PCA only to numeric columns while encoding categoricals separately, then combine with a ColumnTransformer. This is an advanced but realistic pattern for production ML pipelines where image features, numerical measurements, and categorical flags all coexist in the same row.
Quick Check
Test your understanding of PCA as pipeline preprocessing from this lesson.
Lesson Recap
In this lesson you learned: PCA inside a Pipeline prevents data leakage by fitting the transform only on training data, PCA can speed up training and reduce noise especially for high-dimensional or correlated feature sets, and n_components can be tuned via GridSearchCV alongside other model hyperparameters using double-underscore notation. Next up we build our first complete scikit-learn Pipeline combining scaler and classifier.
เรียนรู้ Python ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “PCA ในการเตรียมข้อมูล: ความเร็วและการลดสัญญาณรบกวนในไปป์ไลน์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “PCA ในการเตรียมข้อมูล: ความเร็วและการลดสัญญาณรบกวนในไปป์ไลน์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Machine Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Machine Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “PCA ในการเตรียมข้อมูล: ความเร็วและการลดสัญญาณรบกวนในไปป์ไลน์”
ผู้เรียนจะใส่ PCA ไว้ภายใน sklearn Pipeline ก่อนตัวจำแนก และเปรียบเทียบเวลาในการฝึกกับความแม่นยำของชุดทดสอบทั้งกรณีลดมิติและไม่ลดมิติ คุณปฏิบัติ Machine Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Machine Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Machine Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “PCA ในการเตรียมข้อมูล: ความเร็วและการลดสัญญาณรบกวนในไปป์ไลน์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Machine Learning Academy นี้ได้ไหม
ได้ บทเรียน Machine Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- PCA: ความแปรปรวน เวกเตอร์ลักษณะเฉพาะ และองค์ประกอบหลัก
- การฉายข้อมูลและการสร้างข้อมูลกลับจากองค์ประกอบ
- t-SNE: การรักษาความใกล้เคียงสำหรับการแสดงภาพ
- PCA ในการเตรียมข้อมูล: ความเร็วและการลดสัญญาณรบกวนในไปป์ไลน์