What is Supervised Learning in Machine Learning?

Supervised Learning is a technique of artificial intelligence and mainly machine learning. It is defined by its use of labeled datasets to train algorithms and then predict which class the new data belongs to.

As input data is fed into the model, it adjusts its weights until the model has been fitted appropriately, which occurs as part of the cross-validation process.

Types of Supervised Learning Algorithms

There is a wide range of supervised learning algorithms but we’ll be elaborating on regression and classification algorithms.


It is used to predict numeric variables. There are two variables in regression, one is independent and the other is dependent. Independent variables (IVs) are the ones that you include in the model to explain or predict changes in the dependent variable.

The dependent variable (DV) is what you want to use the model to explain or predict. The dependent variable is denoted "Y" and the independent variables are denoted by "X".The values of this variable depend on other variables. Regression trees (Random forest), Linear regression are good examples of Regression.


It is used to predict absolute variables. Classification means categorizing the data into parts and predicting unclassified data on their basis. Moreover, Decision trees and logistic regression are good examples of classification.


There are many supervised machine learning techniques but we are going to use KNN. KNN predicts the objects that are close to its dataset.

KNN(K Nearest Neighbor classifier)

  • Import the Classifier from sklearn
    from sklearn.neighbors import KNeighborsClassifier
  • Create arrays for the features and the response variable
    y = df['tar'].values
    X = df.drop('tar', axis=1).values
  • Create a KNN classifier with 6 neighbors/Clusters
    KNN = KNeighborsClassifier(n_neighbors = 6)
  • Fitting the classifier to the data
  • Make Predictions with new data value
    new_prediction = KNN.predict(X_new)

Choosing The Right Number Of Clusters To Predict Data More Accurately

The accuracy of the model can be increased by selecting the right cluster value and what number of clusters will be suitable for more accurate results.

If there are fewer clusters or more clusters as compared to best clustering then there will be errors in the model. So, the best technique is to identify the more accurate no of clusters.

#Setup arrays to store train and test accuracies
neighbors = np.arange(1, 9)
train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))

#Define a  Loop over different values of k
for i, k in enumerate(neighbors):

#Setup a KNN Classifier with k neighbors/Clusters
KNN = KNeighborsClassifier(n_neighbors=k)

#Fit the classifier to the training data
KNN.fit(X_train, y_train)

#Compute accuracy on the training set
train_accuracy[i] = KNN.score(X_train, y_train)

#Compute accuracy on the testing set
 test_accuracy[i] = KNN.score(X_test, y_test)

#Generating plot
plt.title('k-NN: Varying Number of Neighbors')
plt.plot(neighbors, test_accuracy, label = 'Testing Accuracy')
plt.plot(neighbors, train_accuracy, label = 'Training Accuracy')
plt.xlabel('Number of Neighbors')

  • Line#1: Defining two variables  train_accuracy  and test_accuracy to store the values of test and training data accuracy.
  • Line#2: Make a loop till the number of neighbors.
  • Line#3: It will set up a KNN Classifier with k neighbors/Clusters.
  • Line#4: It will fit the classifier to the training data.
  • Line#5 & 6: Computer the accuracies of training and testing dataset. 
  • Line#7: It will make a graph of the number of Neighbors on its x-axis and Accuracy on its y-axis.


K Nearest Neighbor Classifier

Steps to Build a Machine Learning Model

There are some steps to follow to make a supervised machine learning model.

  1. Creating the Training dataset.
  2. Identify the features of your object or which you want to predict.
  3. Choose a good and desired learning model to train data.
  4. Test your model with some testing data.
  5. Deploy your model to see performance on unseen data.

Real-World Machine Learning Example

1. Spam Detection

In Spam Detection, the dataset is collected and the machine learning model is trained on the dataset. Using supervised classification algorithms, organizations can train databases to recognize patterns or anomalies in new data to organize spam and non-spam-related correspondences effectively.

2. Customer Sentiment Analysis

In Customer Sentiment Analysis, the dataset is collected and the machine learning model is trained on the dataset. It’s very useful nowadays to increase the percentage of customers to any brand. If a system analyzes the Sentiments of customers then the Organization will improve and then automatically the customer rate will increase.

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...