فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة Python
أمثلة صغيرة بالـ scikit-learn
الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.
المثال ١: PCA مقابل Kernel PCA مقابل LLE
Sample output (press Run to produce your own): PCA : 0.63 KernelPCA (rbf) : 0.71 LLE (8 neighbors): 0.96
المثال ٢: حساسية LLE لعدد الجيران
Sample output (press Run to produce your own): n_neighbors 8 | accuracy on the unrolled data: 0.96 n_neighbors 12 | accuracy on the unrolled data: 0.81 n_neighbors 20 | accuracy on the unrolled data: 0.46
المثال ٣: t-SNE على digits
Sample output (press Run to produce your own): 2D shape: (300, 2) KNN accuracy on the 2D points (cross-validated): 0.963
اختبار قصير
اختبر فهمك
1. Kernel PCA is useful when:
2. t-SNE is mainly for:
3. A Swiss roll is an example of:
4. What should you usually try first?
5. How do we check that dimensionality reduction helped?
تمارين برمجة
جرّب بإيدك
التمرين 1
جرّب KernelPCA بـ gamma = 0.01 و 0.2 و 0.5 على الـ Swiss roll واطبع الدقة في كل مرة. هل أي gamma يتفوق على الـ PCA العادي؟
Try KernelPCA with gamma = 0.01, 0.2 and 0.5 on the Swiss roll and print the accuracy each time. Does any gamma beat plain PCA?
(output appears here)
from sklearn.datasets import make_swiss_roll
from sklearn.decomposition import PCA, KernelPCA
from sklearn.manifold import LocallyLinearEmbedding
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
import numpy as np
X, t = make_swiss_roll(n_samples=600, noise=0.1, random_state=0)
y = t > np.median(t) # which half of the roll is each point in?
def score(Z):
return round(cross_val_score(LogisticRegression(), Z, y, cv=5).mean(), 2)
for g in (0.01, 0.2, 0.5):
print('gamma', g, score(KernelPCA(n_components=2, kernel='rbf', gamma=g).fit_transform(X)))
Output
gamma 0.01 0.61 gamma 0.2 0.71 gamma 0.5 0.57
التمرين 2
على داتا iris اعمل PCA لـ ٢ components واطبع الـ explained variance لكل واحد.
On iris apply PCA with 2 components and print the explained variance of each.
(output appears here)
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
X, y = load_iris(return_X_y=True)
pca = PCA(n_components=2).fit(X)
for i, v in enumerate(pca.explained_variance_ratio_, 1):
print('component', i, round(float(v), 3))
Output
component 1 0.925 component 2 0.053
مشروع صغير في آخر المحاضرة
شوف مجموعات النبيذ في رسم ٢ بُعد
الداتا: Wine recognition dataset: 178 samples, 13 features, 3 varieties. Source: UCI.
License / source: Ships with scikit-learn; open data from the UCI repository. Dataset page
الخطوات
- حمّل wine واعمل StandardScaler.
- قلّل لـ ٢ بُعد بـ PCA واطبع الـ variance المحفوظ.
- قلّل لـ ٢ بُعد بـ TSNE (perplexity=20, random_state=0).
- في الحالتين درّب KNN بـ cross-validation (cv=5) على النقط الـ ٢D واطبع الدقة.
- قارن: أنهي تقليل حافظ على الفصل بين الأصناف أكتر؟
Expected output (press Run to produce your own): PCA variance kept: 0.554 PCA 2D KNN accuracy : 0.966 t-SNE 2D KNN accuracy: 0.944
مشروع نهاية الفصل
ملف المشروع العملي للفصل 8
ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.
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.