Loading
Loading
Machine Learning (ML) is the branch of AI that enables systems to learn from data and improve over time โ without explicitly programming every rule.
In traditional programming:
Data + Rules โ Results
In machine learning:
Data + Results โ Rules (the model)
You feed the model examples, and it derives the rules automatically.
Traditional approach: Write explicit rules (contains "free" + "click here" โ spam). Problem: doesn't cover all cases, needs constant manual updates.
ML approach: Give the model 10,000 emails (5,000 spam + 5,000 real). It learns patterns on its own and discovers spam signals you'd never think of.
Three factors aligned: massive data availability, GPU compute power (100x faster than CPU), and breakthrough algorithms (Gradient Descent, Backpropagation, Transformers).
Data โ Preprocessing โ Model โ Training โ Evaluation โ Deployment
Features: Input characteristics fed to the model (house area, rooms, location).
Labels: The correct answer to learn (house price).
Training set: ~80% of data used for learning.
Test set: ~20% of data used to evaluate generalization.
| Term | Definition | |------|----------| | Overfitting | Model memorizes training data but fails on new data | | Underfitting | Model too simple, misses patterns | | Hyperparameter | Settings you tune (learning rate, depth) |
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris, load_boston
import matplotlib
matplotlib.use("Agg") # ููุชุดุบูู ุจุฏูู ุดุงุดุฉ
import matplotlib.pyplot as plt
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ู
ุซุงู 1: ุงุณุชูุดุงู ุจูุงูุงุช Iris
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("=" * 50)
print("ู
ุซุงู 1: ุจูุงูุงุช Iris ุงูููุงุณูููุฉ")
print("=" * 50)
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = [iris.target_names[t] for t in iris.target]
print(f"\nุดูู ุงูุจูุงูุงุช: {df.shape}")
print(f"ุงูุฃููุงุน: {df['species'].unique()}")
print(f"\nุฃูู 5 ุตููู:")
print(df.head())
print(f"\nุฅุญุตุงุฆูุงุช:")
print(df.describe().round(2))
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ู
ุซุงู 2: ุชู
ููุฒ Features ูLabels
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("\n" + "=" * 50)
print("ู
ุซุงู 2: ูุตู Features ุนู Labels")
print("=" * 50)
X = df[iris.feature_names] # Features (ุงูู
ุฏุฎูุงุช)
y = df["species"] # Labels (ุงูู
ุฎุฑุฌุงุช)
print(f"Features shape: {X.shape} โ (ุนุฏุฏ ุงูุฃู
ุซูุฉ, ุนุฏุฏ ุงูู
ูุฒุงุช)")
print(f"Labels shape: {y.shape} โ (ุนุฏุฏ ุงูุฃู
ุซูุฉ,)")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ู
ุซุงู 3: Train/Test Split
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
print(f"\nุญุฌู
ุจูุงูุงุช ุงูุชุฏุฑูุจ : {X_train.shape[0]} ู
ุซุงู ({X_train.shape[0]/len(X)*100:.0f}%)")
print(f"ุญุฌู
ุจูุงูุงุช ุงูุงุฎุชุจุงุฑ: {X_test.shape[0]} ู
ุซุงู ({X_test.shape[0]/len(X)*100:.0f}%)")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ู
ุซุงู 4: ุฃูู ูู
ูุฐุฌ ุจุณูุท
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
print("\n" + "=" * 50)
print("ู
ุซุงู 4: ุฃุจุณุท ูู
ูุฐุฌ โ KNeighbors")
print("=" * 50)
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"\nุฏูุฉ ุงููู
ูุฐุฌ ุนูู ุจูุงูุงุช ุงูุงุฎุชุจุงุฑ: {accuracy:.1%}")
# ุชูุจุค ุจู
ุซุงู ุฌุฏูุฏ
new_flower = [[5.1, 3.5, 1.4, 0.2]] # ููุงุณุงุช ุฒูุฑุฉ ุฌุฏูุฏุฉ
prediction = model.predict(new_flower)
print(f"\nุชูุจุค ูุฒูุฑุฉ ุฌุฏูุฏุฉ {new_flower[0]}: {prediction[0]}")