Docsity

Machine Learning Summary with Illustrations, Study notes of Machine Learning

Partial preview of the text

Download Machine Learning Summary with Illustrations and more Study notes Machine Learning in PDF only on Docsity!

X

Ikhlaq Sidhu Chief Scientist & Founding Director, Sutardja Center for Entrepreneurship & Technology IEOR Emerging Area Professor Award, UC Berkeley About Me:

Machine Learning Summary with Illustrations

Part I – Setting UP for ML

Data X: A Course on Data, Signals, and Systems

Data

X

X Course Overview

Open-ended, real-world project: Typically 5 students, with available advisor network

We are

now here:

  • tools
  • theory

Titanic

Notebook

X

Linear Regression Illustration

12/19/2016 How to choose machine learning algorithms | Microsoft Docs https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice 9/ Logistic regression Data with a linear trend Although it confusingly includes 'regression' in the name, logistic regression is actually a powerful tool for two-class and multiclass classification. It's fast and simple. The fact that it uses an 'S'-shaped curve instead of a straight line makes it a natural fit for dividing data into groups. Logistic regression gives linear class boundaries, so when you use it, make sure a linear approximation is something you can live with. https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice

 Tip

Flavors of machine learning Supervised

Letter recognition.

To download and print a diagram that gives an overview of the capabilities Studio, see Overview diagram of Azure Machine Learning Studio capabilitie

Supervised learning algorithms make predictions based on a set of e

historical stock prices can be used to hazard guesses at future prices

training is labeled with the value of interest—in this case the stock p

learning algorithm looks for patterns in those value labels. It can use

might be relevant—the day of the week, the season, the company's f

industry, the presence of disruptive geopolicitical events—and each

Illustration Source: #Setting Linear Regression in sklearn from sklearn import linear_model model= linear_model.LinearRegression() model.fit(X_train, Y_train) Y_pred_train = model.predict(X_train) Y_pred_test = model.predict(X_test)

Compare Y_pred_test with Y_test for

error.

X

#Setting up for Supervised learning

# First clean: use mapping + buckets

# X = matrix of data – e.g 1000 rows

# Y = In sample responses

# Typically we want to split in to

training data and test data

X_train = X[0:500]

Y_train = Y[0:500]

X_test = X[501:1000]

Y_test = Y[501:1000]

X train Y train X test Y test X Non Labeled Y? Common Issue: Do you have enough data to train and then test? Small training set - >? All training data - >? How to use the data efficiently?

