OOP for Machine Learning

The Classes in Python are the main tool used for Object-Oriented Programming (OOP). A Class is a coding structure and a specific tool to implement new kinds of objects in Python that supports inheritance. OOP offers a very different and effective way of programming. We can use OOP for Machine Learning also to build Models.

The use of OOP is entirely optional in Machine Learning as we already have libraries like Scikit-learn and TensorFlow from where we can easily use algorithms. So learning Object-Oriented Programming for Machine Learning is not that necessary, but as a programmer, you should not limit yourself. So in this article, I will take you through how you can use OOP for Machine Learning to build models.

Object-Oriented Programming (OOP) for Machine Learning

To illustrate the process of using OOP for Machine Learning, I will use a dataset which is based on Weather, can be easily downloaded from here. As we are using Object Oriented Programming so we don’t need any of the machine learning packages except pandas, which we will use to read the data, and Scikit-Learn to use a Machine Learning Algorithm and Numpy for Numerical Python. So let’s import pandas and get started with OOP for Machine Learning.

import pandas as pd
from google.colab import files
uploaded = files.upload()
class Model:
  def __init__(self, datafile = "weatherHistory (1).csv"):
    self.df = pd.read_csv(datafile)
if __name__ == '__main__':
    model_instance = Model()
    print(model_instance.df.head())Code language: Python (python)
Formatted Date  ...                      Daily Summary
0  2006-04-01 00:00:00.000 +0200  ...  Partly cloudy throughout the day.
1  2006-04-01 01:00:00.000 +0200  ...  Partly cloudy throughout the day.
2  2006-04-01 02:00:00.000 +0200  ...  Partly cloudy throughout the day.
3  2006-04-01 03:00:00.000 +0200  ...  Partly cloudy throughout the day.
4  2006-04-01 04:00:00.000 +0200  ...  Partly cloudy throughout the day.

The next thing to do here is defining a class to call a machine learning model. Just to keep it simple, I will use a Linear Regression model for this task:

import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.linear_model import LinearRegression
class Model:
  def __init__(self, datafile = "weatherHistory (1).csv"):
    self.df = pd.read_csv(datafile)
    self.linear_reg = LinearRegression()Code language: Python (python)

The next step is to split the data into a training set and a test set. For this, I will import train_test_split provided by Scikit-Learn, and then I will create a function to split the data into training and test sets:

from sklearn.model_selection import train_test_split
import numpy as np
def split(self, test_size):
  X = np.array(self.df[['Humidity', 'Pressure (millibars)']])
  y = np.array(self.df['Temperature (C)'])
  self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size = test_size, random_state = 42)Code language: Python (python)

Now, I will define a function to fit the data into our machine learning model:

def fit(self):
  self.model = self.linear_reg.fit(self.X_train, self.y_train)Code language: Python (python)

Now let’s create a function to predict using our machine learning model:

def predict(self):
        result = self.linear_reg.predict(self.X_test)
        return resultCode language: Python (python)

Now, let’s run the complete file with training set as 80 per cent, and the test set as 20 per cent. For this, I will write the whole code with proper indentation. And at the end I will also print the accuracy of our model:

import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.linear_model import LinearRegression
class Model:
  def __init__(self, datafile = "weatherHistory (1).csv"):
    self.df = pd.read_csv(datafile)
    self.linear_reg = LinearRegression()
  def split(self, test_size):
    X = np.array(self.df[['Humidity', 'Pressure (millibars)']])
    y = np.array(self.df['Temperature (C)'])
    self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size = test_size, random_state = 42)
  def fit(self):
    self.model = self.linear_reg.fit(self.X_train, self.y_train)
  def predict(self):
    result = self.linear_reg.predict(self.X_test)
    return result
if __name__ == '__main__':
    model_instance = Model()
    model_instance.split(0.2)
    model_instance.fit() 
    print(model_instance.predict())   
    print("Accuracy: ",     model_instance.model.score(model_instance.X_test, model_instance.y_test))Code language: Python (python)

[18.27024238 8.9966463 14.55962648 … 18.26962999 17.96249378 16.41528188]
Accuracy: 0.39578560465686424

Also, Read – Web Scraping to Create CSV.

So in this way, we can easily use Object-Oriented Programming for Machine Learning. I hope you liked this article on using OOP for Machine Learning. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.

Also, Read – How to Start with Machine Learning?

Follow Us:

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1538

Leave a Reply