Menu
Coddy logo textTech

What is a ML model

Lesson 3 of 19 in Coddy's Introduction to Machine Learning course.

A machine learning model aims to make predictions based on historical data. To do this effectively, the model must understand the statistical properties of the dataset. This understanding cannot be achieved by simply providing the model with an algorithm or mathematical function that describes the data distribution.

Instead, the model must learn these properties through training. Furthermore, each dataset has unique statistical properties. Therefore, a separate model must be trained for each distinct dataset.

 

The learning phase is called fitting or fit. After the model learns the data distribution, we can feed it new data it has never seen and predict its output, this phase is called predicting or predict.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

We will create the simplest supervised learning machine-learning model in this challenge. This model is not used in any way in the industry. Rather, the whole purpose of this model is to demonstrate what a real machine-learning model would look like.

Create a model that returns the most frequent y value for the corresponding X values.

Instructions: 

  • The fit method counts for each X the most frequent y and stores the result in the self.mode variable
    For example for the following dataset:

    <strong>X</strong>ABAAB
    <strong>Y</strong>11011

    self.mode should hold: {"A": 1, "B": 1}

  • The predict method receives a list of X_test and returns a list of the same length. Each value in the returned list is the predicted y corresponding to X, using the self.mode learned in the fit phase.
    for example if self.mode holds {"A": 1, "B": 1}  and X_train is ['A', 'B', 'C'] then the output should be: [1, 1, None]

How to pass the test cases:

Create a function named train_and_predict that receives X_train, y_train, and X_test lists. It will follow the following steps:

  • Learn using the fit method with X_train and y_train 
  • predict the labels of X_test
  • return the predicted labels

Try it yourself

import numpy as np
np.set_printoptions(legacy='1.13')

class SimpleModel:
    def __init__(self):
        self.mode = {}

    def fit(self, X, y):
        # Write your code here
        

    def predict(self, X):
        # Write your code here

def train_and_predict(X_train, y_train, X_test):
    model = SimpleModel()
    # Fit the model with X_train and y_train
    
    # Predict the labels of X_test
    
    # Return the predicted labels

All lessons in Introduction to Machine Learning