فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة Python
أمثلة صغيرة بالـ scikit-learn
الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.
المثال ١: PCA على iris
Sample output (press Run to produce your own): variance kept by each component: [0.925 0.053] total kept with 2 components : 0.978 first flower in 2D: [[-2.68 0.32]]
المثال ٢: كام بُعد نحتاج؟
Sample output (press Run to produce your own): original dimensions: 64 components for 95% variance: 29 components for 99% variance: 41
المثال ٣: ضغط وفك الضغط
Sample output (press Run to produce your own): shape before: (1797, 64) -> after: (1797, 29) average reconstruction error: 0.849
اختبار قصير
اختبر فهمك
1. The curse of dimensionality means:
2. PCA picks the first direction where the data:
3. PCA(n_components=0.95) keeps:
4. Why scale the data before PCA?
5. inverse_transform in PCA:
تمارين برمجة
جرّب بإيدك
التمرين 1
على داتا wine اعمل StandardScaler ثم PCA بـ 0.90 واطبع عدد الـ components اللي اتختارت.
On the wine data apply StandardScaler then PCA with 0.90 and print how many components were chosen.
(output appears here)
from sklearn.datasets import load_wine
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
X, y = load_wine(return_X_y=True)
Xs = StandardScaler().fit_transform(X)
pca = PCA(n_components=0.90).fit(Xs)
print('components:', pca.n_components_)
print('variance kept:', round(pca.explained_variance_ratio_.sum(), 3))
Output
components: 8 variance kept: 0.92
التمرين 2
درّب KNN على digits الأصلية وعلى digits بعد PCA(0.95) وقارن الدقة.
Train KNN on the original digits and on the digits after PCA(0.95) and compare the accuracy.
(output appears here)
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
X, y = load_digits(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0, stratify=y)
print('raw :', round(KNeighborsClassifier().fit(X_tr, y_tr).score(X_te, y_te), 3))
pca = PCA(n_components=0.95).fit(X_tr)
knn = KNeighborsClassifier().fit(pca.transform(X_tr), y_tr)
print('PCA :', round(knn.score(pca.transform(X_te), y_te), 3), '| dims:', pca.n_components_)
Output
raw : 0.978 PCA : 0.975 | dims: 28
مشروع صغير في آخر المحاضرة
اضغط الداتا من ٦٤ بُعد لأقل من النص
الداتا: Optical Recognition of Handwritten Digits: 1,797 images, 64 pixels each. Source: UCI.
License / source: Ships with scikit-learn; open data from the UCI repository. Dataset page
الخطوات
- حمّل digits وقسّمه ٨٠/٢٠.
- درّب SVC على الداتا الأصلية واطبع الدقة والوقت التقريبي (عدد الـ features).
- اعمل PCA(0.95) على الـ training بس.
- درّب نفس الـ SVC على الداتا المضغوطة واطبع الدقة وعدد الأبعاد.
- اشرح: خسرنا إيه وكسبنا إيه؟
Expected output (press Run to produce your own): raw: dims 64 accuracy 0.983 PCA: dims 28 accuracy 0.989
Credits: topic order follows the public chapter list of Aurélien Géron's book "Hands-On Machine Learning with Scikit-Learn, Keras and TensorFlow" (3rd ed.). All explanations and code here are original.