0Pricing
Machine Learning Academy · درس

إسقاط البيانات وإعادة بنائها من المكونات

سيحوّل المتعلمون مجموعة بيانات إلى فضاء المكونات الرئيسية، ويعرضون الإسقاط ثنائي الأبعاد، ويعيدون بناء السمات الأصلية لقياس فقدان المعلومات.

إسقاط البيانات وإعادة بنائها من المكونات درس مجاني في Machine Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Machine Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Machine Learning Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Projection: From High-D to Low-D

After PCA finds the principal components, projection transforms each data point into the new component space. The projected coordinates are called scores. If you keep only 2 components from 64 original features, each 64-dimensional point becomes a 2-dimensional score. This is achieved by multiplying the centred data matrix by the matrix of eigenvectors (the loadings matrix).

The transform Method in sklearn

In scikit-learn, pca.fit(X) learns the components and pca.transform(X) projects the data. The convenience method pca.fit_transform(X) does both in one call. The result is a matrix of shape (n_samples, n_components) — each row is a point in the reduced space.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_digits

X, y = load_digits(return_X_y=True)  # 1797 x 64
X_scaled = StandardScaler().fit_transform(X)

pca = PCA(n_components=10)
X_reduced = pca.fit_transform(X_scaled)

print('Original shape:', X_scaled.shape)
print('Reduced shape:', X_reduced.shape)
print('Variance retained:', pca.explained_variance_ratio_.sum().round(4))

Visualising the 2D Projection

Projecting to 2 components gives a scatter plot where class separation is often visible even though labels were never used during PCA. This is an important exploratory tool: if classes are well-separated in 2D PCA space, a simple linear classifier may perform well in the full-dimensional space.

import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_digits

X, y = load_digits(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)

pca2 = PCA(n_components=2)
X_2d = pca2.fit_transform(X_scaled)

plt.figure(figsize=(8, 6))
for digit in range(10):
    mask = y == digit
    plt.scatter(X_2d[mask, 0], X_2d[mask, 1], label=str(digit), s=10, alpha=0.6)
plt.legend(title='Digit', bbox_to_anchor=(1, 1))
plt.title('MNIST digits in 2D PCA space')
plt.tight_layout()
plt.show()

Reconstruction: Going Back to Original Space

Reconstruction reverses the projection: multiply the reduced scores by the transpose of the loadings matrix and add back the mean. The result is an approximation of the original data in the original feature space. Perfect reconstruction is only possible if you kept all components; retaining fewer introduces reconstruction error.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_digits
import numpy as np

X, _ = load_digits(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)

pca = PCA(n_components=20)
X_reduced = pca.fit_transform(X_scaled)

# Reconstruct back to 64 dimensions
X_reconstructed = pca.inverse_transform(X_reduced)
print('Reconstruction shape:', X_reconstructed.shape)

# Mean squared reconstruction error
mse = np.mean((X_scaled - X_reconstructed) ** 2)
print(f'MSE: {mse:.4f}')

Visualising Reconstruction Quality

For image data, you can plot original and reconstructed images side by side. With more components retained, the reconstruction looks sharper. With very few components, digits become blurry blobs. This visual comparison is a powerful communication tool for showing stakeholders the trade-off between compression and information loss.

import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_digits

X, _ = load_digits(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)

fig, axes = plt.subplots(3, 5, figsize=(12, 7))
component_counts = [1, 2, 5, 10, 30]

for col, nc in enumerate(component_counts):
    pca = PCA(n_components=nc)
    X_r = pca.inverse_transform(pca.fit_transform(X_scaled))
    # Un-standardise for display (approximate)
    axes[0, col].imshow(X[0].reshape(8, 8), cmap='gray')
    axes[0, col].set_title(f'Original' if col == 0 else '')
    axes[1, col].imshow(X_r[0].reshape(8, 8), cmap='gray')
    axes[1, col].set_title(f'n={nc}')

plt.tight_layout()
plt.show()

Reconstruction Error vs Number of Components

Plot reconstruction MSE against the number of components to see the information-loss curve. This is the quantitative version of the visual comparison. A sharp decrease in MSE as you add the first few components mirrors the scree plot, confirming that most information lives in a small subspace.

import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_digits
import numpy as np

X, _ = load_digits(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)

components = [1, 2, 5, 10, 20, 30, 40, 50, 64]
mse_values = []
for nc in components:
    pca = PCA(n_components=nc)
    X_r = pca.inverse_transform(pca.fit_transform(X_scaled))
    mse_values.append(np.mean((X_scaled - X_r) ** 2))

plt.plot(components, mse_values, marker='o')
plt.xlabel('Number of components')
plt.ylabel('Reconstruction MSE')
plt.title('Information Loss vs Compression')
plt.show()

