Module 14 of 15 · Python for AI — Complete Beginner Course · Beginner

Your First Machine Learning Model

Duration: 5 min

Now you're ready to build your first machine learning model! You'll use scikit-learn, the most popular ML library in Python.

Understanding the ML Workflow

Every ML project follows this workflow:

  1. Load data - Get your dataset
  2. Explore data - Understand what you're working with
  3. Prepare data - Clean and format it
  4. Split data - Training (80%) and testing (20%)
  5. Train model - Teach the algorithm
  6. Evaluate - Check how well it works
  7. Predict - Use it on new data

Building Your First Model

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report

# Step 1: Load data
iris = load_iris()
X = iris.data  # Features (measurements)
y = iris.target  # Labels (flower types)

print(f"Dataset shape: {X.shape}")  # (150, 4)
print(f"Classes: {iris.target_names}")  # ['setosa' 'versicolor' 'virginica']

# Step 2: Split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

print(f"Training samples: {len(X_train)}")
print(f"Testing samples: {len(X_test)}")

# Step 3: Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Step 4: Make predictions
y_pred = model.predict(X_test)

# Step 5: Evaluate
accuracy = accuracy_score(y_test, y_pred)
print(f'\nAccuracy: {accuracy:.2%}')
print(f'\nClassification Report:\n{classification_report(y_test, y_pred, target_names=iris.target_names)}')

# Step 6: Predict on new data
new_flower = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(new_flower)
flower_name = iris.target_names[prediction[0]]
print(f'\nThis flower is: {flower_name}')

Try it in Google Colab: Open in Colab

Dataset shape: (150, 4)
Classes: ['setosa' 'versicolor' 'virginica']
Training samples: 120
Testing samples: 30

Accuracy: 100.00%

Classification Report:
              precision    recall  f1-score   support

      setosa       1.00      1.00      1.00        10
  versicolor       1.00      1.00      1.00        10
   virginica       1.00      1.00      1.00        10

    accuracy                           1.00        30
   macro avg       1.00      1.00      1.00        30
weighted avg       1.00      1.00      1.00        30

This flower is: setosa

Next Steps

Congratulations! You've built your first ML model. Now explore:

  1. AI Fundamentals - Learn ML concepts deeply
  2. Deep Learning - Build neural networks
  3. RAG Systems - Build AI applications

Practice: Build models on different datasets. Kaggle.com has thousands of free datasets!

💡 Tip: The best way to learn is by doing. Start with small projects and gradually increase complexity.

❓ What is the purpose of splitting data into training and testing sets?

# Advanced example for Your First Machine Learning Model
# Production-ready pattern
print('Advanced implementation')
Advanced implementation

❓ What is a best practice when working with Your First Machine Learning Model?

💡 Tip: Pro Tip: Master Your First Machine Learning Model thoroughly before moving to advanced topics. This foundation is crucial for writing professional Python code.

← Previous Continue interactively → Next →

Related Courses