X X Y

  • Common Issue: Having enough data to train and test
  • Cross Validation
  • K-fold (ie 3 - fold, 4-fold, ..
  • Example:
    • Train (1,2) - > Test with 3
    • Train (2,3) - > Test with 1
    • Train (1,3( - > Test with 2
    • Estimate model error as average of all 3 Section 1 Section 2 Section 3 The general procedure is as follows:
  1. Shuffle the dataset randomly.
  2. Split the dataset into k groups
  3. For each unique group:
    1. Take the group as a hold out or test data set
    2. Take the remaining groups as a training data set
    3. Fit a model on the training set and evaluate it on the test set
    4. Retain the evaluation score and discard the model
  4. Summarize the skill of the model using the sample of model evaluation scores A Gentle Introduction to k-fold Cross-Validation by Jason Brownlee

X

This Section:

  • Context of the Titanic notebook
  • Setting up data tables for training and testing ML Models
  • Linear regression example in Scikit for prediction
  • Cross validation (k-fold)

Next Section: ML Algorithms for Classification

X

Titanic Notebook

Passenger List with ticket / cabin information Data in Pandas Table Format Clean Data Format for ML Models Run Many ML Models to predict Survival Passenger List (^) Cleaned and Formatted

X

Our experiment with the Titanic Data Set

Model Score Random Forest 86. Decision Tree 86. KNN 84. Support Vector Machines 83. Logistic Regression 80. Linear SVC 79. Perceptron 78. Naive Bayes 72. Stochastic Gradient Decent 72. More Accuracy Generally more training time More risk of overfitting Less Accuracy Generally less computation

Data

X

Logistic Regression Illustration

12/19/2016 How to choose machine learning algorithms | Microsoft Docs Trees, forests, and jungles A logistic regression to two-class data with just one feature - the class boundary is the point at which the logistic curve is just as close to both classes Decision forests (regression, two-class, and multiclass), decision jungles (two-class and multiclass), and boosted decision trees (regression and two-class) are all based on decision trees, a foundational machine learning concept. There are many variants of decision trees, but they all do the same thing—subdivide the feature space into regions with mostly the same label. These can be regions of consistent category or of constant value, depending on whether you are doing classification or regression. https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice

 Tip

Flavors of machine learning Supervised

Letter recognition.

To download and print a diagram that gives an overview of the capabilities Studio, see Overview diagram of Azure Machine Learning Studio capabilitie

Supervised learning algorithms make predictions based on a set of e

historical stock prices can be used to hazard guesses at future prices

training is labeled with the value of interest—in this case the stock p

learning algorithm looks for patterns in those value labels. It can use

might be relevant—the day of the week, the season, the company's f

industry, the presence of disruptive geopolicitical events—and each

Illustration Source: from sklearn.linear_model import LogisticRegression

Logistic Regression

logreg = LogisticRegression() logreg.fit(X_train, Y_train) #option for weights Y_pred = logreg.predict(X_test) #no options

Error

acc_log = round(logreg.score(X_train, Y_train) * 100, 2) acc_log

or compare Y_pred with Y_test

X

Support Vector Machine (SVM) Illustration

12/19/2016 How to choose machine learning algorithms | Microsoft Docs https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice 14/ SVMs The two-class averaged perceptron is neural networks' answer to skyrocketing training times. It uses a network structure that gives linear class boundaries. It is almost primitive by today's standards, but it has a long history of working robustly and is small enough to learn quickly. Support vector machines (SVMs) find the boundary that separates classes by as wide a margin as possible. When the two classes can't be clearly separated, the algorithms find the best boundary they can. As written in Azure Machine Learning, the two-class SVM does this with a straight line only. (In SVM-speak, it uses a linear kernel.) Because it makes this linear approximation, it is able to run fairly quickly. Where it really shines is with feature-intense data, like text or genomic. In these cases SVMs are able to separate classes more quickly and with less overfitting than most other algorithms, in addition to requiring only a modest amount of memory. A typical support vector machine class boundary maximizes the margin separating two classes + Options https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice

 Tip

Flavors of machine learning Supervised

algorithms against the same data and compares the results: Com

Letter recognition.

To download and print a diagram that gives an overview of the capabili Studio, see Overview diagram of Azure Machine Learning Studio capabi

Supervised learning algorithms make predictions based on a set

historical stock prices can be used to hazard guesses at future pri

training is labeled with the value of interest—in this case the stoc

learning algorithm looks for patterns in those value labels. It can

might be relevant—the day of the week, the season, the compan

industry, the presence of disruptive geopolicitical events—and ea

Illustration Source:

X

Support Vector Machine (SVM) Illustration

12/19/2016 How to choose machine learning algorithms | Microsoft Docs https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice 14/ SVMs The two-class averaged perceptron is neural networks' answer to skyrocketing training times. It uses a network structure that gives linear class boundaries. It is almost primitive by today's standards, but it has a long history of working robustly and is small enough to learn quickly. Support vector machines (SVMs) find the boundary that separates classes by as wide a margin as possible. When the two classes can't be clearly separated, the algorithms find the best boundary they can. As written in Azure Machine Learning, the two-class SVM does this with a straight line only. (In SVM-speak, it uses a linear kernel.) Because it makes this linear approximation, it is able to run fairly quickly. Where it really shines is with feature-intense data, like text or genomic. In these cases SVMs are able to separate classes more quickly and with less overfitting than most other algorithms, in addition to requiring only a modest amount of memory. A typical support vector machine class boundary maximizes the margin separating two classes + Options https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice

 Tip

Flavors of machine learning Supervised

algorithms against the same data and compares the results: Com

Letter recognition.

To download and print a diagram that gives an overview of the capabili Studio, see Overview diagram of Azure Machine Learning Studio capabi

Supervised learning algorithms make predictions based on a set

historical stock prices can be used to hazard guesses at future pri

training is labeled with the value of interest—in this case the stoc

learning algorithm looks for patterns in those value labels. It can

might be relevant—the day of the week, the season, the compan

industry, the presence of disruptive geopolicitical events—and ea

Illustration Source:

SVM Considerations:

1. Robust

2. Effective in high dimension

3. Even when data rows < feature

dimensions

4. Overfitting is possible,

regularization is often needed

5. To predict for sparse data, must

train with sparse data

X

Support Vector Machine (SVM) Illustration

12/19/2016 How to choose machine learning algorithms | Microsoft Docs https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice 14/ SVMs The two-class averaged perceptron is neural networks' answer to skyrocketing training times. It uses a network structure that gives linear class boundaries. It is almost primitive by today's standards, but it has a long history of working robustly and is small enough to learn quickly. Support vector machines (SVMs) find the boundary that separates classes by as wide a margin as possible. When the two classes can't be clearly separated, the algorithms find the best boundary they can. As written in Azure Machine Learning, the two-class SVM does this with a straight line only. (In SVM-speak, it uses a linear kernel.) Because it makes this linear approximation, it is able to run fairly quickly. Where it really shines is with feature-intense data, like text or genomic. In these cases SVMs are able to separate classes more quickly and with less overfitting than most other algorithms, in addition to requiring only a modest amount of memory. A typical support vector machine class boundary maximizes the margin separating two classes + Options https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice

 Tip

Flavors of machine learning Supervised

Letter recognition.

To download and print a diagram that gives an overview of the capabilities Studio, see Overview diagram of Azure Machine Learning Studio capabilitie

Supervised learning algorithms make predictions based on a set of e

historical stock prices can be used to hazard guesses at future prices

training is labeled with the value of interest—in this case the stock p

learning algorithm looks for patterns in those value labels. It can use

might be relevant—the day of the week, the season, the company's f

industry, the presence of disruptive geopolicitical events—and each

Illustration Source: from sklearn.svm import SVC, LinearSVC svc = SVC() svc.fit(X_train, Y_train) Y_pred = svc.predict(X_test)

Error

acc_svc = round(svc.score(X_train, Y_train) * 100, 2) acc_svc

or compare Y_pred with Y_test

X

KNN Method: Find the k nearest KNN / K Means Illustration

images and have them vote on the label (i.e. take the mode) ?

X

KNN / K Means Illustration

https://docs.microsoft.com/en-us/azure/machine-learning/machine-learning-algorithm-choice

 Tip

Flavors of machine learning Supervised

Letter recognition.

To download and print a diagram that gives an overview of th Studio, see Overview diagram of Azure Machine Learning Stu

Supervised learning algorithms make predictions based

historical stock prices can be used to hazard guesses at f

training is labeled with the value of interest—in this case

learning algorithm looks for patterns in those value labe

might be relevant—the day of the week, the season, the

industry, the presence of disruptive geopolicitical events

KNN Method: Find the k nearest images and have them vote on the label (i.e. take the mode) Example of k - NN classification. The test sample (green circle) should be classified either to the first class of blue squares or to the second class of red triangles. If k = 3 (solid line circle) it is assigned to the second class because there are 2 triangles and only 1 square inside the inner circle. If k = 5 (dashed line circle) it is assigned to the first class (3 squares vs. 2 triangles inside the outer circle). - Wikipedia ?