Interpretting Reconstruction Error

At zero components, reconstruction error equals the total variance of the data. At full components, error is zero. The ratio 1 - explained_variance_ratio.sum() tells you the fraction of variance discarded. For most practical ML pipelines, keeping 95–99% of variance (and discarding 1–5%) loses very little predictive signal while significantly reducing feature count and training time.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_digits

X, _ = load_digits(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)

for nc in [5, 10, 20, 30, 40]:
    pca = PCA(n_components=nc)
    pca.fit(X_scaled)
    retained = pca.explained_variance_ratio_.sum()
    print(f'n_components={nc:2d}  retained={retained:.3f}  discarded={1-retained:.3f}')

Using inverse_transform in Practice

pca.inverse_transform(X_reduced) is a method on the fitted PCA object. It returns the data in the original feature space but with the information from discarded components zeroed out. This is useful for anomaly detection: reconstruct training data and flag points with high reconstruction error as outliers that the PCA model could not represent well.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import numpy as np

# Simulated normal vs anomalous points
X_normal = np.random.randn(100, 10)
X_anomaly = np.random.randn(5, 10) * 10  # far from origin

X_all = np.vstack([X_normal, X_anomaly])
X_scaled = StandardScaler().fit_transform(X_all)

pca = PCA(n_components=5)
X_r = pca.inverse_transform(pca.fit_transform(X_scaled))
errors = np.mean((X_scaled - X_r) ** 2, axis=1)

print('Max error index:', np.argmax(errors), '(anomalies start at index 100)')

Whitening: Decorrelated Components with Unit Variance

Setting PCA(whiten=True) scales the projected scores so each component has unit variance. This removes correlations between components and can improve the performance of algorithms like SVMs or neural networks that are sensitive to feature scale. Whitening is standard preprocessing before training on PCA-reduced features.

from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
import numpy as np

X, _ = load_iris(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)

pca_white = PCA(n_components=3, whiten=True)
X_w = pca_white.fit_transform(X_scaled)

print('Component variances (should be 1.0):', np.var(X_w, axis=0).round(4))

PCA Limitations on Non-Linear Data

PCA finds only linear projections. If data lies on a curved surface — like a Swiss roll — PCA projects onto a flat plane, destroying the manifold structure. In such cases, consider Kernel PCA with an RBF kernel or non-linear alternatives like t-SNE or UMAP for exploration. For model preprocessing, however, linear PCA is usually sufficient and much faster.

Project and Reconstruct: Complete Workflow

A clean PCA pipeline always follows the same pattern: standardise, fit PCA on training data, transform train and test separately, optionally reconstruct to inspect quality.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_digits
import numpy as np

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=0)

scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = scaler.transform(X_test)  # use train scaler

pca = PCA(n_components=0.95)
X_train_r = pca.fit_transform(X_train_s)   # fit only on train
X_test_r = pca.transform(X_test_s)         # transform test

print(f'Reduced: {X_train_r.shape[1]} components from 64 features')

Quick Check

Test your understanding of PCA projection and reconstruction from this lesson.

Lesson Recap

In this lesson you learned: pca.transform projects data into component space with shape (n_samples, n_components), pca.inverse_transform reconstructs data in original feature space with information from discarded components lost, and reconstruction error quantifies information loss and can flag anomalies. Next up we explore t-SNE — a non-linear technique for 2D visualisation of high-dimensional data.

الأسئلة الشائعة

هل درس «إسقاط البيانات وإعادة بنائها من المكونات» مجاني؟

نعم — نص درس «إسقاط البيانات وإعادة بنائها من المكونات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Machine Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Machine Learning Academy 4 دروس في المجموع.

ماذا ستتعلم في «إسقاط البيانات وإعادة بنائها من المكونات»؟

سيحوّل المتعلمون مجموعة بيانات إلى فضاء المكونات الرئيسية، ويعرضون الإسقاط ثنائي الأبعاد، ويعيدون بناء السمات الأصلية لقياس فقدان المعلومات. تتمرن على Machine Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Machine Learning Academy؟

لا تُشترط خبرة سابقة. Machine Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «إسقاط البيانات وإعادة بنائها من المكونات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Machine Learning Academy هذا؟

نعم. كل درس في Machine Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. PCA: التباين والمتجهات الذاتية والمكونات الرئيسية
  2. إسقاط البيانات وإعادة بنائها من المكونات
  3. t-SNE: الحفاظ على الجوار لأغراض التصوير البصري
  4. استخدام PCA للمعالجة المسبقة: تسريع خطوط المعالجة وتقليل الضوضاء
← العودة إلى Machine Learning Academy