معمل تعلّم الآلة - الأسبوع 1 (خوارزمية KNN)
29 دقيقة قراءة
شرائح الدرس
1 / 14
جامعة المنصورة
- الفصل الدراسي الأول - 2023-2024
- قسم علوم الحاسب
- كلية الحاسبات والمعلومات
- تعلّم الآلة - الأسبوع 1
- مريم عصام
أساسيات تعلّم الآلة
خطة الفصل الدراسي المبدئية
- 01
- أساسيات تعلّم الآلة
- الانحدار الخطي (Linear Regression) والانحدار اللوجستي (Logistic Regression)
- 04
- تصنيف الجيران الأقرب K (K-Nearest Neighbours Classification)
- 02
- تقييم النموذج واختياره (Model Evaluation & Selection)
- 03
- آلات المتجهات الداعمة (Support Vector Machines)
- 05
- أشجار القرار (Decision Trees)
- 06
- مصنّفات بايز الساذج (Naive Bayes Classifiers)
- 07
- الشبكات العصبية (Neural networks)
- 08
4
- البرمجة التقليدية
- برمجة تعلّم الآلة
ما هو تعلّم الآلة (ML)؟
- دراسة برامج الحاسوب (الخوارزميات) القادرة على التعلّم بالمثال
- يمكن لخوارزميات تعلّم الآلة أن تعمّم (Generalize) من أمثلة موجودة لمهمة ما
- مثال: بعد رؤية مجموعة تدريب من صور مُصنَّفة (Labeled)، يمكن لمصنِّف الصور أن يكتشف كيفية تطبيق التصنيفات بدقة على صور جديدة لم يرها من قبل
- يجمع تعلّم الآلة بين الإحصاء وعلوم الحاسب وأكثر..
- 5
تعلّم الآلة (ML)
- تتعلّم الخوارزميات القواعد من أمثلة مُصنَّفة (بيانات التدريب)
- يجب أن تكون القواعد المتعلَّمة قادرة أيضًا على التعميم للتعرّف على أمثلة جديدة غير موجودة في مجموعة التدريب أو التنبؤ بها بشكل صحيح.
- 6
تعلّم الآلة مقابل تعلّم الآلة التطبيقي
- يهتم تعلّم الآلة الخالص (Pure ML) في الغالب بسلوك الخوارزمية ككل
- تعلّم الآلة (ML)
- يهتم تعلّم الآلة التطبيقي (Applied ML) بالسلوك على مجموعات بيانات محدَّدة من واقع الحياة.
- تعلّم الآلة التطبيقي (Applied ML)
8
- تعلّم الآلة لاكتشاف الاحتيال
- خمّن: ما نوع تعلّم الآلة المستخدَم في اكتشاف الاحتيال؟
أنواع مسائل تعلّم الآلة
- التعلّم الخاضع للإشراف (Supervised learning):
- تعلّم التنبؤ بالقيم المستهدفة من بيانات مُصنَّفة
- التصنيف (Classification) (القيم المستهدفة فئات منفصلة - Discrete)
- اكتشاف الاحتيال
- اكتشاف البريد العشوائي (Spam) في البريد الإلكتروني
- التشخيص
- تصنيف الصور
- الانحدار (Regression) (القيم المستهدفة قيم مستمرة - Continuous)
- تقييم المخاطر
- التنبؤ بالدرجات
- التعلّم غير الخاضع للإشراف (Unsupervised learning):
- إيجاد بنية في بيانات غير مُصنَّفة
- التجميع (Clustering)
- إيجاد مجموعات من الحالات المتشابهة في البيانات
- اكتشاف القيم الشاذة (Outlier detection)
- إيجاد أنماط غير معتادة
- التعلّم المعزَّز (Reinforcement learning):
- يمكن لبعض الخوارزميات التعامل مع بيانات تدريب مُصنَّفة جزئيًا، عادةً كمية كبيرة من البيانات غير المُصنَّفة وكمية صغيرة من البيانات المُصنَّفة.
10
- تعلّم الآلة (ML)
- مثال: بكسلات الصورة، مع مصنِّف الجيران الأقرب K
- مثال: نسبة التنبؤات الصحيحة على مجموعة الاختبار
- مثال: جرّب نطاقًا من القيم
- لمعامل "k" في مصنِّف الجيران الأقرب K
11
- تمثيلات السمات (Feature Representations)
12
- بايثون (Python)
- اختُرعت في الثمانينيات
- بايثون تطوّر للغة برمجة ABC، وتأثّرت بلغات البرمجة C وIcon وModula-3.
- تستخدم المسافات البادئة (Indentation) كخاصية نحوية (Syntactic)
- تشجّع بايثون على النمطية (Modularity) وإعادة استخدام الكود.
بايثون - لماذا؟
- 01
- كود قابل للقراءة وسهل الصيانة
- ستساعدك قاعدة كود واضحة وقابلة للقراءة على صيانة البرنامج وتحديثه دون بذل وقت وجهد إضافيَّين
- توافق مع المنصات والأنظمة الرئيسية
- في الوقت الحالي، تدعم بايثون العديد من أنظمة التشغيل. يمكنك حتى استخدام مفسِّرات بايثون (Interpreters) لتشغيل الكود على منصات وأدوات محدَّدة. كما أن بايثون لغة برمجة مفسَّرة (Interpreted)، ما يتيح لك تشغيل نفس الكود على منصات متعددة دون إعادة ترجمة (Recompilation)
- 02
- تعدد أنماط البرمجة (Multiple Programming Paradigms)
- تدعم بايثون البرمجة كائنية التوجه والبرمجة الهيكلية بشكل كامل. كما تدعم ميزات اللغة مفاهيم متنوعة في البرمجة الوظيفية (Functional) والبرمجة الموجّهة بالجوانب (Aspect-oriented). وفي الوقت نفسه، تتميّز بايثون أيضًا بنظام أنواع ديناميكي وإدارة تلقائية للذاكرة.
- 03
- العديد من الأطر والأدوات مفتوحة المصدر
- 04
بايثون - لماذا؟
- تبسيط تطوير البرمجيات المعقّدة
- بايثون لغة برمجة عامة الغرض. لذا يمكنك استخدامها لتطوير تطبيقات سطح المكتب والويب على حد سواء. كما يمكنك استخدام بايثون لتطوير تطبيقات علمية وعددية معقّدة. صُمِّمت بايثون بميزات تسهّل تحليل البيانات وتصويرها (Visualization). يمكنك الاستفادة من ميزات تحليل البيانات في بايثون لإنشاء حلول بيانات ضخمة (Big data) مخصَّصة دون بذل وقت وجهد إضافيَّين. وفي الوقت نفسه،
- 05
- تمتلك بايثون مكتبات قوية مهمة جدًا مثل numpy وPandas وMatplotlib..
- 06
Anaconda هو مدير حزم (Package manager) ومدير بيئات (Environment manager) مجاني وسهل التثبيت، وتوزيعة بايثون، ومجموعة تضم أكثر من 720 حزمة مفتوحة المصدر توفّر دعمًا مجتمعيًا مجانيًا. يمكن تحميل Anaconda مجانًا من https://docs.continuum.io/anaconda/
- 15
16
- إذن في الخلاصة، بايثون
- تستخدم التنميط الديناميكي (Dynamic Typing) (يقوم المفسِّر بتعيين نوع لجميع المتغيرات وقت التشغيل)
- صياغة نحوية بسيطة
- مفسَّرة (Interpreted)
- كائنية التوجه (في إصدارات بايثون المبكرة لم تكن تدعم التغليف - Encapsulation، لكنها تدعمه الآن)
- _ متغير محمي (Protected)
- __ متغير خاص (Private)
17
- الوحدات (Modules) والحزم (Packages)
- الوحدات (Modules) | الحزم (Packages)
- ملف بايثون واحد يمكن استيراده (Import) في وحدة أخرى | مجموعة من الوحدات منظَّمة في تراتبية مجلدات (Directory hierarchy)
- يمكن استيراد الوحدة مباشرة باستخدام الكلمة المفتاحية import متبوعة باسم الوحدة.
- import module_name | تُستورد الحزمة باستخدام الكلمة المفتاحية import متبوعة باسم الحزمة، ويمكنك الوصول إلى الوحدة والحزم الفرعية داخل اسم الحزمة باستخدام تدوين النقطة (Dot notation).
- import package_name.module_name
- math، وrandom، وos، وdatetime، وcsv | Numpy، وPandas، وMatplotlib
18
- scikit-learn | مكتبة تعلّم الآلة في بايثون
- from sklearn.model_selection import train_test_split
- from sklearn.tree import DecisionTreeClassifier
- مكتبة SciPy: أدوات الحوسبة العلمية | توفّر مجموعة متنوعة من أدوات الحوسبة العلمية المفيدة، بما في ذلك التوزيعات الإحصائية، ومجموعة متنوعة من الدوال الرياضية المتخصصة.
- • مع scikit-learn، توفّر SciPy دعمًا للمصفوفات المتناثرة (Sparse matrices)، وهي طريقة لتخزين جداول كبيرة تتكوّن في معظمها من أصفار.
- import scipy as sp
- NumPy: مكتبة الحوسبة العلمية | توفّر بنى البيانات الأساسية التي تستخدمها scikit-learn، وخاصة المصفوفات متعددة الأبعاد.
- • عادةً، تكون البيانات المُدخَلة إلى scikit-learn على شكل مصفوفة NumPy.
- import numpy as np
- Pandas: معالجة البيانات وتحليلها | توفّر بنى بيانات أساسية مثل DataFrame
- • كما تدعم قراءة/كتابة البيانات بتنسيقات مختلفة
- import pandas as pd
- matplotlib: التصوير البياني (Visualization) | import matplotlib.pyplot as plt
19
20
21
22
23
- Code example : Object recognition system
- Dataset : The fruit dataset
- https://github.com/susanli2016/Machine-Learning-with-Python/blob/22c63e943461dab8acdd02bac45710959a90ef65/fruit_data_with_colors.txt
24
25
26
27
28
29
30
31
أي أسئلة؟
- التالي....
- 32
جامعة المنصورة
- الفصل الدراسي الأول - 2023-2024
- قسم علوم الحاسب
- كلية الحاسبات والمعلومات
- تعلّم الآلة - الأسبوع 1
- مريم عصام
تصنيف الجيران الأقرب K (K-Nearest Neighbours Classification)
خطة الفصل الدراسي المبدئية
- 01
- أساسيات تعلّم الآلة
- الانحدار الخطي (Linear Regression) والانحدار اللوجستي (Logistic Regression)
- 04
- تصنيف الجيران الأقرب K (K-Nearest Neighbours Classification)
- 02
- تقييم النموذج واختياره (Model Evaluation & Selection)
- 03
- آلات المتجهات الداعمة (Support Vector Machines)
- 05
- أشجار القرار (Decision Trees)
- 06
- مصنّفات بايز الساذج (Naive Bayes Classifiers)
- 07
- الشبكات العصبية (Neural networks)
- 08
4
- خوارزمية مصنِّف الجيران الأقرب K (k-Nearest Neighbor - k-NN)
- تعلّم خاضع للإشراف قائم على الحالات/الذاكرة (Instance/memory based)
- بالنظر إلى مجموعة تدريب X_train بتصنيفاتها y_train، وحالة جديدة x_test نريد تصنيفها:
- أوجد الحالات الأكثر تشابهًا (لنسمّها X_NN) مع x_test من بين الحالات الموجودة في X_train.
- احصل على التصنيفات y_NN الخاصة بحالات X_NN
- تنبّأ بتصنيف x_test من خلال دمج التصنيفات y_NN، مثلاً بالتصويت بالأغلبية البسيطة
5
- خوارزمية مصنِّف الجيران الأقرب K (k-Nearest Neighbor - k-NN)
- نقطة الاستعلام (Query point)
- (فضاء السمات - Feature space)
- حدود القرار (Decision boundary)
6
- تحتاج خوارزمية KNN إلى 4 أشياء
- مقياس مسافة (Distance metric)
- كم عدد الجيران "الأقرب" الذي يجب النظر إليه؟
- دالة ترجيح اختيارية (Optional weighting function) لنقاط الجيران
- طريقة لتجميع فئات نقاط الجيران
7
- تحتاج خوارزمية KNN إلى 4 أشياء
8
- المفاضلة بين التحيّز والتباين (Bias-variance trade off)
- أقل حساسية للضجيج (Noise)، لكن حدود أقل تفصيلاً
9
10
- النموذج/المقدِّر (Model/Estimator): يُنتج ملاءمة النموذج (Model fitting) "نموذجًا مدرَّبًا".
- – التدريب هو عملية تقدير معاملات النموذج (Model parameters).
11
التعلّم الآلي الخاضع للإشراف
13
- تمثيل السمات (Feature representation)
- X: حالات البيانات، العيّنات، الأمثلة
- y: القيمة المستهدفة
- تُستخدَم لأغراض بشرية
التعميم، والإفراط في الملاءمة، والقصور في الملاءمة (Generalization, Overfitting, and Underfitting)
- 14
- على سبيل المثال، عند k = 1، ستُعتبَر النقطة الأقرب لنقطة البيانات المعنيّة فقط.
- هنا، قد يكون التنبؤ دقيقًا لتلك النقطة تحديدًا، لذا يكون خطأ التحيّز (Bias error) أقلّ.
- لكن خطأ التباين (Variance error) سيكون مرتفعًا لأنه لا تُعتبَر إلا النقطة الأقرب الواحدة، ولا يأخذ ذلك بعين الاعتبار النقاط المحتملة الأخرى. هذا يعني أن نموذجنا يعاني من الإفراط في الملاءمة (Overfitting).
- من ناحية أخرى، عند قيم أعلى لـk، ستُعتبَر نقاط أكثر قربًا من نقطة البيانات المعنيّة. هذا سينتج عنه خطأ تحيّز أعلى وقصور في الملاءمة (Underfitting) لأن نقاطًا أكثر قربًا من نقطة البيانات تُعتبَر، وبالتالي لا يستطيع النموذج تعلّم التفاصيل الدقيقة من مجموعة التدريب. لكن يمكننا الحصول على خطأ تباين أقل لمجموعة الاختبار ذات القيم غير المعروفة.
- تشير القدرة على التعميم (Generalization ability) إلى قدرة الخوارزمية على إعطاء تنبؤات دقيقة لبيانات جديدة لم تُرَ من قبل.
15
16
- التحيّز (bias)
- التحيّز (Bias): الفرق بين تنبؤ نموذج تعلّم الآلة بالقيم والقيمة الصحيحة
- التحيّز المرتفع = خطأ كبير في بيانات التدريب وكذلك بيانات الاختبار، ويعاني النموذج من قصور في الملاءمة
17
- التباين (variance)
- التباين (Variance): تباين تنبؤ النموذج لنقطة بيانات معينة، وهو ما يخبرنا بمدى تشتّت بياناتنا
- التباين المرتفع = تؤدّي النماذج أداءً جيدًا جدًا على بيانات التدريب لكن بمعدلات خطأ مرتفعة على بيانات الاختبار، ويعاني النموذج من الإفراط في الملاءمة
- (التباين هو مقدار تغيّر تقدير الدالة المستهدفة إذا استُخدِمت بيانات تدريب مختلفة)
مجموعات بيانات التعلّم الخاضع للإشراف
- يمكن إنشاء مجموعات بيانات اصطناعية وحقيقية متنوعة أو استخدامها مباشرة.
- مجموعة بيانات الفاكهة مثال على مجموعة بيانات تصنيف متعدد الفئات (Multi-class Classification)
- 18
- https://scikit-learn.org/stable/datasets.html#:~:text=The%20sklearn.,from%20the%20'real%20world'.
الجيران الأقرب K: التصنيف والانحدار
- 19
- يمكن استخدام KNN لكل من التصنيف والانحدار
20
- يعتمد حساب model.score(X,y) في Sklearn على معامل التحديد (Coefficient of determination) أي R^2
- تكون الدرجة بين 0 و1: - القيمة 0 تقابل نموذجًا ثابتًا يتنبأ بمتوسط جميع القيم المستهدفة في التدريب. - القيمة 1 تقابل تنبؤًا مثاليًا
- العدد الافتراضي = 5
التحقق المتقاطع (Cross-Validation)
- 21
- • يستخدم تقسيمات تدريب-اختبار متعددة، وليس تقسيمًا واحدًا فقط، وكل تقسيم يُستخدَم لتدريب نموذج منفصل وتقييمه
- • لماذا هذا أفضل؟
- يمكن أن تتفاوت درجة الدقة لطريقة تعلّم خاضع للإشراف، اعتمادًا على العيّنات التي ينتهي بها الأمر في مجموعة التدريب.
- استخدام تقسيمات تدريب-اختبار متعددة يعطي تقديرات أكثر استقرارًا وموثوقية لكيفية أداء المصنِّف في المتوسط على الأرجح.
- تُحسَب النتائج كمتوسط عبر مجموعات تدريب مختلفة متعددة بدلاً من الاعتماد على نموذج واحد مدرَّب على مجموعة تدريب معينة.
22
- التحقق المتقاطع k-Fold (k-Fold Cross-Validation)
- في التصنيف تستخدم sklearn
- في الانحدار تستخدم sklearn
23
24
25
- باستخدام نفس المثال (عن KNN)، عدّل الكود المكتوب بحيث تأخذ K نطاق قيم من 1 إلى 15 لنفس المسألة، وسجّل الدقة لكل قيمة k
- حدّد أي قيمة لـk ستتفوّق على القيم الأخرى.
- التكليف 1
- كيف تسلّمه؟
- Google Classroom
- https://classroom.google.com/c/NjMxOTI0NzkyNzc2?cjc=mxqfokp
26
أي أسئلة؟
- التالي....
- 27
مقدمة في التعلّم الآلي الخاضع للإشراف (Supervised Machine Learning)
%matplotlib notebook
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# first let's bring in some datasets from scikitlearn
from sklearn.datasets import make_classification, make_blobs
from matplotlib.colors import ListedColormap
from sklearn.datasets import load_breast_cancer
# here we define a colormap for the figures
cmap_bold = ListedColormap(['#FFFF00', '#00FF00', '#0000FF', '#000000'])
# and let's bring in a tool to make regression
from sklearn.datasets import make_regression
plt.figure()
plt.title('Sample regression problem with one input varible')
# now we create a scattered points sample which has one informarive
# input variable
X_R1, y_R1 = make_regression(n_samples=100, n_features=1,
n_informative=1, bias=150.0,
noise=30, random_state=0)
# and we plot the sample where x-axes shows the feature value, and
# y-axes shows the regression target
plt.scatter(X_R1, y_R1, marker='o', s=50)
plt.show()<IPython.core.display.Javascript object><IPython.core.display.HTML object># another similar example could be the following, where we use a
# different dataset
from sklearn.datasets import make_friedman1
plt.figure()
# we create a sample again
plt.title('Complex regression problem with one input variable')
X_F1, y_F1 = make_friedman1(n_samples=100, n_features=7,
random_state=0)
# and we plot it, as well as previous figure here x-axes and y-axes
# keep the same mean
plt.scatter(X_F1[:, 2], y_F1, marker='o', s=50)
plt.show()<IPython.core.display.Javascript object><IPython.core.display.HTML object># Otherwise, to illustrate binary classification we will include two
# class dataset with 2 informatice features
plt.figure()
plt.title('Sample binary classification problem with two informative features')
X_C2, y_C2 = make_classification(n_samples=100, n_features=2,
n_redundant=0, n_informative=2,
n_clusters_per_class=1, flip_y=0.1,
class_sep=0.5, random_state=0)
# Here both axes show every of two features, and the color shows which
# class that data instance is labeled
plt.scatter(X_C2[:, 0], X_C2[:, 1], c=y_C2, marker='o',
s=50, cmap=cmap_bold)
plt.show()<IPython.core.display.Javascript object><IPython.core.display.HTML object># another example of this
X_D2, y_D2 = make_blobs(n_samples = 100, n_features = 2, centers = 8,
cluster_std = 1.3, random_state = 4)
y_D2 = y_D2 % 2
plt.figure()
# but in this case we have non-linear data
plt.title('Sample binary classification problem with non-linearly separable classes')
# this data gathers in different clusters
plt.scatter(X_D2[:,0], X_D2[:,1], c=y_D2,
marker= 'o', s=50, cmap=cmap_bold)
plt.show()<IPython.core.display.Javascript object><IPython.core.display.HTML object>تصنيف الجيران الأقرب K (K-Nearest Neighbors Classification)
# let's bring in our tools to clasify and plot
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from adspy_shared_utilities import plot_two_class_knn
# splitting data
X_train, X_test, y_train, y_test = train_test_split(X_C2, y_C2,
random_state=0)
# fisrt we plot data using k=1, namely a sole nightbor
plot_two_class_knn(X_train, y_train, 1, 'uniform', X_test, y_test)
# and then we plot it using k=3 and k=11
plot_two_class_knn(X_train, y_train, 3, 'uniform', X_test, y_test)
# we can appreciate how data is overfitting when k=1, and this
# improves when k increase
plot_two_class_knn(X_train, y_train, 11, 'uniform', X_test, y_test)<IPython.core.display.Javascript object><IPython.core.display.HTML object><IPython.core.display.Javascript object><IPython.core.display.HTML object><IPython.core.display.Javascript object><IPython.core.display.HTML object>انحدار الجيران الأقرب K (K-Nearest Neighbors Regression)
يمكن أن تكون خوارزمية الجيران الأقرب K مفيدة ليس فقط للتصنيف، بل أيضًا لإجراء الانحدار (Regression)
# let's bring in the tools we need
from sklearn.neighbors import KNeighborsRegressor
# we are going to use the first regression dataset
X_train, X_test, y_train, y_test = train_test_split(X_R1, y_R1, random_state=0)
knnreg = KNeighborsRegressor(n_neighbors=5).fit(X_train, y_train)
print(knnreg.predict(X_test))
# R-squared value measure how well a prediction model for regression
# fits the given data
print('R-squared test score: {:.3f}'.format(knnreg.score(X_test, y_test)))[231.70974697 148.35572605 150.58852659 150.58852659 72.14859259
166.50590948 141.90634426 235.57098756 208.25897836 102.10462746
191.31852674 134.50044902 228.32181403 148.35572605 159.16911306
113.46875166 144.03646012 199.23189853 143.19242433 166.50590948
231.70974697 208.25897836 128.01545355 123.14247619 141.90634426]
R-squared test score: 0.425# let's plot how the regression adjust to training data
fig, subaxes = plt.subplots(1, 2, figsize=(8,4))
X_predict_input = np.linspace(-3, 3, 50).reshape(-1,1)
X_train, X_test, y_train, y_test = train_test_split(X_R1[0::5], y_R1[0::5], random_state = 0)
# This's gonna plot training data and predicted data
# training data is depicted by orange circles
# and predicted data by blue triangles
for thisaxis, K in zip(subaxes, [1, 3]):
knnreg = KNeighborsRegressor(n_neighbors = K).fit(X_train, y_train)
y_predict_output = knnreg.predict(X_predict_input)
thisaxis.set_xlim([-2.5, 0.75])
thisaxis.plot(X_predict_input, y_predict_output, '^', markersize = 10,
label='Predicted', alpha=0.8)
thisaxis.plot(X_train, y_train, 'o', label='True Value', alpha=0.8)
thisaxis.set_xlabel('Input feature')
thisaxis.set_ylabel('Target value')
thisaxis.set_title('KNN regression (K={})'.format(K))
thisaxis.legend()
# we can appreciate when k=1 the plot is overfitting, on the other
# hand when k increases the plot take a general behavior
plt.tight_layout()<IPython.core.display.Javascript object><IPython.core.display.HTML object># so we can measure how well each of this regression has predicted
# the true values
# plot k-NN regression on sample dataset for different values of K
fig, subaxes = plt.subplots(5, 1, figsize=(5,20))
X_predict_input = np.linspace(-3, 3, 500).reshape(-1,1)
X_train, X_test, y_train, y_test = train_test_split(X_R1, y_R1,
random_state = 0)
# as well as last figures, circules depicts true values and triangles
# depicts the prediction made on true value according to the
# number of neighbors
for thisaxis, K in zip(subaxes, [1, 3, 7, 15, 55]):
knnreg = KNeighborsRegressor(n_neighbors = K).fit(X_train, y_train)
y_predict_output = knnreg.predict(X_predict_input)
train_score = knnreg.score(X_train, y_train)
test_score = knnreg.score(X_test, y_test)
# here we plot the predicted values by a line
thisaxis.plot(X_predict_input, y_predict_output)
# here we plot the training and testing data
thisaxis.plot(X_train, y_train, 'o', alpha=0.9, label='Train')
thisaxis.plot(X_test, y_test, '^', alpha=0.9, label='Test')
thisaxis.set_xlabel('Input feature')
thisaxis.set_ylabel('Target value')
thisaxis.set_title('KNN Regression (K={})\n\
Train $R^2 = {:.3f}$, Test $R^2 = {:.3f}$'
.format(K, train_score, test_score))
thisaxis.legend()
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
# we can see how well prediction line fits on true values when the
# the number of neighbors stays into a range. For instance, when
# k is low (e.g., k=1) r-squared value is low as well, but when the
# value of k increases the r-squared does as well. However, when this
# values is high the line turns out to be over-training, and the
# r-squared value drops<IPython.core.display.Javascript object><IPython.core.display.HTML object>الانحدار الخطي: المربعات الصغرى (Linear Regression: Least-Squares)
# We also have the ordinary regression, i.e., the linear regression,
# which use least-squared to fit the training data
from sklearn.linear_model import LinearRegression
# again we use first data
X_train, X_test, y_train, y_test = train_test_split(X_R1, y_R1,
random_state = 0)
linreg = LinearRegression().fit(X_train, y_train)
print('linear model coeff (w): {}'
.format(linreg.coef_))
print('linear model intercept (b): {:.3f}'
.format(linreg.intercept_))
print('R-squared score (training): {:.3f}'
.format(linreg.score(X_train, y_train)))
print('R-squared score (test): {:.3f}'
.format(linreg.score(X_test, y_test)))linear model coeff (w): [45.70870465]
linear model intercept (b): 148.446
R-squared score (training): 0.679
R-squared score (test): 0.492# so if we plot it
plt.figure(figsize=(5,4))
# here we plot all data, not only training data
plt.scatter(X_R1, y_R1, marker= 'o', s=50, alpha=0.8)
# and we'll have that the regrasion is given by the linear equation
# y = m*x+b
plt.plot(X_R1, linreg.coef_ * X_R1 + linreg.intercept_, 'r-')
plt.title('Least-squares linear regression')
plt.xlabel('Feature value (x)')
plt.ylabel('Target value (y)')
plt.show()<IPython.core.display.Javascript object><IPython.core.display.HTML object>الانحدار الخطي: Ridge وLasso والانحدار متعدد الحدود (Polynomial Regression)
# first, Riged Regression put a penalty value to previous linear regression
# let's use a database on crime
from adspy_shared_utilities import load_crime_dataset
from sklearn.linear_model import Ridge
(X_crime, y_crime) = load_crime_dataset()
X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime,
random_state = 0)
linridge = Ridge(alpha=20.0).fit(X_train, y_train)
# and we make the ridge linear regression
linridge = Ridge(alpha=20.0).fit(X_train, y_train)
# some information about it
print('Crime dataset')
print('Ridge regression linear model intercept: {}'
.format(linridge.intercept_))
print('Ridge regreesion linear model coeff: {}'
.format(linridge.coef_))
print('R-squared score (training): {:.3f}'
.format(linridge.score(X_train, y_train)))
print('R-squared score (test): {:.3f}'
.format(linridge.score(X_test, y_test)))
print('Number of non-zero features: {}'
.format(np.sum(linridge.coef_ != 0)))Crime dataset
Ridge regression linear model intercept: -3352.4230358465616
Ridge regreesion linear model coeff: [ 1.95091438e-03 2.19322667e+01 9.56286607e+00 -3.59178973e+01
6.36465325e+00 -1.96885471e+01 -2.80715856e-03 1.66254486e+00
-6.61426604e-03 -6.95450680e+00 1.71944731e+01 -5.62819154e+00
8.83525114e+00 6.79085746e-01 -7.33614221e+00 6.70389803e-03
9.78505502e-04 5.01202169e-03 -4.89870524e+00 -1.79270062e+01
9.17572382e+00 -1.24454193e+00 1.21845360e+00 1.03233089e+01
-3.78037278e+00 -3.73428973e+00 4.74595305e+00 8.42696855e+00
3.09250005e+01 1.18644167e+01 -2.05183675e+00 -3.82210450e+01
1.85081589e+01 1.52510829e+00 -2.20086608e+01 2.46283912e+00
3.29328703e-01 4.02228467e+00 -1.12903533e+01 -4.69567413e-03
4.27046505e+01 -1.22507167e-03 1.40795790e+00 9.35041855e-01
-3.00464253e+00 1.12390514e+00 -1.82487653e+01 -1.54653407e+01
2.41917002e+01 -1.32497562e+01 -4.20113118e-01 -3.59710660e+01
1.29786751e+01 -2.80765995e+01 4.38513476e+01 3.86590044e+01
-6.46024046e+01 -1.63714023e+01 2.90397330e+01 4.15472907e+00
5.34033563e+01 1.98773191e-02 -5.47413979e-01 1.23883518e+01
1.03526583e+01 -1.57238894e+00 3.15887097e+00 8.77757987e+00
-2.94724962e+01 -2.33527019e-04 3.13528914e-04 -4.13096792e-04
-1.80382678e-04 -5.74054528e-01 -5.17742507e-01 -4.20670930e-01
1.53383593e-01 1.32725423e+00 3.84863158e+00 3.03024594e+00
-3.77692644e+01 1.37933464e-01 3.07676522e-01 1.57128807e+01
3.31418306e-01 3.35994414e+00 1.61265911e-01 -2.67619878e+00]
R-squared score (training): 0.671
R-squared score (test): 0.494
Number of non-zero features: 88# we can rescale the data as well
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime,
random_state = 0)
# we must rescale both train and test data
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
linridge = Ridge(alpha=20.0).fit(X_train_scaled, y_train)
# in this rescaling we get better R-squared values, but only a little
print('Crime dataset')
print('ridge regression linear model intercept: {}'
.format(linridge.intercept_))
print('ridge regression linear model coeff:\n{}'
.format(linridge.coef_))
print('R-squared score (training): {:.3f}'
.format(linridge.score(X_train_scaled, y_train)))
print('R-squared score (test): {:.3f}'
.format(linridge.score(X_test_scaled, y_test)))
print('Number of non-zero features: {}'
.format(np.sum(linridge.coef_ != 0)))Crime dataset
ridge regression linear model intercept: 933.3906385044108
ridge regression linear model coeff:
[ 88.68827454 16.48947987 -50.30285445 -82.90507574 -65.89507244
-2.27674244 87.74108514 150.94862182 18.8802613 -31.05554992
-43.13536109 -189.44266328 -4.52658099 107.97866804 -76.53358414
2.86032762 34.95230077 90.13523036 52.46428263 -62.10898424
115.01780357 2.66942023 6.94331369 -5.66646499 -101.55269144
-36.9087526 -8.7053343 29.11999068 171.25963057 99.36919476
75.06611841 123.63522539 95.24316483 -330.61044265 -442.30179004
-284.49744001 -258.37150609 17.66431072 -101.70717151 110.64762887
523.13611718 24.8208959 4.86533322 -30.46775619 -3.51753937
50.57947231 10.84840601 18.27680946 44.11189865 58.33588176
67.08698975 -57.93524659 116.1446052 53.81163718 49.01607711
-7.62262031 55.14288543 -52.08878272 123.39291017 77.12562171
45.49795317 184.91229771 -91.35721203 1.07975971 234.09267451
10.3887921 94.7171829 167.91856631 -25.14025088 -1.18242839
14.60362467 36.77122659 53.19878339 -78.86365997 -5.89858411
26.04790298 115.1534917 68.74143311 68.28588166 16.5260514
-97.90513652 205.20448474 75.97304123 61.3791085 -79.83157049
67.26700741 95.67094538 -11.88380569]
R-squared score (training): 0.615
R-squared score (test): 0.599
Number of non-zero features: 88# also we can choose different values of alpha parameter, depending
# on this we can get a better R-squared value
print('Ridge regression: effect of alpha regularization parameter\n')
for this_alpha in [0, 1, 10, 20, 50, 100, 1000]:
linridge = Ridge(alpha = this_alpha).fit(X_train_scaled, y_train)
r2_train = linridge.score(X_train_scaled, y_train)
r2_test = linridge.score(X_test_scaled, y_test)
num_coeff_bigger = np.sum(abs(linridge.coef_) > 1.0)
print('Alpha = {:.2f}\nnum abs(coeff) > 1.0: {}, \
r-squared training: {:.2f}, r-squared test: {:.2f}\n'
.format(this_alpha, num_coeff_bigger, r2_train, r2_test))Ridge regression: effect of alpha regularization parameter
Alpha = 0.00
num abs(coeff) > 1.0: 88, r-squared training: 0.67, r-squared test: 0.49
Alpha = 1.00
num abs(coeff) > 1.0: 87, r-squared training: 0.66, r-squared test: 0.56
Alpha = 10.00
num abs(coeff) > 1.0: 87, r-squared training: 0.63, r-squared test: 0.59
Alpha = 20.00
num abs(coeff) > 1.0: 88, r-squared training: 0.61, r-squared test: 0.60
Alpha = 50.00
num abs(coeff) > 1.0: 86, r-squared training: 0.58, r-squared test: 0.58
Alpha = 100.00
num abs(coeff) > 1.0: 87, r-squared training: 0.55, r-squared test: 0.55
Alpha = 1000.00
num abs(coeff) > 1.0: 84, r-squared training: 0.31, r-squared test: 0.30# Lasso regression is another regularized regression model where
# instead of use L2 penalty parameter, this use L1 parameter
from sklearn.linear_model import Lasso
scaler = MinMaxScaler()
X_train, X_test, y_train, y_test = train_test_split(X_crime, y_crime,
random_state = 0)
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
linlasso = Lasso(alpha=2.0, max_iter = 10000).fit(X_train_scaled, y_train)
print('Crime dataset')
print('lasso regression linear model intercept: {}'
.format(linlasso.intercept_))
print('lasso regression linear model coeff:\n{}'
.format(linlasso.coef_))
print('Non-zero features: {}'
.format(np.sum(linlasso.coef_ != 0)))
print('R-squared score (training): {:.3f}'
.format(linlasso.score(X_train_scaled, y_train)))
print('R-squared score (test): {:.3f}\n'
.format(linlasso.score(X_test_scaled, y_test)))
print('Features with non-zero weight (sorted by absolute magnitude):')
for e in sorted (list(zip(list(X_crime), linlasso.coef_)),
key = lambda e: -abs(e[1])):
if e[1] != 0:
print('\t{}, {:.3f}'.format(e[0], e[1]))Crime dataset
lasso regression linear model intercept: 1186.6120619985793
lasso regression linear model coeff:
[ 0. 0. -0. -168.18346054
-0. -0. 0. 119.6938194
0. -0. 0. -169.67564456
-0. 0. -0. 0.
0. 0. -0. -0.
0. -0. 0. 0.
-57.52991966 -0. -0. 0.
259.32889226 -0. 0. 0.
0. -0. -1188.7396867 -0.
-0. -0. -231.42347299 0.
1488.36512229 0. -0. -0.
-0. 0. 0. 0.
0. 0. -0. 0.
20.14419415 0. 0. 0.
0. 0. 339.04468804 0.
0. 459.53799903 -0. 0.
122.69221826 -0. 91.41202242 0.
-0. 0. 0. 73.14365856
0. -0. 0. 0.
86.35600042 0. 0. 0.
-104.57143405 264.93206555 0. 23.4488645
-49.39355188 0. 5.19775369 0. ]
Non-zero features: 20
R-squared score (training): 0.631
R-squared score (test): 0.624
Features with non-zero weight (sorted by absolute magnitude):
PctKidsBornNeverMar, 1488.365
PctKids2Par, -1188.740
HousVacant, 459.538
PctPersDenseHous, 339.045
NumInShelters, 264.932
MalePctDivorce, 259.329
PctWorkMom, -231.423
pctWInvInc, -169.676
agePct12t29, -168.183
PctVacantBoarded, 122.692
pctUrban, 119.694
MedOwnCostPctIncNoMtg, -104.571
MedYrHousBuilt, 91.412
RentQrange, 86.356
OwnOccHiQuart, 73.144
PctEmplManu, -57.530
PctBornSameState, -49.394
PctForeignBorn, 23.449
PctLargHouseFam, 20.144
PctSameCity85, 5.198# once again we can chance the value of alpha paramter
print('Lasso regression: effect of alpha regularization\n\
parameter on number of features kept in final model\n')
for alpha in [0.5, 1, 2, 3, 5, 10, 20, 50]:
linlasso = Lasso(alpha, max_iter = 10000).fit(X_train_scaled, y_train)
r2_train = linlasso.score(X_train_scaled, y_train)
r2_test = linlasso.score(X_test_scaled, y_test)
print('Alpha = {:.2f}\nFeatures kept: {}, r-squared training: {:.2f}, \
r-squared test: {:.2f}\n'
.format(alpha, np.sum(linlasso.coef_ != 0), r2_train, r2_test))Lasso regression: effect of alpha regularization
parameter on number of features kept in final model
Alpha = 0.50
Features kept: 35, r-squared training: 0.65, r-squared test: 0.58
Alpha = 1.00
Features kept: 25, r-squared training: 0.64, r-squared test: 0.60
Alpha = 2.00
Features kept: 20, r-squared training: 0.63, r-squared test: 0.62
Alpha = 3.00
Features kept: 17, r-squared training: 0.62, r-squared test: 0.63
Alpha = 5.00
Features kept: 12, r-squared training: 0.60, r-squared test: 0.61
Alpha = 10.00
Features kept: 6, r-squared training: 0.57, r-squared test: 0.58
Alpha = 20.00
Features kept: 2, r-squared training: 0.51, r-squared test: 0.50
Alpha = 50.00
Features kept: 1, r-squared training: 0.31, r-squared test: 0.30# another type of regression is the polynomial regresison
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
X_train, X_test, y_train, y_test = train_test_split(X_F1, y_F1,
random_state = 0)
linreg = LinearRegression().fit(X_train, y_train)
print('linear model coeff (w): {}'
.format(linreg.coef_))
print('linear model intercept (b): {:.3f}'
.format(linreg.intercept_))
print('R-squared score (training): {:.3f}'
.format(linreg.score(X_train, y_train)))
print('R-squared score (test): {:.3f}'
.format(linreg.score(X_test, y_test)))
print('\nNow we transform the original input data to add\n\
polynomial features up to degree 2 (quadratic)\n')
poly = PolynomialFeatures(degree=2)
X_F1_poly = poly.fit_transform(X_F1)
X_train, X_test, y_train, y_test = train_test_split(X_F1_poly, y_F1,
random_state = 0)
linreg = LinearRegression().fit(X_train, y_train)
# here we can see that this polynomial regression improve significantly
# the R-squared values, compared to linear regression
print('(poly deg 2) linear model coeff (w):\n{}'
.format(linreg.coef_))
print('(poly deg 2) linear model intercept (b): {:.3f}'
.format(linreg.intercept_))
print('(poly deg 2) R-squared score (training): {:.3f}'
.format(linreg.score(X_train, y_train)))
print('(poly deg 2) R-squared score (test): {:.3f}\n'
.format(linreg.score(X_test, y_test)))
print('\nAddition of many polynomial features often leads to\n\
overfitting, so we often use polynomial features in combination\n\
with regression that has a regularization penalty, like ridge\n\
regression.\n')linear model coeff (w): [ 4.42036739 5.99661447 0.52894712 10.23751345 6.5507973 -2.02082636
-0.32378811]
linear model intercept (b): 1.543
R-squared score (training): 0.722
R-squared score (test): 0.722
Now we transform the original input data to add
polynomial features up to degree 2 (quadratic)
(poly deg 2) linear model coeff (w):
[ 3.40951018e-12 1.66452443e+01 2.67285381e+01 -2.21348316e+01
1.24359227e+01 6.93086826e+00 1.04772675e+00 3.71352773e+00
-1.33785505e+01 -5.73177185e+00 1.61813184e+00 3.66399592e+00
5.04513181e+00 -1.45835979e+00 1.95156872e+00 -1.51297378e+01
4.86762224e+00 -2.97084269e+00 -7.78370522e+00 5.14696078e+00
-4.65479361e+00 1.84147395e+01 -2.22040650e+00 2.16572630e+00
-1.27989481e+00 1.87946559e+00 1.52962716e-01 5.62073813e-01
-8.91697516e-01 -2.18481128e+00 1.37595426e+00 -4.90336041e+00
-2.23535458e+00 1.38268439e+00 -5.51908208e-01 -1.08795007e+00]
(poly deg 2) linear model intercept (b): -3.206
(poly deg 2) R-squared score (training): 0.969
(poly deg 2) R-squared score (test): 0.805
Addition of many polynomial features often leads to
overfitting, so we often use polynomial features in combination
with regression that has a regularization penalty, like ridge
regression.الانحدار اللوجستي (Logistic regression)
الانحدار اللوجستي للتصنيف الثنائي على مجموعة بيانات الفاكهة باستخدام سمتَي الارتفاع والعرض (الفئة الموجبة: تفاح، الفئة السالبة: غير ذلك)
fruits = pd.read_table('assets/fruit_data_with_colors.txt')
feature_names_fruits = ['height', 'width', 'mass', 'color_score']
X_fruits = fruits[feature_names_fruits]
y_fruits = fruits['fruit_label']
target_names_fruits = ['apple', 'mandarin', 'orange', 'lemon']
X_fruits_2d = fruits[['height', 'width']]
y_fruits_2d = fruits['fruit_label']from sklearn.linear_model import LogisticRegression
from adspy_shared_utilities import (
plot_class_regions_for_classifier_subplot)
fig, subaxes = plt.subplots(1, 1, figsize=(7, 5))
y_fruits_apple = y_fruits_2d == 1 # make into a binary problem: apples vs everything else
X_train, X_test, y_train, y_test = (
train_test_split(X_fruits_2d.values,
y_fruits_apple.values,
random_state = 0))
clf = LogisticRegression(C=100).fit(X_train, y_train)
plot_class_regions_for_classifier_subplot(clf, X_train, y_train, None,
None, 'Logistic regression \
for binary classification\nFruit dataset: Apple vs others',
subaxes)
h = 6
w = 8
print('A fruit with height {} and width {} is predicted to be: {}'
.format(h,w, ['not an apple', 'an apple'][int(clf.predict([[h,w]])[0])]))
h = 10
w = 7
print('A fruit with height {} and width {} is predicted to be: {}'
.format(h,w, ['not an apple', 'an apple'][int(clf.predict([[h,w]])[0])]))
subaxes.set_xlabel('height')
subaxes.set_ylabel('width')
print('Accuracy of Logistic regression classifier on training set: {:.2f}'
.format(clf.score(X_train, y_train)))
print('Accuracy of Logistic regression classifier on test set: {:.2f}'
.format(clf.score(X_test, y_test)))<IPython.core.display.Javascript object><IPython.core.display.HTML object>A fruit with height 6 and width 8 is predicted to be: an apple
A fruit with height 10 and width 7 is predicted to be: not an apple
Accuracy of Logistic regression classifier on training set: 0.80
Accuracy of Logistic regression classifier on test set: 0.73# so we can use multiples values of C to make a better estimate
X_train, X_test, y_train, y_test = (
train_test_split(X_fruits_2d.values,
y_fruits_apple.values,
random_state=0))
fig, subaxes = plt.subplots(3, 1, figsize=(4, 10))
for this_C, subplot in zip([0.1, 1, 100], subaxes):
clf = LogisticRegression(C=this_C).fit(X_train, y_train)
title ='Logistic regression (apple vs rest), C = {:.3f}'.format(this_C)
plot_class_regions_for_classifier_subplot(clf, X_train, y_train,
X_test, y_test, title,
subplot)
plt.tight_layout()<IPython.core.display.Javascript object><IPython.core.display.HTML object># applying the logic regression to real data
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
(X_cancer, y_cancer) = load_breast_cancer(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 0)
clf = LogisticRegression().fit(X_train, y_train)
print('Breast cancer dataset')
print('Accuracy of Logistic regression classifier on training set: {:.2f}'
.format(clf.score(X_train, y_train)))
print('Accuracy of Logistic regression classifier on test set: {:.2f}'
.format(clf.score(X_test, y_test)))Breast cancer dataset
Accuracy of Logistic regression classifier on training set: 0.94
Accuracy of Logistic regression classifier on test set: 0.94
/home/segismundo/.local/lib/python3.10/site-packages/sklearn/linear_model/_logistic.py:458: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(المصنِّفات الخطية: آلات المتجهات الداعمة (Support Vector Machines)
from sklearn.svm import SVC
from adspy_shared_utilities import(
plot_class_regions_for_classifier_subplot)
X_train, X_test, y_train, y_test = train_test_split(X_C2, y_C2,
random_state=0)
fig, subaxes = plt.subplots(1, 1, figsize=(7,5))
this_C = 1.0
clf = SVC(kernel = 'linear', C=this_C).fit(X_train, y_train)
title = 'Linear SVC, C = {:.3f}'.format(this_C)
plot_class_regions_for_classifier_subplot(clf, X_train, y_train,
None, None, title, subaxes)<IPython.core.display.Javascript object><IPython.core.display.HTML object># also we can change the tolerance parameter C
from sklearn.svm import LinearSVC
from adspy_shared_utilities import plot_class_regions_for_classifier
X_train, X_test, y_train, y_test = train_test_split(X_C2, y_C2, random_state = 0)
fig, subaxes = plt.subplots(1, 2, figsize=(8, 4))
for this_C, subplot in zip([0.00001, 100], subaxes):
clf = LinearSVC(C=this_C).fit(X_train, y_train)
title = 'Linear SVC, C = {:.5f}'.format(this_C)
plot_class_regions_for_classifier_subplot(clf, X_train, y_train,
None, None, title, subplot)
plt.tight_layout()<IPython.core.display.Javascript object><IPython.core.display.HTML object>/home/segismundo/.local/lib/python3.10/site-packages/sklearn/svm/_base.py:1244: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
warnings.warn(# and if we apply this method to real data
from sklearn.svm import LinearSVC
X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 0)
clf = LinearSVC().fit(X_train, y_train)
print('Breast cancer dataset')
print('Accuracy of Linear SVC classifier on training set: {:.2f}'
.format(clf.score(X_train, y_train)))
print('Accuracy of Linear SVC classifier on test set: {:.2f}'
.format(clf.score(X_test, y_test)))Breast cancer dataset
Accuracy of Linear SVC classifier on training set: 0.93
Accuracy of Linear SVC classifier on test set: 0.95
/home/segismundo/.local/lib/python3.10/site-packages/sklearn/svm/_base.py:1244: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
warnings.warn(آلات المتجهات الداعمة ذات النواة (Kernelized Support Vector Machines)
from sklearn.svm import SVC
from adspy_shared_utilities import plot_class_regions_for_classifier
X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state = 0)
# The default SVC kernel is radial basis function (RBF)
plot_class_regions_for_classifier(SVC().fit(X_train, y_train),
X_train, y_train, None, None,
'Support Vector Classifier: RBF kernel')
# Compare decision boundries with polynomial kernel, degree = 3
plot_class_regions_for_classifier(SVC(kernel = 'poly', degree = 3)
.fit(X_train, y_train), X_train,
y_train, None, None,
'Support Vector Classifier: Polynomial kernel, degree = 3')<IPython.core.display.Javascript object><IPython.core.display.HTML object><IPython.core.display.Javascript object><IPython.core.display.HTML object># also we can vary the value of gamma to generate more complex boundary decitions
from adspy_shared_utilities import plot_class_regions_for_classifier
X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state = 0)
fig, subaxes = plt.subplots(3, 1, figsize=(4, 11))
for this_gamma, subplot in zip([0.01, 1.0, 10.0], subaxes):
clf = SVC(kernel = 'rbf', gamma=this_gamma).fit(X_train, y_train)
title = 'Support Vector Classifier: \nRBF kernel, gamma = {:.2f}'.format(this_gamma)
plot_class_regions_for_classifier_subplot(clf, X_train, y_train,
None, None, title, subplot)
plt.tight_layout()<IPython.core.display.Javascript object><IPython.core.display.HTML object>آلة المتجهات الداعمة بنواة RBF: باستخدام معامل C ومعامل gamma معًا
# once we have varied gamma we could vary C parameter as well
from sklearn.svm import SVC
from adspy_shared_utilities import plot_class_regions_for_classifier_subplot
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state = 0)
fig, subaxes = plt.subplots(3, 4, figsize=(15, 10), dpi=50)
for this_gamma, this_axis in zip([0.01, 1, 5], subaxes):
for this_C, subplot in zip([0.1, 1, 15, 250], this_axis):
title = 'gamma = {:.2f}, C = {:.2f}'.format(this_gamma, this_C)
clf = SVC(kernel = 'rbf', gamma = this_gamma,
C = this_C).fit(X_train, y_train)
plot_class_regions_for_classifier_subplot(clf, X_train, y_train,
X_test, y_test, title,
subplot)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)<IPython.core.display.Javascript object><IPython.core.display.HTML object>تطبيق آلات المتجهات الداعمة على مجموعة بيانات حقيقية: بيانات غير مُطبَّعة (Unnormalized)
from sklearn.svm import SVC
X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer,
random_state = 0)
clf = SVC(C=10).fit(X_train, y_train)
print('Breast cancer dataset (unnormalized features)')
print('Accuracy of RBF-kernel SVC on training set: {:.2f}'
.format(clf.score(X_train, y_train)))
print('Accuracy of RBF-kernel SVC on test set: {:.2f}'
.format(clf.score(X_test, y_test)))Breast cancer dataset (unnormalized features)
Accuracy of RBF-kernel SVC on training set: 0.92
Accuracy of RBF-kernel SVC on test set: 0.94# on the other hand, when we scaling the input data using MinMaxScaler, we get
# a better fit on testing data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
clf = SVC(C=10).fit(X_train_scaled, y_train)
print('Breast cancer dataset (normalized with MinMax scaling)')
print('RBF-kernel SVC (with MinMax scaling) training set accuracy: {:.2f}'
.format(clf.score(X_train_scaled, y_train)))
print('RBF-kernel SVC (with MinMax scaling) test set accuracy: {:.2f}'
.format(clf.score(X_test_scaled, y_test)))Breast cancer dataset (normalized with MinMax scaling)
RBF-kernel SVC (with MinMax scaling) training set accuracy: 0.99
RBF-kernel SVC (with MinMax scaling) test set accuracy: 0.97أشجار القرار (Decision Trees)
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from adspy_shared_utilities import plot_decision_tree
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state = 3)
clf = DecisionTreeClassifier().fit(X_train, y_train)
# we need to avoid that overfitting
print('Accuracy of Decision Tree classifier on training set: {:.2f}'
.format(clf.score(X_train, y_train)))
print('Accuracy of Decision Tree classifier on test set: {:.2f}'
.format(clf.score(X_test, y_test)))Accuracy of Decision Tree classifier on training set: 1.00
Accuracy of Decision Tree classifier on test set: 0.97# so to avoid overfitting (trainscore = 1.00), we setting max decision tree depth to help avoid overfitting
clf2 = DecisionTreeClassifier(max_depth = 3).fit(X_train, y_train)
print('Accuracy of Decision Tree classifier on training set: {:.2f}'
.format(clf2.score(X_train, y_train)))
print('Accuracy of Decision Tree classifier on test set: {:.2f}'
.format(clf2.score(X_test, y_test)))Accuracy of Decision Tree classifier on training set: 0.98
Accuracy of Decision Tree classifier on test set: 0.97# and we plot the decision tree to visualize how this method works
plot_decision_tree(clf, iris.feature_names, iris.target_names)<graphviz.sources.Source at 0x7f77e71ee8c0>أهمية السمات (Feature importance)
# we can also analyse how important a feature is to the prediction
# e.g., in the case of iris prediction we have
from adspy_shared_utilities import plot_feature_importances
plt.figure(figsize=(10,4), dpi=80)
plot_feature_importances(clf, iris.feature_names)
plt.show()
print('Feature importances: {}'.format(clf.feature_importances_))<IPython.core.display.Javascript object><IPython.core.display.HTML object>Feature importances: [0. 0.02457904 0.54643762 0.42898334]