Mastering Machine Learning Models: A Comprehensive Guide

Kaif
6 min readMar 12, 2023

--

AI generated art using Midjourney AI Discord Bot

Machine Learning (ML) is an exciting and rapidly evolving field that involves building systems that can automatically learn from data. If you’re like me and have recently embarked on a journey to learn ML, you may have encountered a lot of confusing terminology and mathematical concepts. But don’t worry, with the right approach and some persistence, anyone can master the basics of ML and start building their own models.

I’m learning ML since early 2021 and like every other curious student, I started understanding basics of AI. I started learning the key concepts on ML since my junior year as an undergrad. I’m assuming you’re familiar with Python and some common libraries like Pandas etc. If not you can start with understand that and come back to this article later! Anyways, the optimal way to learn ML is to start finding answers of these questions:

  • What is AI?
  • What is Simple AI and Complex AI?
  • What comprises of AI?
  • What is ML and DL?
  • What Mathematical concepts are required for Machine Learning?
  • Types of ML Models? [Understand all basic ML Models Supervised and Unsupervised]
  • What is Bias, Variance, Loss Functions in ML?
  • What are Neural Networks?
  • What are Decision Trees and Tree Ensembles?
  • Types of Layers in Neural Networks? [Basically, Different Type of Neural Networks]
  • What is TensorFlow & PyTorch?

By answering these questions, you’ll have a solid foundation of knowledge to build upon. Once you’ve learned the basics of ML, you can start practicing by building your own models. However, it’s important to practice until you fully understand the purpose of each ML model and its use case.

How to start practicing ML Models?

One great resource is Kaggle, a platform similar to GitHub that’s solely focused on ML and data science. On Kaggle, you can find free ML models, datasets, personalized algorithms, and much more. Start by downloading datasets for various ML algorithms so you can begin practicing. By this point, you should have a basic understanding of NumPy, Pandas, and at least one of the two libraries TensorFlow or PyTorch. To gain more exposure and hands-on experience, I recommend learning the Sci-Kit library for Python, another useful ML library.

While learning about ML, you may have noticed that a lot of it involves mathematics. In fact, most of AI, ML, and data science are just mathematical expressions. Therefore, it’s crucial to understand the math behind each algorithm and every activation layer in your Neural Network. To truly master each ML algorithm, I’ve come up with a solution that worked for me.

You start by taking an algorithm you want to start with and download a medium sized dataset for it. Now you start with writing the Model in NumPy (Yes, you read it correctly), now write the same model Sci-Kit Library, do the same by writing the same model in TensorFlow and PyTorch. Use the same Data Set for all of these. Writing a ML Model in each of these libraries has a greater meaning, writing an ML Model in NumPy makes you really understand the Math underlying that model, companies would find it very impressive if you know the math behind each ML algorithm and not just writing super easy TensorFlow models within 100–200 lines of code and go home and come back the next day and repeat! Writing in Sci-kit is more like a bonus learning for you which is still valid and really helpful.

To make this more concrete, let me demonstrate a ML Model.

Writing a House Price Prediction Linear Regression model using NumPy

import numpy as np
import pandas as pd

data = pd.DataFrame({
'Size': [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700],
'Bedrooms': [3, 3, 2, 4, 2, 3, 4, 4, 3, 3],
'Price': [245000, 312000, 279000, 308000, 199000, 219000, 405000, 324000, 319000, 255000]
})

X = data[['Size', 'Bedrooms']].values
Y = data['Price'].values.reshape(-1, 1)

X = np.hstack((np.ones((len(X), 1)), X))

def linear_regression(X, Y):
beta = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(Y)
return beta

beta = linear_regression(X, Y)
print(beta)

new_data = pd.DataFrame({
'Size': [1800, 2000],
'Bedrooms': [3, 4]
})

new_X = new_data[['Size', 'Bedrooms']].values
new_X = np.hstack((np.ones((len(new_X), 1)), new_X))

new_Y = new_X.dot(beta)

print(new_Y)

Writing the same model using Sci-kit Learn

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

data = pd.DataFrame({
'Size': [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700],
'Bedrooms': [3, 3, 2, 4, 2, 3, 4, 4, 3, 3],
'Price': [245000, 312000, 279000, 308000, 199000, 219000, 405000, 324000, 319000, 255000]
})

X = data[['Size', 'Bedrooms']]
Y = data['Price']

model = LinearRegression()
model.fit(X, Y)

new_data = pd.DataFrame({
'Size': [1800, 2000],
'Bedrooms': [3, 4]
})

new_Y = model.predict(new_data)

print(new_Y)
print(model.intercept_)
print(model.coef_)

