Table of Content
What You’ll Learn
In this tutorial, we’ll walk through how to build a basic machine learning model — your first step into the world of Artificial Intelligence. You’ll learn:
- How machine learning works at a beginner level
- How to use Python and Scikit-learn
- How to train, test, and evaluate an ML model using real data
Prerequisites
- Basic Python knowledge
- Python 3.8+ installed
- Familiarity with Jupyter Notebook (optional)
- Libraries:
pandas
,scikit-learn
,matplotlib
Step 1: Install Required Libraries
Run this in your terminal or notebook:
pip install pandas scikit-learn matplotlib
Step 2: Load and Understand the Dataset
We’ll use the classic Iris dataset — perfect for first-timers.
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['target'] = iris.target
df.head()
This dataset contains measurements of iris flowers from three different species.
Step 3: Build a Simple ML Model
We’ll use a Decision Tree Classifier to predict flower species.
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X = df[iris.feature_names] # features
y = df['target'] # labels
# Split into training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create model
model = DecisionTreeClassifier()
Step 4: Train & Test the Model
# Train
model.fit(X_train, y_train)
# Predict
y_pred = model.predict(X_test)
Step 5: Evaluate Model Performance
from sklearn.metrics import accuracy_score, classification_report
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
Bonus: Visualize the Model (Optional)
from sklearn import tree
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
tree.plot_tree(model, feature_names=iris.feature_names, class_names=iris.target_names, filled=True)
plt.show()
This gives a full visualization of your Decision Tree model — great for understanding how AI makes decisions.
Final Thoughts
Congratulations! You’ve just built your first AI model from scratch using open-source tools.
You:
- Loaded a dataset
- Built and trained a model
- Evaluated its performance
- Visualized the results