ML Week01 Lab (KNN)
29 min read
Lesson slides
1 / 14
Mansoura University
- First Semester- 2023-2024
- Department of Computer Science
- Faculty of Computers and Information
- ML week1
- Mariam Essam
Fundamentals of Machine Learning
Initial Term plan
- 01
- Fundamentals of Machine Learning
- Linear Regression and Logistic Regression
- 04
- K-Nearest Neighbours Classification
- 02
- Model Evaluation & Selection
- 03
- Support Vector Machines
- 05
- Decision Trees
- 06
- Naive Bayes Classifiers
- 07
- Neural networks
- 08
4
- Traditional programming
- Machine learning programming
What is Machine Learning (ML)?
- The study of computer programs (algorithms) that can learn by example
- ML algorithms can generalize from existing examples of a task
- e.g. after seeing a training set of labeled images, an image classifier can figure out how to apply labels accurately to new, previously unseen images
- Machine Learning brings together statistics, computer science, and more..
- 5
ML
- Algorithms learn rules from labelled examples (training data)
- The learned rules should also be able to generalize to correctly recognize or predict new examples not in the training set.
- 6
Machine Learning vs. applied machiine learning
- Pure ML is mostly concerned with algorithm behavior as a whole
- ML
- Applied ML is concerned with behavior on specific real life data sets.
- Applied ML
8
- Machine Learning for fraud detection
- Take a guess :What type of ML is fraud detection?
Types of ML problems
- Supervised learning :
- Learn to predict target values from labelled data
- Classification (target values are discrete classes)
- Fraud detection
- Email spam detection
- Diagnostics
- Image classification
- Regression (target values are continuous values)
- Risk assessment
- Score prediction
- Unsupervised learning :
- Find structure in unlabeled data
- Clustring
- Find groups of similar instances in the data
- Outlier detection
- Finding unusual patterns
- Reinforcement learning :
- Some algorithms can deal with partially labeled training data, usually a lot of unlabeled data and a little bit of labeled data.
10
- ML
- e.g. image pixels, with k-nearest neighbor classifier
- e.g. % correct predictions on test set
- e.g. try a range of values for
- "k" parameter in k-nearest neighbor classifier
11
- Feature Representations
12
- Python
- Was invented in the 80s
- Python is an evolution of ABC programming language and influenced from C, Icon, and Modula -3 programming language.
- uses indentation as a syntactic feature
- Python encourages modularity and reuse of code .
Python – why?
- 01
- Readable and Maintainable Code
- readable and clean code base will help you to maintain and update the software without putting extra time and effort
- Compatible with Major Platforms and Systems
- At present, Python is supports many operating systems. You can even use Python interpreters to run the code on specific platforms and tools. Also, Python is an interpreted programming language. It allows you to run the same code on multiple platforms without recompilation
- 02
- Multiple Programming Paradigms
- It supports object oriented and structured programming fully. Also, its language features support various concepts in functional and aspect-oriented programming. At the same time, Python also features a dynamic type system and automatic memory management.
- 03
- Many Open Source Frameworks and Tools
- 04
Python – why?
- Simplify Complex Software Development
- Python is a general purpose programming language. Hence, you can use the programming language for developing both desktop and web applications. Also, you can use Python for developing complex scientific and numeric applications. Python is designed with features to facilitate data analysis and visualization. You can take advantage of the data analysis features of Python to create custom big data solutions without putting extra time and effort. At the same time,
- 05
- Python has a powerful libraries which very important like numpy , Pandas and Matplotlib ..
- 06
Anaconda is an easy-to-install free package manager, environment manager, Python distribution, and collection of over 720 open-source packages offering free community support. Anaconda can be downloaded for free from https://docs.continuum.io/anaconda/
- 15
16
- So in conclusion python
- Uses Dynamic Typing (an interpreter assigns a type to all the variables at run-time)
- Simple Syntax
- Interpreted
- Object orientation ( in earlier python versions it didn’t support encapsulation but know it does )
- _ protected variable
- __ private variable
17
- Modules and packages
- modules | Packages
- a single Python file that can be imported into another module | a collection of modules organized into a directory hierarchy
- the module can be directly installed using the import keyword followed by the module name.
- import module_name | A package is installed using the import keyword followed by the package name, and you can access the module and sub-packages within the package name using dot notation.
- import package_name.module_name
- math, random, os, datetime, csv | Numpy, Pandas, Matplotlib
18
- scikit-learn | Python Machine Learning Library
- from sklearn.model_selection import train_test_split
- from sklearn.tree import DecisionTreeClassifier
- SciPy Library: Scientific Computing Tools | Provides a variety of useful scientific computing tools, including statistical distributions, and a variety of specialized mathematical functions.
- • With scikit-learn, it provides support for sparse matrices, a way to store large tables that consist mostly of zeros.
- import scipy as sp
- NumPy: Scientific Computing Library | Provides fundamental data structures used by scikit-learn, particularly multi-dimensional arrays.
- • Typically, data that is input to scikit-learn will be in the form of a NumPy array.
- import numpy as np
- Pandas: Data Manipulation and Analysis | Provides key data structures like DataFrame
- • Also, support for reading/writing data in different formats
- 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
Any Question ?
- Next ….
- 32
Mansoura University
- First Semester- 2023-2024
- Department of Computer Science
- Faculty of Computers and Information
- ML week1
- Mariam Essam
K-Nearest Neighbours Classification
Initial Term plan
- 01
- Fundamentals of Machine Learning
- Linear Regression and Logistic Regression
- 04
- K-Nearest Neighbours Classification
- 02
- Model Evaluation & Selection
- 03
- Support Vector Machines
- 05
- Decision Trees
- 06
- Naive Bayes Classifiers
- 07
- Neural networks
- 08
4
- The k-Nearest Neighbor (k-NN) Classifier Algorithm
- Instance/memory based supervised learning
- Given a training set X_train with labels y_train, and given a new instance x_test to be classified:
- Find the most similar instances (let's call them X_NN) to x_test that are in X_train.
- Get the labels y_NN for the instances in X_NN
- Predict the label for x_test by combining the labels y_NN e.g. simple majority vote
5
- The k-Nearest Neighbor (k-NN) Classifier Algorithm
- Query point
- (Feature space)
- Decision boundary
6
- KNN algorithm needs 4 things
- A distance metric
- How many 'nearest' neighbors to look at?
- Optional weighting function on the neighbor points
- Method for aggregating the classes of neighbor points
7
- KNN algorithm needs 4 things
8
- Bias-variance trade off
- Less sensitive to noise , but less detailed boundary
9
10
- Model/Estimator : Model fitting produces a 'trained model'.
- – Training is the process of estimating model parameters.
11
Supervised Machine Learning
13
- Feature representation
- X : data instances , samples, examples
- y: target value
- Used for human purposes
Generalization, Overfitting, and Underfitting
- 14
- say, for, k = 1, the point closest to the data point in question will be considered.
- Here, the prediction might be accurate for that particular data point so the bias error will be less.
- However, the variance error will be high since only the one nearest point is considered and this doesn’t take into account the other possible points. this means that our model is overfitting.
- On the other hand, for higher values of k, many more points closer to the data point in question will be considered. This would result in higher bias error and under fitting since many points closer to the data point are considered and thus it can’t learn the specifics from the training set. However, we can account for a lower variance error for the testing set which has unknown values.
- Generalization ability refers to an algorithm's ability to give accurate predictions for new, previously unseen data.
15
16
- bias
- Bias : the difference between the prediction of the values by the Machine Learning model and the correct value
- High bias = large error in training as well as testing data & model underfitts
17
- variance
- Variance : The variability of model prediction for a given data point which tells us the spread of our data
- High variance = models perform very well on training data but have high error rates on test data & model overfitts
- (Variance is the amount that the estimate of the target function will change if different training data was used)
Supervised learning datasets
- Different synthetic and real datasets can be created or used directly.
- Fruit dataset is an example of a Multi-class Classification Dataset
- 18
- https://scikit-learn.org/stable/datasets.html#:~:text=The%20sklearn.,from%20the%20'real%20world'.
K-Nearest Neighbours: Classification and Regression
- 19
- KNN can be used for both classification , regression
20
- Sklearn's model.score(X,y) calculation is based on co-efficient of determination i.e. R^2
- The score is between 0 and 1: – A value of 0 corresponds to a constant model that predicts the mean value of all training target values. – A value of 1 corresponds to perfect prediction
- Default num = 5
Cross-Validation
- 21
- • Uses multiple train-test splits, not just a single one , Each split used to train & evaluate a separate model
- • Why is this better?
- The accuracy score of a supervised learning method can vary, depending on which samples happen to end up in the training set.
- Using multiple train-test splits gives more stable and reliable estimates for how the classifier is likely to perform on average.
- Results are averaged over multiple different training sets instead of relying on a single model trained on a particular training set.
22
- k-Fold Cross-Validation
- In classification sklearn uses
- In regression sklearn uses
23
24
25
- Using the same example (about KNN) , modify the code written to make the K have a range value from 1 to 15 for the same problem and report the accuracy for each k-value
- Define which k value will outperform other values.
- Assignment 1
- How to turn it in?
- Google classroom
- https://classroom.google.com/c/NjMxOTI0NzkyNzc2?cjc=mxqfokp
26
Any Question ?
- Next ….
- 27
Introduction to 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-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-Nearest Neighbors Regression
K-Nearest Neighbors can be usefull not only to classify, but to make 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>Linear Regression: Ridge, Lasso, and 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
Logistic regression for binary classification on fruits dataset using height, width features (positive class: apple, negative class: others)
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(Linear Classifiers: 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>Support Vector Machine with RBF kernel: using both C and gamma parameter
# 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>Application of SVMs to a real dataset: unnormalized data
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.97Decision 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]