Kernel: base
Machine Learning with PyTorch and Scikit-Learn
-- Code Examples
Package version checks
Add folder to path in order to load from the check_packages.py script:
In [1]:
Check recommended package versions:
In [2]:
Out[2]:
[OK] Your Python version is 3.9.6 | packaged by conda-forge | (default, Jul 11 2021, 03:35:11)
[Clang 11.1.0 ]
[OK] numpy 1.21.2
[OK] matplotlib 3.4.3
[OK] sklearn 1.0
[OK] pandas 1.3.2
Chapter 3 - A Tour of Machine Learning Classifiers Using Scikit-Learn
Overview
In [3]:
Choosing a classification algorithm
...
First steps with scikit-learn
Loading the Iris dataset from scikit-learn. Here, the third column represents the petal length, and the fourth column the petal width of the flower examples. The classes are already converted to integer labels where 0=Iris-Setosa, 1=Iris-Versicolor, 2=Iris-Virginica.
In [4]:
Out[4]:
Class labels: [0 1 2]
Splitting data into 70% training and 30% test data:
In [5]:
In [6]:
Out[6]:
Labels counts in y: [50 50 50]
Labels counts in y_train: [35 35 35]
Labels counts in y_test: [15 15 15]
Standardizing the features:
In [7]:
Training a perceptron via scikit-learn
In [9]:
Out[9]:
Perceptron(eta0=0.1, random_state=1)
In [10]:
Out[10]:
Misclassified examples: 1
In [11]:
Out[11]:
Accuracy: 0.978
In [12]:
Out[12]:
Accuracy: 0.978
In [13]:
Training a perceptron model using the standardized training data:
In [15]:
Out[15]:
Modeling class probabilities via logistic regression
...
Logistic regression intuition and conditional probabilities
In [17]:
Out[17]:
In [18]:
Out[18]:
In [19]:
Out[19]:
Learning the weights of the logistic loss function
In [57]:
Out[57]:
In [22]:
In [24]:
Out[24]:
Training a logistic regression model with scikit-learn
In [26]:
Out[26]:
In [27]:
Out[27]:
array([[3.81527885e-09, 1.44792866e-01, 8.55207131e-01],
[8.34020679e-01, 1.65979321e-01, 3.25737138e-13],
[8.48831425e-01, 1.51168575e-01, 2.62277619e-14]])
In [28]:
Out[28]:
array([1., 1., 1.])
In [29]:
Out[29]:
array([2, 0, 0])
In [30]:
Out[30]:
array([2, 0, 0])
In [31]:
Out[31]:
array([2])
Tackling overfitting via regularization
In [33]:
Out[33]:
In [34]:
Out[34]:
Maximum margin classification with support vector machines
In [36]:
Out[36]:
Maximum margin intuition
...
Dealing with the nonlinearly separable case using slack variables
In [38]:
Out[38]:
In [39]:
Out[39]:
Alternative implementations in scikit-learn
In [40]:
Solving non-linear problems using a kernel SVM
In [41]:
Out[41]:
In [42]:
Out[42]:
Using the kernel trick to find separating hyperplanes in higher dimensional space
In [43]:
Out[43]:
In [44]:
Out[44]:
In [45]:
Out[45]:
Decision tree learning
In [46]:
Out[46]:
In [47]:
Out[47]:
In [48]:
Out[48]:
Maximizing information gain - getting the most bang for the buck
In [49]:
Out[49]:
Building a decision tree
In [50]:
Out[50]:
In [51]:
Out[51]:
Combining weak to strong learners via random forests
In [52]:
Out[52]:
K-nearest neighbors - a lazy learning algorithm
In [53]:
Out[53]:
In [54]:
Out[54]:
Summary
...
Readers may ignore the next cell.
In [55]:
Out[55]:
[NbConvertApp] WARNING | Config option `kernel_spec_manager_class` not recognized by `NbConvertApp`.
[NbConvertApp] Converting notebook ch03.ipynb to script
[NbConvertApp] Writing 19384 bytes to ch03.py
In [ ]: