KnowledgeCity

Python for Data Science Advanced: Supervised Learning

Learn about supervised learning and how to use classification and regression

Supervised learning, a form of machine learning, trains algorithms to classify data or predict outcomes by using labeled datasets. Supervised learning comes in two forms: classification and regression. Classification uses an algorithm to assign test data into categories. It recognizes items within the dataset and attempts to draw conclusions on how to define those items. Common classification algorithms are linear classifiers and support vector machines. You can use regression to get a better understanding of the relationship between dependent and independent variables, and then use that knowledge to make projections. Popular regression algorithms include linear regression, logistical regression, and polynomial regression.

In this KnowledgeCity course on Python for Data Science Advanced: Supervised Learning, we’ll explore different types of supervised learning algorithms, including linear and logistic regression, ridge and lasso regression, polynomial and Bayesian regression, and linear and nonlinear support vectors. We’ll also discuss linear support vector machines, or SVMs, and supervised machine learning using SVMs, a popular technique for classification and regression tasks.

Learning Objectives

  • Describe the different regression techniques
  • Understand how supervised learning algorithms work
  • Explain how regression can help make useful projections

Author: Nizar Dajani

Duration: 49m · 6 lessons
Level: Advanced
Language: English

Skills you’ll gain

Machine LearningRegression AnalysisSupervised Learning

What You'll Learn

  • Describe different regression techniques, including linear, logistic, ridge, lasso, polynomial, and Bayesian regression
  • Understand how supervised learning algorithms work for classification and regression tasks
  • Explain how regression helps make useful projections about variable relationships
  • Apply linear and non-linear support vector machines (SVMs) for classification and regression
  • Distinguish between the two forms of supervised learning: classification and regression

Key Takeaways

  • Supervised learning is a form of machine learning that trains algorithms to classify data or predict outcomes using labeled datasets.
  • Supervised learning comes in two forms: classification, which assigns test data into categories, and regression, which models the relationship between dependent and independent variables.
  • Common classification algorithms include linear classifiers and support vector machines, while popular regression algorithms include linear, logistic, and polynomial regression.
  • Support vector machines (SVMs) are a popular technique used for both classification and regression tasks.
  • Regression can be used to understand variable relationships and then make useful projections from that knowledge.

Frequently Asked Questions

What does this course cover?

This advanced course explores different types of supervised learning algorithms, including linear and logistic regression, ridge and lasso regression, polynomial and Bayesian regression, and linear and non-linear support vector machines (SVMs) for classification and regression tasks.

What is supervised learning?

Supervised learning is a form of machine learning that trains algorithms to classify data or predict outcomes using labeled datasets. It comes in two forms: classification and regression.

What skills will I gain from this course?

The course develops skills in Machine Learning, Regression Analysis, and Supervised Learning.

What topics are taught in the lessons?

The lessons cover Linear and Logistic Regression (Parts 1 and 2), Ridge and Lasso Regression, Polynomial and Bayesian Regression, and Linear and Non-Linear Support Vector Machines.

What is the difference between classification and regression?

Classification uses an algorithm to assign test data into categories by recognizing items within the dataset, while regression helps understand the relationship between dependent and independent variables to make projections.

Transcript

Show transcript (free preview lesson)

Transcript of the free preview lesson. Remaining lessons unlock with the full course.

