Lesson 1: Foundations of Machine Learning and Python
Introduction
Welcome to your first step into the world of Artificial Intelligence and Machine Learning! In this lesson, we'll cover the basic concepts of AI/ML, essential Python programming for ML, and touch on the fundamental mathematical concepts that power these technologies.
Don't worry if you're new to coding or if math isn't your strong suit. We'll take it step by step, focusing on practical applications and hands-on learning. By the end of this lesson, you'll have created your first ML-related project!
What is Machine Learning?
Machine Learning is like teaching a computer to learn from experience, much like how humans learn. Instead of programming explicit instructions for every task, we provide data and let the computer figure out patterns and make decisions based on what it has "learned."
Imagine teaching a child to recognize dogs. You wouldn't give them a list of exact measurements or features. Instead, you'd show them many pictures of dogs, and over time, they'd learn to recognize dogs, even ones they haven't seen before. That's similar to how machine learning works!
Python: The Language of ML
Python is like the Swiss Army knife of programming languages for ML. It's versatile, relatively easy to learn, and has a vast ecosystem of libraries that make ML tasks much simpler.
Let's start with some basic Python code:
# This is a comment in Python
# Variables and basic operations
x = 5
y = 2 * x + 1
print(f"When x is {x}, y is {y}")
# Lists
numbers = [1, 2, 3, 4, 5]
print("First number:", numbers[0])
print("Last number:", numbers[-1])
# For loop
for num in numbers:
print(num ** 2)
# Function definition
def square(n):
return n ** 2
# Using the function
result = square(4)
print("4 squared is:", result)
# Importing a library
import numpy as np
# Creating a numpy array
arr = np.array([1, 2, 3, 4, 5])
print("Mean of the array:", np.mean(arr))
This code demonstrates variables, lists, loops, functions, and using a library (NumPy). These are fundamental concepts you'll use frequently in ML projects.
Basic Linear Algebra and Calculus for ML
Don't let these terms scare you! In ML, we often work with large sets of numbers organized in structures called matrices and vectors. Linear algebra helps us manipulate these efficiently.
Calculus, particularly derivatives, helps our ML models learn and improve over time. It's like giving our model a sense of direction on how to adjust itself to make better predictions.
Let's look at a simple example using PyTorch, a popular ML library:
import torch
# Creating tensors
x = torch.tensor([1, 2, 3, 4, 5])
y = torch.tensor([2, 4, 6, 8, 10])
# Element-wise operations
z = x + y
print("x + y =", z)
# Matrix multiplication
A = torch.tensor([[1, 2], [3, 4]])
B = torch.tensor([[5, 6], [7, 8]])
C = torch.matmul(A, B)
print("A * B =", C)
# Gradients
x = torch.tensor([2.0], requires_grad=True)
y = x ** 2
y.backward()
print("Gradient of y with respect to x:", x.grad)
This code shows how we can create and manipulate tensors (multi-dimensional arrays) and compute gradients, which are crucial for training ML models.
Interactive Visualization: Linear Function
Let's visualize a simple linear function: y = 2x + 1. This is a fundamental concept in ML, often used in linear regression.
When x is 5, y is 11
Your First ML Project: Predicting House Prices
Now, let's put what we've learned into practice with a simple ML project. We'll create a model to predict house prices based on their size.
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Sample data: house sizes (in sq ft) and prices
sizes = np.array([1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700]).reshape(-1, 1)
prices = np.array([245000, 312000, 279000, 308000, 199000, 219000, 405000, 324000, 319000, 255000])
# Create and train the model
model = LinearRegression()
model.fit(sizes, prices)
# Make a prediction
new_size = np.array([2000]).reshape(-1, 1)
predicted_price = model.predict(new_size)
print(f"Predicted price for a 2000 sq ft house: ${predicted_price[0]:.2f}")
# Visualize the results
plt.scatter(sizes, prices, color='blue', label='Actual Prices')
plt.plot(sizes, model.predict(sizes), color='red', label='Predicted Prices')
plt.scatter(new_size, predicted_price, color='green', marker='x', s=100, label='New Prediction')
plt.xlabel('House Size (sq ft)')
plt.ylabel('Price ($)')
plt.title('House Price Prediction')
plt.legend()
plt.show()
This code creates a simple linear regression model to predict house prices based on their size. It uses real-world concepts like data preparation, model training, making predictions, and visualizing results.
Challenge: Extend the House Price Predictor
Now it's your turn! Try to extend our house price predictor by adding another feature, such as the number of bedrooms. You'll need to:
- Modify the input data to include the number of bedrooms
- Update the model to handle multiple features
- Make predictions using both house size and number of bedrooms
- Bonus: Try to visualize the results in 3D!
This challenge will help you practice working with multi-feature datasets and more complex models.