Supervised Learning with Scikit-learn
Tags: Artificial Intelligence, Supervised Learning, Regression, Machine Learning, Classification

So let's start with what is supervised learning, how is it different from unsupervised learning, what are its practical applications, and how to implement supervised learning with the Scikit-learn library of python.

Supervised Learning is an approach to create Artificial Intelligence where we provide input and expected output results to the supervised learning model and the model has the task to detect underlying patterns and relationships. This helps the model to perform well on data that it has never seen before and predict the output results. The opposite of supervised learning is unsupervised learning where the model tries to make sense of data itself without any guidelines and expected results from the data. Unsupervised learning models comprehend the data and try to detect similarities and patterns in the data, and categorizes it.

Supervised Learning can be broadly classified into two subcategories-

Regression- It is a kind of Supervised Learning algorithm where the algorithms learn from labeled datasets and it is able to predict a continuous-valued output for new data fed to the algorithm. Example of regression algorithms is- Predicting housing price, Predict Car price, Predicting Crime rates, etc. The major regression algorithms are- Linear Regression and Logistic Regression. Classification- Classification algorithms are kind of learning algorithms where they have to classify the new data into different classes. Unlike Regression, it doesn't have continuous-valued output. It is used where we need binary output, For example- Is the tumor malignant or benign, Does it will be a sunny day or not, Is a mail Spam or not, etc. The major Classifiers are- Decision Tree, Naive Bayes Classifier, and Support Vector Machines(SVM).

Supervised learning is widely used in the practical world. The major practical applications of supervised learning are as follows-

Spam Detection- This is used by Gmail, rediffmail, etc. It identifies words from the mail which can make it more likely to be spam mail. SO it classifies mails by identifying suspicious words in the mails. It keeps learning and improves with time. Speech Recognition- It is an application we see in Alexa, Siri, Google Assistant, Cortana, etc. We teach the algorithm about our voice and how to interpret it. It keeps learning every time we use it and become more efficient. Object Recognition- It is an application we see at various places. In this first, we train the algorithm on a large set of data, and then the algorithm recognizes different objects. It is largely used in Traffic systems, security systems, etc. Bioinformatics- This is one of the most widely used applications of supervised learning algorithms, It is used in our smartphones for facial recognition, in-screen fingerprint, etc. It stores our biological information and uses it to recognize us and it gets better every time we use it. Recommender Systems-It is used by popular sites like Netflix, Amazon Prime, Hotstar, etc. It recommends new movies for us based on our searches and previously watched movies.

Now let's see how to implement Supervised Learning Algorithms by scikit learn with the help of an example problem. The example problem is “Predicting Life expectancy using Gapminder Dataset”.

But first, let's set some insights about scikit-learn. Scikit-learn is the most robust and useful library for machine learning in Python. It provides us with a variety of efficient tools for statistical modeling and machine learning. It is largely written in python and built upon other python libraries like NumPy, Matplotlib, and SciPy.

Let's Code for our problem statement i.e “Predicting Life expectancy using Gapminder Dataset”. It comes under the regression category, ok let's see how to use scikit-learn to implement the Supervised Learning model-

You can get information about the Gapminder dataset from here-Gapminder_Dataset.

I will try to give an overview of the code without going deeper into technical definitions. So that it will help to get a holistic view of Scikit-Learn and Supervised Learning.

import numpy as np import pandas as pd # Read the CSV file into a DataFrame 'df' df = pd.read_csv('gapminder.csv') y = df['life'].values X = df['fertility'].values y = y.reshape(-1,1) X = X.reshape(-1,1)

In this Code block, we have imported NumPy, pandas, and our gapminder dataset. After that, we have splitter our dataset into the target variable ‘y’ and features ‘X’. We then reshaped it so that it fits according to our needs.

from sklearn.linear_model import LinearRegression # Create the regressor 'reg' reg = LinearRegression()

Here we have imported LinearRegression from sklearn.linear_model module. And we have created an instance of it called ‘reg’.

# Create the prediction space

Here we have preloaded the fertility column in X to numpy array X_fertiity. prediction_space = np.linspace(min(X_fertility), max(X_fertility)).reshape(-1,1) # Fit the model to the data reg.fit(X_fertility,y) # Calculating the prediction y_pred = reg.predict(prediction_space) # Print R^2 error print(reg.score(X_fertility,y)) # reg score is=0.6192442167740035 # Plotting the regression line plt.plot(prediction_space, y_pred, color='black', linewidth=3) plt.show()

Here we have trained and tested our regressor ‘reg’ by using fertility as a feature and calculated accuracy and plotted the distribution of our fitted linear regression line.

# Import necessary modules from sklearn from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split # Creating X_train, X_test, y_train, y_test X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.3, random_state=42) # Create the regressor reg_a reg_a = LinearRegression() # Fit the regressor to the training data reg_a.fit(X_train,y_train) # Predict on the test data 'y_pred' y_pred = reg_a.predict(X_test) # Compute and print R^2 and RMSE print("R^2: {}".format(reg_a.score(X_test, y_test))) #Prints , R^2: 0.838046873142936

rmse = np.sqrt(mean_squared_error(y_test,y_pred))

print("Root Mean Squared Error: {}".format(rmse)) # Prints, Root Mean Squared Error: 3.2476010800377213

In this Code block, we have trained our regressor reg_a with the whole X _train and y_train and tested on X_test and y_test. We had also printed the R² error and accuracy score of the model. We had used the sklearn module and imported LinearRegression, train_test_split, and mean_squared_error module which does all backend calculations. So this model can predict life expectancy now on new data with almost 84 percent accuracy.

So we can see that how easy it is to implement supervised learning models using Scikit-Learn. This is the power of Scikit-Learn which helps to easily implement any model without worrying about complex calculations.