Welcome to Deep Learning in Python

Discover deep learning in Python, how it differs from traditional machine learning, and how neural networks power AI breakthroughs like image generation and AlphaGo. Includes a simple Keras example predicting baseball game attendance.

Welcome to Deep Learning in Python

Deep learning has become one of the most exciting fields in computer science, powering breakthroughs in computer vision, natural language processing, autonomous vehicles, and game-playing AI. But what exactly is deep learning, and how is it different from traditional machine learning?


What Is Deep Learning?

At its core, deep learning is a subset of machine learning that uses neural networks with multiple layers (hence "deep") to automatically learn patterns from data. Instead of manually crafting features, deep learning models can learn directly from raw data — whether that’s pixels, audio waveforms, or text tokens.


Deep Learning vs. Machine Learning

While machine learning and deep learning are related, there are some key differences:

  • Feature engineering:
    • Machine Learning: Requires humans to design features.
    • Deep Learning: Learns features automatically.
  • Scalability:
    • Machine Learning: Works well with smaller datasets.
    • Deep Learning: Excels with large datasets and GPUs.
  • Performance:
    • Machine Learning: Great for structured/tabular data.
    • Deep Learning: Superior in high-dimensional tasks like vision, language, and speech.

Neural Networks: The Heart of Deep Learning

A neural network consists of interconnected layers of nodes (neurons) that transform inputs into outputs through weighted connections and nonlinear activations.

Types of neural networks include:

  • Feedforward networks – basic architecture for regression/classification.
  • Convolutional Neural Networks (CNNs) – excel at image processing.
  • Recurrent Neural Networks (RNNs) and Transformers – powerful for sequential data like text or speech.
  • Reinforcement Learning with Deep Networks – where systems like DeepMind’s AlphaGo used deep learning to beat world champions in the game of Go.

Real-World Applications of Deep Learning

  • Image generation (Stable Diffusion, GANs).
  • Language models (ChatGPT, BERT, LLaMA).
  • Game AI (DeepMind’s AlphaGo).
  • Healthcare (disease prediction from scans).
  • Recommendation systems (Netflix, Amazon).

A Simple Example: Predicting Baseball Game Attendance

Let’s walk through a toy example of using deep learning to predict baseball game attendance.

Our dataset has numeric inputs such as:

  • Opposing team (encoded as a number).
  • Weather forecast (0 = clear, 1 = rainy).
  • Starting pitcher (categorical, mapped to numbers 1–5).
  • Day of the week (1–7).

The output is a single number: attendance.


Python Example (with Keras)

import numpy as np
from tensorflow import keras
from tensorflow.keras import layers

# Example dataset (features: team, weather, pitcher, day_of_week)
# Each row is one game
X = np.array([
    [1, 0, 3, 5],
    [2, 1, 1, 6],
    [3, 0, 5, 7],
    [1, 0, 2, 2],
    [4, 1, 4, 3],
    [2, 0, 1, 4]
], dtype=float)

# Example labels (attendance)
y = np.array([32000, 25000, 41000, 28000, 22000, 35000], dtype=float)

# Normalize data (good practice for neural networks)
X = X / np.max(X, axis=0)

# Define a simple feedforward neural network
model = keras.Sequential([
    layers.Dense(16, activation='relu', input_shape=(4,)),
    layers.Dense(8, activation='relu'),
    layers.Dense(1)  # Regression output
])

# Compile the model
model.compile(optimizer='adam', loss='mse', metrics=['mae'])

# Train the model
model.fit(X, y, epochs=50, verbose=1)

# Predict attendance for a new game
new_game = np.array([[3, 1, 2, 6]])  # team=3, rainy=1, pitcher=2, Saturday
new_game = new_game / np.max(X, axis=0)
prediction = model.predict(new_game)
print(f"Predicted attendance: {prediction[0][0]:.0f}")

Steps in Training the Model

  1. Collect Data: Historical game attendance with numeric features.
  2. Preprocess Data: Normalize values and encode categorical inputs.
  3. Define Model: Build a feedforward neural network.
  4. Train Model: Use training data to adjust weights via backpropagation.
  5. Evaluate: Check mean absolute error (MAE) or root mean squared error (RMSE).
  6. Predict: Use the trained model to forecast attendance.

Why This Matters

Even this simple example shows how deep learning can turn raw data into actionable predictions. A stadium manager could use such a model to decide:

  • How many temp staff to hire.
  • How much food and drink inventory to stock.
  • Whether extra transportation should be scheduled.

Final Thoughts

Deep learning isn’t magic — it’s math plus lots of data. But its ability to scale, adapt, and outperform traditional methods in complex tasks has made it a cornerstone of modern AI.

Whether you’re generating images, building chatbots, or predicting baseball attendance, deep learning opens the door to smarter, data-driven decisions.

Deep Learning

Learn More

Read more