ch14s1_BasicsOfMachineLearning

**Machine Learning (ML)** is a branch of Artificial Intelligence (AI) that enables computers to **learn patterns from data** and make predictions or decisions **without being explicitly programmed**.

Chapter 14: Introduction to Machine Learning — Basics of Machine Learning

🤖 What Is Machine Learning?

Machine Learning (ML) is a branch of Artificial Intelligence (AI) that enables computers to learn patterns from data and make predictions or decisions without being explicitly programmed.
Instead of following fixed rules, ML systems improve automatically through experience.


🧠 1. The AI Hierarchy

ConceptDescriptionExample
Artificial Intelligence (AI)The broad field of creating machines that can perform human-like reasoning or perception.Chatbots, recommendation engines
Machine Learning (ML)Subset of AI focused on algorithms that learn from data.Predicting housing prices
Deep Learning (DL)Subset of ML using neural networks to learn complex representations.Image recognition, voice assistants

⚙️ 2. The Machine Learning Workflow

  1. Data Collection → Gather raw, relevant data.
  2. Data Preprocessing → Clean, normalize, and transform data into usable form.
  3. Model Training → Train an algorithm to learn patterns from data.
  4. Model Evaluation → Test the model on unseen data and measure accuracy.
  5. Deployment & Monitoring → Use the model in real-world applications and track performance.

“Garbage in, garbage out” — model quality depends heavily on data quality.


🧩 3. Key Concepts in Machine Learning

Data

The foundation of ML. It may be:

Features

Quantifiable characteristics of the data used as input for the model.
Example: in predicting house prices, square footage, location, and bedrooms are features.

Labels / Targets

The correct outputs used for training in supervised learning.
Example: house price is the label corresponding to each house.

Model

A mathematical function that maps input features to predicted outputs.
It’s the “learned brain” of the ML system.

Algorithm

The procedure or set of rules used to train a model.
Examples: Linear Regression, Decision Trees, K‑Means Clustering, Random Forest, Neural Networks.

Training and Testing


🎓 4. Types of Machine Learning

TypeDescriptionExamplesCommon Algorithms
Supervised LearningLearns from labeled data (input + correct output).Predicting sales, spam detectionLinear Regression, SVM, Random Forest
Unsupervised LearningFinds patterns in unlabeled data.Customer segmentation, anomaly detectionK‑Means, PCA, DBSCAN
Reinforcement LearningLearns through feedback and rewards.Robotics, game AIQ‑Learning, Policy Gradient

📈 5. Example — Linear Regression with Scikit‑Learn

Linear Regression predicts a continuous value — such as exam scores based on study hours.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Data: hours studied vs. exam scores
hours = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).reshape(-1, 1)
scores = np.array([65, 70, 75, 80, 85, 88, 92, 95, 98, 100])

# Split into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(hours, scores, test_size=0.2, random_state=42)

# Train a Linear Regression model
model = LinearRegression()
model.fit(X_train, y_train)

# Predict on test data
y_pred = model.predict(X_test)

# Evaluate
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")
print(f"R² Score: {r2:.2f}")

# Plot results
plt.scatter(hours, scores, color='steelblue', label='Actual')
plt.plot(hours, model.predict(hours), color='red', label='Predicted Line')
plt.xlabel('Hours Studied')
plt.ylabel('Exam Score')
plt.title('Linear Regression: Study Hours vs Exam Score')
plt.legend()
plt.grid(True, linestyle='--', alpha=0.5)
plt.show()

📊 Interpreting Results


🧮 6. Common Machine Learning Algorithms

CategoryExample AlgorithmsTypical Use Cases
RegressionLinear Regression, Lasso, RidgeForecasting, price prediction
ClassificationLogistic Regression, Decision Trees, SVMSpam detection, medical diagnosis
ClusteringK‑Means, DBSCAN, Gaussian Mixture ModelsCustomer segmentation, anomaly detection
Dimensionality ReductionPCA, t‑SNEVisualization, noise reduction
Ensemble MethodsRandom Forest, XGBoost, Gradient BoostingImproving accuracy via multiple models

🔍 7. Key Takeaways


🧭 Conclusion

Machine Learning empowers computers to uncover insights, make predictions, and automate decisions across industries.
By mastering its fundamentals — from data preparation to model evaluation — you’ll build a solid foundation for more advanced topics like Deep Learning, Natural Language Processing, and AI-driven analytics.

“The best way to learn Machine Learning is not by reading — but by experimenting.”
Andrew Ng