Machine Learning Basics: A Hands-On Overview

Vicky’s Notes
The Power of AI
Published in
6 min readOct 23, 2023

--

This article is a glimpse of the most fundamental machine learning concepts presented through widely recognized code examples from the Machine Learning Basics course on the IBM Skills Network. Take a look because it breaks down complex machine learning ideas into easily with digestible code examples, allowing beginners to grasp essential concepts of machine learning with confidence.💪

Introduction to Machine Learning

Machine learning is a subset of artificial intelligence that focuses on building algorithms that can learn from data. It involves the creation of models that can make predictions or decisions without being explicitly programmed. For instance, teaching a computer to differentiate between cats and dogs is a classic machine learning problem. You don’t provide explicit instructions; instead, the computer learns to recognize each animal by analyzing numerous labeled images, a process akin to how a child learns to identify different animals.

The Toolbox: Python and Its Libraries

Python reigns as a favorite in the machine learning community due to its rich ecosystem of libraries that simplify data handling, analysis, and model building. Libraries like NumPy, Pandas, and Sckkit-learn are go-to tools for data manipulation, analysis, and modeling.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

Preparing the Data: Train/Test Split

Now, before we let our models loose, we’ve got to split our data into a training set and a test set. Think of it like cooking; you taste a bit of the sauce before you serve it up, right? Same deal here. You train with most of your data, but you save a bit to test how well your recipe… I mean, model, turned out.

# Splitting data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

The Two Pillars: Classification and Regression

Then, there are two main problems you’ll solve with machine learning: classification and regression. Classification is like sorting your socks — deciding which pile they belong to. Regression is more like guessing the temperature outside. Not just hot or cold, but the exact number.

Let’s peek at an example. Say we’re working with the famous Iris dataset. We can use a Logistic Regression model to classify the different types of iris flowers. On the flip side, we might want to predict housing prices based on various features like size and location using Linear Regression.

And here’s the kicker: after you’ve trained your model, you gotta know how well it’s doing. For classification, you’ll look at accuracy, precision, recall, and this thing called a confusion matrix — it’s less confusing than it sounds, promise! It basically shows you the times your model got it right or hilariously wrong. Stick around as these concepts will be covered later in this article.

For regression, you’ve got your Mean Absolute Error, Mean Squared Error, and R-squared — which is a fancy way of saying, “How close did we get to the bullseye?”

Here’s a little bit of code to show how you’d get those numbers:

from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score
from sklearn.datasets import load_iris

# Load the Iris dataset
data = load_iris()
X = data.data
y = data.target

# Create a Logistic Regression model
model = LogisticRegression()

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

# Make predictions on the test set
y_pred = model.predict(X_test)
  1. Accuracy: Accuracy is the ratio of correctly predicted instances to the total instances in the dataset. Formula:

Accuracy = Number of Correct Predictions / Total Number of Predictions

2. Confusion Matrix: A confusion matrix is a table used to visualize the performance of a classification algorithm. It shows the counts of true positive, true negative, false positive, and false negative predictions.

Regression

from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.linear_model import LinearRegression

# Load a sample dataset for regression
from sklearn.datasets import load_boston
data = load_boston()
X = data.data
y = data.target

# Create a Linear Regression model
regression_model = LinearRegression()

# Fit the model to the training data
regression_model.fit(X_train, y_train)

# Make predictions on the test set
y_pred = regression_model.predict(X_test)

Measuring Success: Evaluation Metrics

Evaluating machine learning models is essential to determine their performance. Different metrics are used for classification and regression tasks. Let’s take a look at these metrics:

Classification Metrics

  1. Accuracy: Accuracy is the ratio of correctly predicted instances to the total instances in the dataset. Formula:
Accuracy = Total Number of Correct Predictions / Total Number of Predictions​

2. Precision: Precision represents the proportion of correctly predicted positive instances out of all instances predicted as positive. Formula:

Precision = True Positives / (True Positives + False Positives)

3. Recall (Sensitivity): Recall calculates the proportion of correctly predicted positive instances out of all actual positive instances. Formula:

Recall = True Positives / (True Positives + False Negatives)

4. Confusion Matrix: A confusion matrix is a table used to visualize the performance of a classification algorithm. It shows the counts of true positive, true negative, false positive, and false negative predictions.

Code:

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

# Calculate precision
precision = precision_score(y_test, y_pred, average='weighted')
print(f"Precision: {precision:.2f}")

# Calculate recall
recall = recall_score(y_test, y_pred, average='weighted')
print(f"Recall: {recall:.2f}")

# Calculate confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(conf_matrix)

Regression Metrics

  1. Mean Absolute Error (MAE): The MAE represents the average absolute difference between the predicted values and the actual values. Formula:
MAE = (1/n) * Σ | y_i - ŷ_i |

Where:
- n is the number of data points.
- y_i is the actual value for the i-th data point.
- ŷ_i is the predicted value for the i-th data point.

2. Mean Squared Error (MSE): The MSE calculates the average squared difference between predicted and actual values:

MSE = (1/n) * Σ (y_i - ŷ_i)^2

Where:
- n is the number of data points.
- y_i is the actual value for the i-th data point.
- ŷ_i is the predicted value for the i-th data point.
Lower MSE values indicate better performance.

3. R-squared Score (Coefficient of Determination): The R-squared score measures the proportion of variance predictable by the model:

R^2 = 1 - Σ (y_i - ŷ_i)^2 / Σ (y_i - ȳ)^2

Where:
- n is the number of data points.
- y_i is the actual value for the i-th data point.
- ŷ_i is the predicted value for the i-th data point.
- ȳ is the mean of the actual values.
Higher R^2 values indicate a better fit, ranging from 0 to 1.

Code:

# Calculate Mean Absolute Error
mae = mean_absolute_error(y_test, y_pred)
print(f"Mean Absolute Error: {mae:.2f}")

# Calculate Mean Squared Error
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}")

# Calculate R-squared
r2 = r2_score(y_test, y_pred)
print(f"R-squared: {r2:.2f}")

Conclusion

Let’s wrap up what we’ve learned about Machine Learning. We went over the basic ideas, starting with what machine learning is and then looking at different types. We spent some time on popular methods like Linear and Logistic Regression and how to see if they’re working well.

If you’re hungry for more, you can totally check out IBM CognitiveClass.ai to get certificates and badges on other cool data science topics. All this amazing knowledge is available for free!

Let’s keep learning and growing. 🌱 🚀 There’s a lot more out there for us to discover!

For more info, you can visit:

  1. Machine Learning Basics at CognitiveClass.ai
  2. CognitiveClass.ai’s courses

--

--

Vicky’s Notes
The Power of AI

An IT enthusiast driven by the belief that success is not just about personal achievement but inspiring others to excel.