Lesson 2: Fundamentals of Machine Learning and Neural Networks

📚 Learn AI 📁 Technology

Lesson 2: Fundamentals of Machine Learning and Neural Networks

Introduction & Hook

Imagine a world where computers can recognize faces, recommend movies you’ll love, predict health risks, and even drive cars. What powers these incredible feats? At the heart of modern Artificial Intelligence (AI) lie Machine Learning (ML) and Neural Networks. This lesson unveils how machines can “learn” from data, enabling them to perform tasks that once seemed exclusive to human intelligence. By understanding these foundations, you unlock the door to building smart systems that solve real-world challenges—from diagnosing diseases to optimizing logistics, and far beyond.

Learning Objectives

  • Define and distinguish between supervised, unsupervised, and reinforcement learning paradigms.
  • Explain the basic structure and functioning of neural networks.
  • Implement a simple machine learning model in Python using industry-standard libraries.
  • Identify real-world applications of machine learning and neural networks.
  • Interpret the results of basic machine learning experiments.

Key Terminology

  • Machine Learning (ML): A subset of AI focused on algorithms that enable computers to learn from and make predictions or decisions based on data.
  • Neural Network: A computational model inspired by the human brain, consisting of interconnected nodes (“neurons”) that process information in layers.
  • Supervised Learning: A learning paradigm where a model is trained on labeled data (input-output pairs).
  • Unsupervised Learning: A learning paradigm where a model finds patterns or structures in data without explicit labels.
  • Activation Function: A mathematical function applied to a neuron’s output to introduce non-linearity, enabling neural networks to learn complex mappings.

Core Instructional Content

What is Machine Learning?

Machine Learning is the science of enabling computers to act without being explicitly programmed for every possible scenario. Instead of following rigid instructions, a machine learning system “learns” patterns from historical data and applies this knowledge to new, unseen information. For example, an email spam filter learns which messages are likely spam based on a dataset of labeled emails.

At its core, machine learning is about building models—mathematical representations of real-world processes. The model adjusts itself based on input data, using algorithms to “train” and “improve” over time.

Types of Machine Learning

  • Supervised Learning: The most common approach, using labeled datasets where the correct answer is known (e.g., classifying images of cats and dogs).
  • Unsupervised Learning: Extracts structure from unlabeled data (e.g., grouping customers by purchasing behavior without predefined categories).
  • Reinforcement Learning: Focuses on agents that learn to make decisions by receiving rewards or penalties (e.g., teaching a robot to walk or a computer to play chess).

The choice of paradigm depends on the problem and the availability of labeled data.

The Anatomy of a Neural Network

A neural network is a collection of connected units or nodes called “neurons.” These nodes are organized into layers:

  • Input Layer: Receives the raw data (e.g., pixel values for images).
  • Hidden Layers: Intermediate layers that transform the inputs through weighted connections and activation functions. The more hidden layers, the deeper the network (hence “deep learning”).
  • Output Layer: Produces the final prediction (e.g., probability of an image being a cat or dog).

Each connection between neurons has an associated “weight,” which is adjusted during training to minimize prediction errors.

How Neural Networks Learn

Training a neural network involves feeding it data, comparing its predictions to the actual outcomes (labels), and then updating the weights to reduce errors. This process, called backpropagation, uses calculus-based optimization (commonly gradient descent).

Mathematically, the prediction of a neuron is:

output = activation(weighted_sum(inputs) + bias)

Where the activation function (like sigmoid, relu, or tanh) introduces non-linearity, allowing the network to model complex relationships.

Building a Simple Machine Learning Model: Python Example

Let’s walk through a basic supervised learning task: classifying whether a flower is an Iris Setosa or not, using the scikit-learn library.


# Import libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Load dataset
iris = load_iris()
X, y = iris.data, iris.target

# For binary classification, focus on Setosa vs non-Setosa
import numpy as np
y_binary = (y == 0).astype(int)

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2, random_state=42)

# Initialize and train model
model = LogisticRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate accuracy
print("Accuracy:", accuracy_score(y_test, y_pred))

This code demonstrates the entire ML workflow: data loading, splitting, model training, prediction, and evaluation.

Constructing a Neural Network: Keras Example

Let’s build a neural network using TensorFlow and Keras to classify digits from the classic MNIST dataset:


# Import libraries
import tensorflow as tf
from tensorflow import keras

# Load dataset
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()

# Normalize data
X_train = X_train.reshape(-1, 28*28) / 255.0
X_test = X_test.reshape(-1, 28*28) / 255.0

# Build model
model = keras.Sequential([
    keras.layers.Dense(128, activation='relu', input_shape=(784,)),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dense(10, activation='softmax')  # 10 classes
])

# Compile model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train model
model.fit(X_train, y_train, epochs=5, batch_size=32)

# Evaluate model
test_loss, test_acc = model.evaluate(X_test, y_test)
print("Test accuracy:", test_acc)

This example highlights the power and flexibility of neural networks for complex tasks like image recognition.

Comparing ML Algorithms and Neural Networks

Algorithm Data Type Strengths Limitations
Logistic Regression Tabular, linearly separable Simple, interpretable Limited to linear relationships
Decision Trees Tabular, non-linear Handles non-linearity, easy to visualize Prone to overfitting
Neural Networks Images, text, tabular Can model complex, non-linear patterns Requires more data and computational power

Practical Application & Case Study

Consider a hospital seeking to reduce patient readmissions. By collecting data on patients’ demographics, diagnoses, treatments, and previous admissions, they can train a supervised learning model to predict which patients are at higher risk of readmission. For even more accuracy, a neural network can incorporate both structured data and unstructured notes from doctors. This enables hospital staff to proactively intervene, improving patient outcomes and reducing costs.


# Example: Predicting readmission risk (simplified)
from sklearn.ensemble import RandomForestClassifier

# Assume X_patient and y_readmit are loaded from hospital records
# X_patient: patient features, y_readmit: 1 if readmitted, 0 otherwise

model = RandomForestClassifier()
model.fit(X_patient, y_readmit)

# Predict risk for a new patient
risk_score = model.predict_proba([new_patient_features])[0][1]
print("Predicted readmission risk:", risk_score)

In practice, such models are validated for fairness, accuracy, and ethical implications before being deployed in real clinical settings.

Knowledge Check

  • 1. Which type of machine learning uses labeled data to train models?
    • A. Supervised Learning
    • B. Unsupervised Learning
    • C. Reinforcement Learning
    • D. Transfer Learning
  • 2. What is the function of an activation function in a neural network?
  • 3. In your own words, describe a real-world scenario where unsupervised learning might be useful.
  • 4. What is the purpose of splitting data into training and testing sets?

Summary & Next Steps

You’ve just taken a significant step in your AI journey by mastering the fundamentals of machine learning and neural networks. You now understand the main types of ML, how neural networks are structured, and how these models are trained and evaluated. With hands-on examples in Python, you’ve seen how theory translates into practice.

As we continue, you’ll dive deeper into model evaluation, overfitting, and advanced neural architectures. Next, we’ll explore techniques for improving model performance and ensuring reliable, fair AI systems. Keep experimenting and reflecting on how these concepts apply to real-world challenges you encounter or care about.

Recommended Resources: