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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyWe 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
fitmethod counts for eachXthe most frequentyand stores the result in theself.modevariable
For example for the following dataset:<strong>X</strong>A B A A B <strong>Y</strong>1 1 0 1 1 self.modeshould hold:{"A": 1, "B": 1}- The
predictmethod receives a list ofX_testand returns a list of the same length. Each value in the returned list is the predictedycorresponding toX, using theself.modelearned in thefitphase.
for example ifself.modeholds{"A": 1, "B": 1}andX_trainis['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