← Back to Home
๐ค Part 4: Introduction to Machine Learning with scikit-learn
What Is Machine Learning?
Machine Learning (ML) is a subset of AI where systems learn from data rather than being explicitly programmed. The goal is to make predictions or decisions without human intervention.
๐ Types of Machine Learning
Type | Description | Example |
---|---|---|
Supervised Learning | Learn from labeled data | Spam detection, housing price prediction |
Unsupervised Learning | Discover patterns in unlabeled data | Customer segmentation |
Reinforcement Learning | Learn from actions and rewards | Game playing, robotics |
๐งฐ Why Use scikit-learn?
scikit-learn is a powerful and beginner-friendly library for:
-
Classification
-
Regression
-
Clustering
-
Preprocessing
-
Model Evaluation
๐ ️ Installing scikit-learn
Install with pip:
pip install scikit-learn
๐ First Machine Learning Project: Iris Classification
๐ Step 1: Load Dataset
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target
print(df.head())
๐งช Step 2: Train/Test Split
from sklearn.model_selection import train_test_split
X = df.drop('target', axis=1)
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
๐ง Step 3: Train a Classifier
Let’s use a Decision Tree:
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
๐ Step 4: Evaluate the Model
from sklearn.metrics import accuracy_score
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy * 100:.2f}%")
๐ Bonus: Make a Prediction
sample = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(sample)
print("Predicted class:", iris.target_names[prediction[0]])
๐งญ Practice Challenge
Try using a Logistic Regression model instead of a Decision Tree:
from sklearn.linear_model import LogisticRegression
log_model = LogisticRegression(max_iter=200)
log_model.fit(X_train, y_train)
print("Accuracy:", log_model.score(X_test, y_test))
๐ What You’ve Learned:
-
What machine learning is and its main types
-
How to load and prepare data
-
Training and evaluating a simple ML model using scikit-learn
๐งญ What’s Next?
In Part 5, we’ll move into Deep Learning and explore how to build Neural Networks using TensorFlow and Keras.