Writing the same model using TensorFlow

import numpy as np
import pandas as pd
import tensorflow as tf

data = pd.DataFrame({
'Size': [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700],
'Bedrooms': [3, 3, 2, 4, 2, 3, 4, 4, 3, 3],
'Price': [245000, 312000, 279000, 308000, 199000, 219000, 405000, 324000, 319000, 255000]
})

X = data[['Size', 'Bedrooms']].values
Y = data['Price'].values.reshape(-1, 1)

# Initialize variables with random values
W = tf.Variable(tf.random.normal([2, 1]), name='weights')
b = tf.Variable(tf.random.normal([1]), name='bias')

# Define placeholders for input and output
X_placeholder = tf.placeholder(tf.float32, [None, 2], name='X')
Y_placeholder = tf.placeholder(tf.float32, [None, 1], name='Y')

# Define linear regression model
linear_regression = tf.matmul(X_placeholder, W) + b

# Define loss function (mean squared error)
loss = tf.reduce_mean(tf.square(linear_regression - Y_placeholder))

# Define optimizer (gradient descent)
optimizer = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)

# Initialize TensorFlow session
sess = tf.Session()

# Initialize variables
sess.run(tf.global_variables_initializer())

# Train model
for i in range(10000):
_, loss_value = sess.run([optimizer, loss], feed_dict={X_placeholder: X, Y_placeholder: Y})
if i % 1000 == 0:
print("Loss at step {}: {}".format(i, loss_value))

# Get final parameter values
W_value, b_value = sess.run([W, b])
print("Parameter values:")
print("W: {}".format(W_value))
print("b: {}".format(b_value))

# Use model to predict new house prices
new_data = pd.DataFrame({
'Size': [1800, 2000],
'Bedrooms': [3, 4]
})

new_X = new_data[['Size', 'Bedrooms']].values
new_Y = np.matmul(new_X, W_value) + b_value

print("Predict")

Writing the same model using PyTorch

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim

data = pd.DataFrame({
'Size': [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700],
'Bedrooms': [3, 3, 2, 4, 2, 3, 4, 4, 3, 3],
'Price': [245000, 312000, 279000, 308000, 199000, 219000, 405000, 324000, 319000, 255000]
})

X = data[['Size', 'Bedrooms']].values.astype(np.float32)
Y = data['Price'].values.reshape(-1, 1).astype(np.float32)

class LinearRegression(nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(2, 1)

def forward(self, x):
out = self.linear(x)
return out

model = LinearRegression()

# Define loss function (mean squared error)
criterion = nn.MSELoss()

# Define optimizer (gradient descent)
optimizer = optim.SGD(model.parameters(), lr=0.0001)

# Train model
num_epochs = 10000
for epoch in range(num_epochs):
# Convert inputs and outputs to PyTorch tensors
inputs = torch.from_numpy(X)
targets = torch.from_numpy(Y)

# Clear the gradients
optimizer.zero_grad()

# Forward pass
outputs = model(inputs)

# Calculate loss
loss = criterion(outputs, targets)

# Backward pass and optimize
loss.backward()
optimizer.step()

if epoch % 1000 == 0:
print("Epoch [{}/{}], Loss: {:.4f}".format(epoch+1, num_epochs, loss.item()))

# Get final parameter values
with torch.no_grad():
W_value = model.linear.weight.numpy()
b_value = model.linear.bias.numpy()
print("Parameter values:")
print("W: {}".format(W_value))
print("b: {}".format(b_value))

# Use model to predict new house prices
new_data = pd.DataFrame({
'Size': [1800, 2000],
'Bedrooms': [3, 4]
})

new_X = new_data[['Size', 'Bedrooms']].values.astype(np.float32)
new_X_tensor = torch.from_numpy(new_X)
new_Y_tensor = model(new_X_tensor)
new_Y = new_Y_tensor.detach().numpy()

print("Predicted prices:")
print(new_Y.flatten())

This way it might take you 2 or 3 days to practice every algorithm but surely in the end it’ll be worth it coz now you’re flexible enough to code any model in any library, this method also gives you enough insight that you can come up with your own algorithm which can be much more optimized than existing ones. Practicing this way should take you 75 — 90 days and you can master all ML Models, supervised as well as unsupervised.

Conclusion

AI and Machine Learning are surely tricky but once you become the know-it-all it is very interesting. Remember this strategy can be used everywhere, if you want to master Deep Learning try writing your models in TensorFlow as well as PyTorch and you are good to go.

--

--

Kaif

Enhancing accessibility through #NLProc | B. Tech CSE | AIML | DL | NLP | LLMs