Hello, my name is Nazar Dajani, and in these lessons, you will learn about different types of supervised learning algorithms. This will include linear and logistic regression, ridge and lasso regression, polynomial and Bayesian regression, as well as linear and nonlinear support vector machine. Now in this lesson, we will begin with two regression types, linear and logistic regression. In general, supervised learning is a type of machine learning where the algorithm learns from labeled training data and makes predictions on unseen data. Linear regression is a type of supervised learning algorithm that is used for regression tasks, while logistic regression is another type of a supervised learning algorithm that is used for classification tasks. So let's start with linear regression. We use linear regression when the target variable is continuous. It tries to find a linear relationship between the input features and the target variable. So let's review the code in our first example. We start out by importing the necessary libraries, so we import numpy for numerical operations, matplotlib for data visualization, and sidekit learn for machine learning functionalities. We then create synthetic data for this example. X represents the input features, and Y represents the target variable. Next we split the data into training and testing sets using the train test split method. This is a common practice in supervised learning to assess model performance. Next we need to create a linear regression model, so we initialize a model by calling the linear regression class and set it equal to the variable name model. So model is now an object for an instance of the linear regression class, and has access to all of its attributes and methods. Next we fit the model, which means we train the model on the training data using the model dot fit method. Then we use the train model to make predictions on the test data. And the last step is to evaluate the model. So we calculate mean squared error, or MSC, and R squared, or R2, as evaluation metrics. MSC measures the average squared difference between predicted and actual values, while R2 measures the goodness of fit of the regression line. And to see what all of this will return, we print the model's coefficients and evaluation metrics. But I prefer a graph type result as well, so we plot the data and regression line so we can better visualize the data points and the regression line. Alright, let's run this code and see what we get. We get coefficients MSC and R2. The coefficient represents the slope of the linear regression line, and in this case the coefficient is approximately 2.9, which means that for every unit increase the input variable X, the predicted output Y is expected to increase by approximately 2.9 units. This coefficient reflects the relationship between the input and output variables. We also got the MSC value. This is a measure of how well the linear regression model fits the data. A lower MSC indicates a better fit. In this case, the MSC is approximately 0.9, which means that on average the predicted values are about 0.9 units away from the actual values. As for R2, this is a measure of the goodness of fit the regression line. It ranges from 0 to 1, with higher values indicating a better fit. In this case, R2 is approximately 0.65, which means that the linear regression model explains about 65% of the variance in the data. This is a medium R2 value, indicating a medium-linear relationship between the input and output variables. And then after these numbers, we see a scatter plot with data points and a linear regression line is usually displayed to visualize how well the line fits the data. So this was a quick example of linear regression. Now let's move on to the next example and I'll show you a logistic regression for classification tasks. Logistic regression is used when the target variable is binary or categorical. It models the probability of a sample belonging to a particular class. We begin by importing the required libraries, including scikit-learned for logistic regression. Then we generate synthetic data with two features. Next, the binary target variable Y. And similar to linear regression, we split the data into training and testing sets. Then we create a logistic regression model, and next we train the model on the training data using the model.fit method. Then we need to make predictions, so we use the trained model to make predictions on the test data. And as always, we need to evaluate the model. So we calculate the accuracy, confusion matrix, and classification report to assess the model's performance. In case you were wondering, the confusion matrix is a fundamental tool in the evaluation of classification models, including logistic regression. It's a two-by-two table that shows true positives, true negatives, and false positives and false negatives. It is often used to assess the performance of a machine learning classifier, providing insights into the model's ability to correctly classify instances into different classes. In the context of logistic regression, the confusion matrix is typically used for binary classification problems, although it can be extended to multi-class problems as well. Okay, so once we calculate this information, we then print the accuracy, confusion matrix, and classification report to evaluate the model. So let's run this code and see what we get back. We first see accuracy, and accuracy is a common classification metric that measures the percentage of correctly predicted labels, and in this case, the accuracy is 95%, which means that 95% of the test data points were correctly classified by the logistic regression model. Next we see the confusion matrix, and it provides detailed information about the model's performance. It's a two-by-two matrix in this binary classification problem, the diagonal elements to and 17 represent the true positives and true negatives. The true positives are the correctly classified positive class, and the true negatives are the correctly classified negative class. As for the other two off-diagonal elements, these represent false positives and false negatives. After the confusion matrix comes the classification report, the classification report provides additional classification metrics, including precision, recall, and F1 score for each class. It also includes macro and weighted averages of these metrics. We also see in this report, the precision and recall for both these 0 and 1 classes. Precision measures how many of the predicted positive instances are actually positive. For class 0, precision is 0.94, and for class 1, precision is 1. We also have a recall column, and this measures how many of the actual positive instances were correctly predicted. For class 0, recall is 0.67, and for class 1, recall is 1. As for the F1 score, this is a harmonic mean of precision and recall, providing a single metric that balances precision and recall. Now we just saw that both linear regression and logistic regression are supervised machine learning techniques, but they are used for different types of problems and have several key high level differences. Thanks for watching.

Learn on the Go

Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.