🎯 Project: Your First ML Model
Congratulations! You're ready to build your first machine learning model. This project will walk you through the complete ML workflow using a simple, understandable example.
We'll predict a simple linear relationship: y = 2x + 1. While this seems trivial, it demonstrates all the key concepts you'll use in more complex projects.
Project Goal
Create a model that learns the relationship between input (x) and output (y) values. Given training examples, the model should learn to predict y for any new x value.
training_data = [
(1, 3),
(2, 5),
(3, 7),
(4, 9),
(5, 11)
]
print("Training Examples:")
for x, y in training_data:
print(f"x={x}, y={y}")
Step 1: Prepare the Data
First, we need to format our data for the machine learning algorithm. We'll separate inputs (X) and outputs (y):
import numpy as np
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([3, 5, 7, 9, 11])
print("Input features (X):")
print(X)
print("\nTarget values (y):")
print(y)
print("\nShape of X:", X.shape)
print("Shape of y:", y.shape)
Step 2: Train the Model
Now we'll use scikit-learn's LinearRegression to learn the pattern from our data:
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([3, 5, 7, 9, 11])
model = LinearRegression()
model.fit(X, y)
print("Model trained successfully!")
print("Learned coefficient (slope):", model.coef_[0])
print("Learned intercept:", model.intercept_)
print("\nThe model learned: y = {:.2f}x + {:.2f}".format(model.coef_[0], model.intercept_))
Step 3: Make Predictions
Once trained, use the model to predict y for new x values it hasn't seen:
from sklearn.linear_model import LinearRegression
import numpy as np
X_train = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y_train = np.array([3, 5, 7, 9, 11])
model = LinearRegression()
model.fit(X_train, y_train)
X_new = np.array([[6], [7], [8]])
predictions = model.predict(X_new)
print("Predictions on new data:")
for x, pred in zip(X_new, predictions):
expected = 2 * x[0] + 1
print(f"x={x[0]}, predicted y={pred:.2f}, expected y={expected}")
Step 4: Evaluate the Model
Check how well the model learned by comparing predictions with expected values:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import numpy as np
X_train = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y_train = np.array([3, 5, 7, 9, 11])
model = LinearRegression()
model.fit(X_train, y_train)
X_test = np.array([[6], [7], [8]])
y_test = np.array([13, 15, 17])
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)
print("\nModel Performance:")
print("The lower the error, the better the model learned!")
💡 Key Insight
This simple example demonstrates the complete ML cycle: data → training → prediction → evaluation. Every complex ML project follows this same pattern!
🎉
Lesson Complete!
Great work! Continue to the next lesson.