In this chapter, you will learn the basic concepts of machine learning, the three learning types, major algorithms, and a practical workflow. You will understand the technical terminology and prepare for implementation.
Learning Objectives
After completing this chapter, you will be able to:
- Explain the definition of machine learning and its three learning types (supervised, unsupervised, and reinforcement learning)
- Use 20 essential ML terms appropriately
- Understand the characteristics of four major frameworks (scikit-learn, PyTorch, TensorFlow, XGBoost) and when to use each
- Describe all seven steps of the machine learning workflow in detail
- Understand the types of features and transformation methods, and implement them
2.1 Definition and Classification of Machine Learning
2.1.1 What is Machine Learning?
Machine Learning (ML) is a general term for computer programs that automatically learn patterns from data to make predictions and decisions.
Classic definition (Arthur Samuel, 1959):
"The field of study that gives computers the ability to learn without being explicitly programmed."
Formal definition (Tom Mitchell, 1997):
"A program that learns from experience E with respect to some task T and performance measure P, if its performance at task T, as measured by P, improves with experience E."
Understanding through a concrete example:
- Task T: Detecting spam email
- Experience E: Past emails (labeled as spam/not spam)
- Performance P: Detection accuracy (correct classification rate)
If the system learns from past email data and improves its accuracy in judging whether new emails are spam, then we can say it is "doing machine learning."
The Relationship Between AI, ML, and DL
Artificial Intelligence] --> B[Machine Learning ML
Machine Learning] B --> C[Deep Learning DL
Deep Learning] A1[Systems that exhibit
intelligent behavior] -.-> A B1[Learning patterns
from data] -.-> B C1[Learning using
neural networks] -.-> C style A fill:#e3f2fd style B fill:#fff3e0 style C fill:#f3e5f5
Containment relationship:
- AI (Artificial Intelligence): The broadest concept. All systems that exhibit intelligent behavior, such as chess programs, chatbots, and self-driving cars
- ML (Machine Learning): One approach within AI. Technology that automatically learns from data
- DL (Deep Learning): A type of ML. A learning method that uses multi-layer neural networks
2.1.2 The Three Learning Types of Machine Learning
Machine learning is broadly divided into three categories based on the type of data and the learning method.
1. Supervised Learning
Definition: A method that learns from labeled data (pairs of inputs and correct answers).
How it works:
Input data (features) + correct labels → model training → prediction
Concrete examples:
- Spam detection: Email body (input) → spam/not spam (answer)
- Price prediction: House features (size, age, location) → price
- Image classification: Image of a cat → the label "cat"
- Speech recognition: Audio data → text
Major algorithms:
- Linear Regression: Prediction of continuous values
- Logistic Regression: Binary classification
- Decision Tree: Classification through conditional branching
- Support Vector Machine (SVM): Learns the optimal boundary
- Neural Network: Learning of complex patterns
2. Unsupervised Learning
Definition: A method that discovers hidden patterns or structure from unlabeled data.
How it works:
Input data only (no answers) → model → pattern discovery
Concrete examples:
- Customer segmentation: Grouping customers based on purchase history
- Anomaly detection: Detecting patterns that differ from the norm (fraudulent transactions, equipment failure)
- Dimensionality reduction: Compressing high-dimensional data to 2-3 dimensions for visualization
- Recommendation systems: Discovering similar products
Major algorithms:
- K-means: Divides data into K clusters
- Hierarchical clustering: Represents group structure with a dendrogram
- Principal Component Analysis (PCA): Extracts the main directions of the data
- Self-Organizing Map (SOM): Visualizes the distribution of the data
3. Reinforcement Learning
Definition: A method that learns behavior to maximize rewards through trial and error.
How it works:
Agent → action → environment → reward/state feedback → learning
Key concepts:
- Agent: The entity that learns (e.g., game AI, robot)
- Environment: The space in which the agent acts (e.g., game board, real world)
- Action: The operation the agent chooses
- Reward: A numeric value indicating how good or bad an action is (+ is good, - is bad)
- Policy: A mapping from states to actions ("in this situation, act this way")
Concrete examples:
- Game AI: AlphaGo (Go), Dota 2, Atari games
- Robot control: Bipedal walking, object grasping, drone flight
- Autonomous driving: Lane keeping, obstacle avoidance
- Recommendation systems: Learning optimal recommendations from user responses
Comparison of the Three Learning Types
| Learning Type | Data | Objective | Applications | Difficulty |
|---|---|---|---|---|
| Supervised Learning | Labeled | Prediction / classification | Spam detection, price prediction, image recognition | Medium |
| Unsupervised Learning | Unlabeled | Pattern discovery | Customer analysis, anomaly detection, data compression | High |
| Reinforcement Learning | Reward signal | Action optimization | Game AI, robotics, autonomous driving | Highest |
Flowchart for Choosing a Learning Type
ground-truth data?} B -->|Yes| C{What is the output?} B -->|No| D{What is the goal?} C -->|Continuous value
e.g. price| E[Regression
Linear Regression] C -->|Category
e.g. spam/not spam| F[Classification
Logistic Regression
Decision Tree] D -->|Grouping| G[Clustering
K-means] D -->|Dimensionality reduction| H[Dimensionality Reduction
PCA] D -->|Anomaly detection| I[Anomaly Detection
Isolation Forest] J{Optimize through
trial and error?} -->|Yes| K[Reinforcement Learning
Q-learning
DQN] style E fill:#e8f5e9 style F fill:#e8f5e9 style G fill:#fff3e0 style H fill:#fff3e0 style I fill:#fff3e0 style K fill:#f3e5f5
2.2 Glossary of Key Machine Learning Terms (20 Terms)
Here are the essential technical terms for learning machine learning, organized by category. Understand each term as a four-part set: Japanese, English, definition, and a concrete example.
Basic Terms (8 Terms)
| Term (Japanese) | Term (English) | Definition and Example |
|---|---|---|
| 1. 特徴量 | Feature | An attribute or property of the data that serves as input to the model. For house price prediction, this includes "size, age, distance to station," etc. The quality of features greatly influences model performance. Designing appropriate features is called "feature engineering." |
| 2. ラベル | Label | The ground-truth data in supervised learning. For spam detection, the labels "spam"/"not spam"; for house price prediction, the numeric "price." Because collecting labeled data is costly, the amount of data is often limited. |
| 3. 訓練データ | Training Data | The dataset used to train the model. It is common to use 70-80% of all data for training. The more training data there is, the more complex patterns the model can learn, but the risk of overfitting also increases. |
| 4. テストデータ | Test Data | Unseen data used to evaluate the performance of a trained model. It uses 20-30% of all data. Important: test data is never used for training and is used only for final evaluation. If performance is high on the test data, we can judge that generalization performance is high. |
| 5. モデル | Model | A formula or program that represents a mapping from inputs to outputs. For linear regression, the formula $y = wx + b$; for a neural network, a multi-layer computation graph. Through training, the model's parameters (weights w, bias b) are optimized. |
| 6. 予測 | Prediction | The value that a trained model outputs for new input data. For regression, a continuous value (e.g., a price of 35 million yen); for classification, a class (e.g., spam). Improving prediction accuracy is the main goal of machine learning. |
| 7. 損失関数 | Loss Function | A function that quantifies the difference (error) between the predicted value and the correct answer. Mean squared error (MSE) is commonly used for regression, and cross-entropy for classification. The goal of learning is to minimize the loss function. A smaller loss means higher prediction accuracy. |
| 8. 過学習 | Overfitting | A phenomenon in which the model fits the training data too closely and its predictive performance on unseen data declines. It occurs when training accuracy is high but test accuracy is low. Countermeasures: increase the amount of data, simplify the model, regularization, cross-validation, etc. It is one of the most important problems to watch for in machine learning. |
Method Terms (7 Terms)
| Term (Japanese) | Term (English) | Definition and Example |
|---|---|---|
| 9. 回帰 | Regression | A task of predicting continuous values. Numeric predictions such as house prices, stock prices, and temperatures. Representative methods: linear regression, ridge regression, random forest. The output is a concrete numeric value such as "35 million yen" or "25.3°C." |
| 10. 分類 | Classification | A task of predicting a category (class). Spam detection, disease diagnosis, image recognition, etc. There are binary classification (Yes/No) and multi-class classification (cat/dog/bird). The output is a label such as "spam," "benign," or "cat." |
| 11. クラスタリング | Clustering | Unsupervised learning that groups data without ground-truth labels. Used for customer segmentation, document classification, image compression, etc. In K-means, you specify the number of groups K in advance, and the data is automatically split into K groups. |
| 12. 交差検証 | Cross-Validation | A method that splits the data into K parts and repeats K times the operation of using one part for testing and the rest for training. K-fold cross-validation (K=5 or 10 is common) allows accurate evaluation of the model's generalization performance. It is especially effective when data is scarce. |
| 13. ハイパーパラメータ | Hyperparameter | Adjustable settings that a human sets before training. Learning rate, tree depth, number of neurons, etc. Unlike model parameters (weights w, bias b), they are not updated during training. The optimal values are searched for with Grid Search or Random Search. |
| 14. 正則化 | Regularization | A method that imposes a penalty on model complexity to prevent overfitting. L1 regularization (Lasso) pushes parameters toward zero, and L2 regularization (Ridge) suppresses them to small values. Regularization improves generalization performance. |
| 15. アンサンブル | Ensemble | A method that combines the predictions of multiple models to improve accuracy. Bagging (Random Forest), boosting (XGBoost), stacking, etc. Following the principle "two heads are better than one," it often achieves higher accuracy than a single model. |
Evaluation Terms (5 Terms)
| Term (Japanese) | Term (English) | Definition and Example |
|---|---|---|
| 16. 精度 | Accuracy | The proportion of all predictions that are correct. Formula: $\text{Accuracy} = \frac{\text{number correct}}{\text{total number of data}}$. If 85 out of 100 are correct, the accuracy is 85%. Simple, but it can be misleading when the data is imbalanced (spam 1% vs. not spam 99%). |
| 17. 再現率 | Recall | The proportion of actual positive cases that were correctly detected. Formula: $\text{Recall} = \frac{\text{true positives}}{\text{true positives + false negatives}}$. Emphasized when "not missing sick people" is important in disease diagnosis. The higher the recall, the fewer the misses. |
| 18. 適合率 | Precision | Of the cases the model predicted as positive, the proportion that were actually positive. Formula: $\text{Precision} = \frac{\text{true positives}}{\text{true positives + false positives}}$. Emphasized when "not misclassifying non-spam as spam" is important in spam detection. The higher the precision, the fewer the false detections. |
| 19. F1スコア | F1 Score | The harmonic mean of precision and recall. Formula: $\text{F1} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}$. A metric that balances both; a good score is not obtained if only one of them is high. It is more useful than accuracy when the data is imbalanced. |
| 20. AUC-ROC | AUC-ROC | The area under the ROC curve (horizontal axis: false positive rate, vertical axis: true positive rate). 0.5 means completely random, and 1.0 means perfect classification. It is a threshold-independent evaluation metric that indicates the overall performance of a classifier. An AUC of 0.9 or higher is evaluated as an excellent model. |
2.3 Comparison of Major Frameworks
For implementing machine learning, it is important to choose a framework according to your purpose. Here we compare four major frameworks in detail.
2.3.1 scikit-learn
Characteristics: The most widely used general-purpose machine learning library in Python.
Strengths:
- Beginner-friendly: Easy to use with a unified API (fit, predict, and score are the three basic methods)
- Rich set of classical ML methods: Regression, classification, clustering, dimensionality reduction, etc.
- Extensive documentation: The official documentation is detailed with plenty of sample code
- Preprocessing features: Scaling, encoding, feature selection, etc.
Weaknesses:
- Does not support deep learning (only shallow neural networks)
- Slow with large-scale data (hundreds of GB or more)
- Cannot use GPUs
Where to use it:
- Analysis of tabular data (CSV, Excel, etc.)
- Small to medium-scale data (tens of thousands to millions of samples)
- Prototyping and building baselines
- Educational / learning purposes
2.3.2 PyTorch
Characteristics: A deep learning framework developed by Meta (formerly Facebook).
Strengths:
- Research-oriented: Highly flexible and easy to implement new architectures
- Pythonic: You can write it in a Python-like style, and debugging is easy
- Dynamic computation graph: Intuitive because the graph is built at runtime
- Research community: Abundant implementations of the latest papers (GitHub)
Weaknesses:
- Deployment to production is somewhat complex
- Fewer options for model optimization and compression than TensorFlow
Where to use it:
- Research and development in deep learning
- Implementing custom models (new architectures)
- Image recognition, natural language processing, speech recognition
- Reproducing implementations from academic papers
2.3.3 TensorFlow / Keras
Characteristics: A production-oriented deep learning framework developed by Google (Keras is the high-level API).
Strengths:
- Production-oriented: Easy deployment with TensorFlow Serving
- Scalability: Strong support for distributed training and multi-GPU
- Keras integration: Build models concisely with the high-level API
- Ecosystem: TensorFlow Lite (mobile), TensorFlow.js (web), etc.
Weaknesses:
- Somewhat steep learning curve (some parts are difficult for beginners)
- Debugging is harder than PyTorch (static computation graph)
Where to use it:
- Projects that assume deployment to production
- Large-scale systems (cloud, distributed training)
- Embedding ML into mobile apps
- Practical use in enterprises
2.3.4 XGBoost / LightGBM
Characteristics: Fast libraries specialized in gradient boosting.
Strengths:
- Best performance on tabular data: Overwhelming share in Kaggle competitions
- Fast: Trains in a short time even with large-scale data
- Feature importance: Automatically evaluates which features are important
- Missing-value handling: Can handle missing values automatically
Weaknesses:
- Not suited to image or text data (CNNs or RNNs are needed)
- Hyperparameter tuning is complex
Where to use it:
- Classification and regression on tabular data (CSV, databases)
- Data analysis competitions such as Kaggle
- Business data analysis (sales forecasting, customer churn prediction)
- Situations requiring high accuracy on structured data
Framework Comparison Table
| Framework | Strong Area | Learning Difficulty | Speed | Production | GPU Support |
|---|---|---|---|---|---|
| scikit-learn | Classical ML, tabular data | Easy | Medium | △ | × |
| PyTorch | Deep learning, R&D | Medium | High (GPU) | ○ | ◎ |
| TensorFlow/Keras | Production, large-scale systems | Medium–High | High (GPU) | ◎ | ◎ |
| XGBoost/LightGBM | Tabular data, competitions | Medium | High (CPU) | ○ | △ |
Framework Selection Flowchart
are you handling?] --> B{Tabular data
CSV/Excel} A --> C{Image, text, or audio} B --> D{Data size} D -->|Small–Medium
up to 100k rows| E[scikit-learn
beginner-friendly] D -->|Large
over 100k rows| F[XGBoost/LightGBM
high accuracy, fast] C --> G{What is the goal?} G -->|Research / experiments| H[PyTorch
flexibility-focused] G -->|Production deployment| I[TensorFlow/Keras
scalability-focused] style E fill:#e8f5e9 style F fill:#e8f5e9 style H fill:#f3e5f5 style I fill:#fff3e0
2.4 The Machine Learning Ecosystem
Machine learning projects consist of multiple steps, from data collection to production operation. It is important to understand the overall picture.
DB/API/files] --> B[Data collection
Scraping/ETL] B --> C[Data storage
Data Lake/DWH] C --> D[Preprocessing
Cleaning/Normalization] D --> E[Feature engineering
Transform/Select/Generate] E --> F[Model training
Learning/Tuning] F --> G[Evaluation
Accuracy validation] G --> H{Performance OK?} H -->|No| I[Hyperparameter tuning
Add features] I --> F H -->|Yes| J[Deploy
Production] J --> K[Monitoring
Performance monitoring] K --> L[Feedback
Add data] L --> B style A fill:#e3f2fd style F fill:#fff3e0 style J fill:#e8f5e9 style K fill:#ffebee
The role of each component:
- Data source: Databases, APIs, sensors, files, web scraping, etc.
- Data storage: Data Lake (storing raw data), Data Warehouse (cleaned data)
- Preprocessing: Handling missing values, removing outliers, data cleaning
- Feature engineering: Transformation into a form suitable for ML models
- Model training: Algorithm selection, learning, hyperparameter tuning
- Evaluation: Cross-validation, accuracy checks on test data
- Deployment: Placing the model in production (as an API, embedded)
- Monitoring: Monitoring prediction accuracy, detecting model degradation
- Feedback: Collecting new data, retraining the model
2.5 The Machine Learning Workflow in Detail (7 Steps)
An actual machine learning project proceeds through the following seven steps. Understand the details, time estimates, and points to note for each step.
Step 0: Problem Formulation (The most important, and often overlooked)
Objective: Define the problem to be solved in ML terms.
What to do:
- Identify the problem type: Regression? Classification? Clustering?
- Define success metrics (KPIs): Accuracy of 80% or higher, F1 score of 0.9, etc.
- Confirm data availability: Is the necessary data obtainable?
- Organize constraints: Budget, deadline, computational resources
Time estimate: 1-2 weeks (including discussions with stakeholders)
Common failures:
- Skipping problem formulation and jumping straight into coding
- Mismatch between business goals and ML goals
- Setting unachievable accuracy targets
Concrete example:
Business challenge: We want to reduce customer churn.
ML problem formulation:
- Task: Binary classification (churn / no churn)
- Input: Customer attributes (age, contract length, purchase history)
- Output: Churn probability
- Success metric: Recall of 80% or higher (do not miss churners)
- Data: 100,000 customer records over the past two years
Step 1: Data Collection
Objective: Gather the data needed for learning.
Data sources:
- Internal data: Databases, logs, CRM
- Public data: Kaggle, UCI ML Repository, government statistics
- APIs: Twitter API, Google Maps API
- Web scraping: BeautifulSoup, Scrapy
- Annotation: Manual labeling by humans
Guidelines for the amount of data needed:
- Minimum: 100 samples (prototype)
- Recommended: 1,000-10,000 samples (classical ML)
- Ideal: 10,000-1,000,000 samples (deep learning)
Time estimate: 1-4 weeks
Points to note:
- Check data quality (noise, missing values)
- Watch for class imbalance (spam 1% vs. not spam 99%, etc.)
- Check privacy and licensing
Step 2: Exploratory Data Analysis (EDA)
Objective: Understand the characteristics of the data and discover problems.
What to do:
- Check statistics: Mean, median, variance, maximum and minimum values
- Visualize distributions: Histograms, box plots
- Correlation analysis: Relationships between variables (correlation coefficients, scatter plots)
- Check for missing values: Which columns have how many missing values
- Detect outliers: Abnormally large/small values
Time estimate: A few days to 1 week
Tools: pandas, matplotlib, seaborn, pandas-profiling
Step 3: Preprocessing / Data Cleaning
Objective: Transform the data into a form that ML models can handle.
What to do:
1. Handling Missing Values
- Deletion: When missing values are few (less than 5%)
- Imputation: Fill with the mean, median, or mode
- Prediction: Predict missing values from other features
2. Handling Outliers
- Removal: Clearly erroneous data
- Capping: Set upper and lower limits
- Transformation: Reduce the impact with a log transform
3. Scaling (Normalization / Standardization)
- Standardization: Transform to mean 0, standard deviation 1
$z = \frac{x - \mu}{\sigma}$ - Normalization: Transform to the 0-1 range
$x' = \frac{x - x_{\min}}{x_{\max} - x_{\min}}$
4. Encoding Categorical Variables
- Label Encoding: Convert categories to numbers (red→0, blue→1, green→2)
- One-Hot Encoding: Convert categories to binary vectors
Time estimate: A few days to 1 week
Points to note:
- Apply the same preprocessing to the training data and the test data
- Compute scaling statistics (mean, standard deviation) from the training data
Step 4: Feature Engineering
Objective: Create features that improve the model's prediction accuracy.
Methods:
- Transforming existing features: Log transform, square root, polynomials
- Interaction terms: Products of features (size × distance to station)
- Aggregate statistics: Mean, sum, count per group
- Time-series features: Lag features, moving averages, seasonality
- Leveraging domain knowledge: Industry-specific indicators
Time estimate: A few days to 2 weeks
Concrete example:
In house price prediction, create new features from "size" and "building age":
area_per_building_age = area / (building_age + 1)near_station_flag = 1 if station_distance < 500m else 0
Step 5: Model Selection and Training
Objective: Choose an appropriate algorithm and train it.
Strategy:
- Baseline model: The simplest model (linear regression, logistic regression)
- Comparison of multiple models: Decision tree, random forest, XGBoost, SVM
- Selecting the best model: Evaluate performance with cross-validation
Time estimate: A few hours to a few days
Tools: scikit-learn, XGBoost, LightGBM
Step 6: Evaluation and Hyperparameter Tuning
Objective: Maximize the model's performance.
Evaluation methods:
- Hold-out method: Split into 80% training, 20% test
- K-fold cross-validation: Split the data into K parts and evaluate K times
Tuning methods:
- Grid Search: Try all combinations (time-consuming)
- Random Search: Sample randomly (efficient)
- Bayesian optimization: Search for optimal values efficiently
Time estimate: A few days to 1 week
Step 7: Model Interpretation and Validation
Objective: Confirm that the model is working correctly.
What to do:
- Feature importance: Which features are important
- Prediction error analysis: On which data does it make mistakes
- SHAP values: Explanation of individual predictions
- Business validation: Confirm it can actually be used
Time estimate: A few days to 1 week
Time Estimate for the Entire Workflow
Entire project: 2-3 months (small to medium scale), 6-12 months (large scale)
2.6 A Deep Dive into Features
A feature is an attribute of the data that serves as input to a machine learning model. By designing appropriate features, model accuracy can improve dramatically.
2.6.1 Types of Features
1. Numerical Features
Continuous values: Numbers that can be divided infinitely finely
- Age: 25.5 years, 30.2 years
- Price: 1,234,567 yen
- Temperature: 23.7°C
- Distance: 5.3 km
Discrete values: Integer values
- Number of purchases: 0, 1, 2, 3 times
- Number of rooms: 1LDK, 2LDK, 3LDK
- Stock quantity: 0, 5, 10 units
2. Categorical Features
Nominal: No ordering relationship
- Color: red, blue, green
- Gender: male, female
- Region: Tokyo, Osaka, Fukuoka
- Product category: appliances, clothing, food
Ordinal: Has an ordering relationship
- Education level: elementary < junior high < high school < university
- Rating: low < medium < high
- Size: S < M < L < XL
3. Text Features
Transformation methods:
- Bag of Words (BoW): Word occurrence counts
- TF-IDF: Quantifies word importance
- Word Embeddings: Vectorize words (Word2Vec, GloVe)
4. Time Series Features
Derived features:
- Lag features: Past values (1 day ago, 7 days ago)
- Moving average: Average over the past N days
- Seasonality: Month, day of week, holiday flag
- Trend: Increasing / decreasing tendency
5. Image Features
Transformation methods:
- Pixel values: Use RGB values directly
- Edge detection: Extract contours
- CNN features: Automatically extracted with deep learning
2.6.2 Numericalizing Categorical Variables (Code Example)
"""
Two methods for converting categorical variables to numbers
Purpose: Machine learning models cannot handle categories directly, so numericalization is required
Audience: Beginners
Runtime: about 3 seconds
"""
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# 1. Prepare sample data
df = pd.DataFrame({
'color': ['red', 'blue', 'green', 'red', 'blue'],
'size': ['S', 'M', 'L', 'M', 'S'],
'price': [100, 150, 200, 120, 90]
})
print("Original data:")
print(df)
print("\n" + "="*50 + "\n")
# 2. Label Encoding
# Convert each category to an integer (red->0, blue->1, green->2)
le = LabelEncoder()
df['color_label'] = le.fit_transform(df['color'])
print("After Label Encoding:")
print(df[['color', 'color_label']])
print("\nNote: an ordering relationship is introduced (blue=1 > red=0), but in reality there is no ordering")
print("\n" + "="*50 + "\n")
# 3. One-Hot Encoding
# Convert each category into an independent binary column
df_onehot = pd.get_dummies(df, columns=['size'], prefix='size')
print("After One-Hot Encoding:")
print(df_onehot)
print("\nThe three columns size_S, size_M, size_L are created; only the matching column is 1, the others are 0")
# Expected output:
# Original data:
# color size price
# 0 red S 100
# 1 blue M 150
# 2 green L 200
# 3 red M 120
# 4 blue S 90
#
# After Label Encoding:
# color color_label
# 0 red 2
# 1 blue 0
# 2 green 1
# 3 red 2
# 4 blue 0
#
# After One-Hot Encoding:
# color price color_label size_L size_M size_S
# 0 red 100 2 0 0 1
# 1 blue 150 0 0 1 0
# 2 green 200 1 1 0 0
# 3 red 120 2 0 1 0
# 4 blue 90 0 0 0 1
2.6.3 Scaling Numerical Features (Code Example)
"""
Scaling numerical features (standardization / normalization)
Purpose: Unify features of different scales to stabilize model training
Audience: Beginners
Runtime: about 3 seconds
"""
import numpy as np
from sklearn.preprocessing import StandardScaler, MinMaxScaler
# 1. Sample data (housing data)
data = np.array([
[50, 10, 500], # size(m^2), building age, distance to station(m)
[60, 15, 300],
[70, 5, 800],
[80, 20, 200],
[90, 8, 600]
])
print("Original data:")
print(" size(m^2) age distance(m)")
print(data)
print("\nNote: the scales are completely different (size:50-90, age:5-20, distance:200-800)")
print("\n" + "="*50 + "\n")
# 2. Standardization
# Transform to mean 0, standard deviation 1
scaler_std = StandardScaler()
data_std = scaler_std.fit_transform(data)
print("After standardization (mean 0, std 1):")
print(data_std)
print("\nMean of each column:", data_std.mean(axis=0))
print("Std of each column:", data_std.std(axis=0))
print("\n" + "="*50 + "\n")
# 3. Normalization
# Transform to the 0-1 range
scaler_norm = MinMaxScaler()
data_norm = scaler_norm.fit_transform(data)
print("After normalization (0-1 range):")
print(data_norm)
print("\nMin of each column:", data_norm.min(axis=0))
print("Max of each column:", data_norm.max(axis=0))
# Expected output:
# Original data:
# size(m^2) age distance(m)
# [[ 50 10 500]
# [ 60 15 300]
# [ 70 5 800]
# [ 80 20 200]
# [ 90 8 600]]
#
# After standardization (mean 0, std 1):
# [[-1.41 -0.39 0.00]
# [-0.71 0.78 -1.00]
# [ 0.00 -1.17 1.50]
# [ 0.71 1.56 -1.50]
# [ 1.41 -0.78 0.50]]
#
# After normalization (0-1 range):
# [[0.00 0.33 0.50]
# [0.25 0.67 0.17]
# [0.50 0.00 1.00]
# [0.75 1.00 0.00]
# [1.00 0.20 0.67]]
2.6.4 Feature Selection and Importance (Code Example)
"""
Feature selection and importance evaluation
Purpose: Determine which features are important for prediction and remove unnecessary features
Audience: Beginner to intermediate
Runtime: about 5 seconds
"""
import pandas as pd
from sklearn.datasets import load_diabetes
from sklearn.ensemble import RandomForestRegressor
import matplotlib.pyplot as plt
# 1. Load sample data (diabetes dataset)
diabetes = load_diabetes()
X = pd.DataFrame(diabetes.data, columns=diabetes.feature_names)
y = diabetes.target
print("Dataset info:")
print(f"Number of samples: {X.shape[0]}, number of features: {X.shape[1]}")
print(f"Features: {list(X.columns)}")
print("\n" + "="*50 + "\n")
# 2. Compute feature importance with a random forest
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X, y)
# 3. Retrieve feature importances
importances = pd.DataFrame({
'feature': X.columns,
'importance': model.feature_importances_
}).sort_values('importance', ascending=False)
print("Feature importance ranking:")
print(importances)
print("\n" + "="*50 + "\n")
# 4. Compute correlation coefficients
correlations = X.corrwith(pd.Series(y)).abs().sort_values(ascending=False)
print("Correlation coefficients with the target variable (absolute value):")
print(correlations)
# Expected output:
# Dataset info:
# Number of samples: 442, number of features: 10
# Features: ['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
#
# Feature importance ranking:
# feature importance
# 2 bmi 0.289345
# 8 s5 0.201928
# 4 s1 0.127654
# ...
#
# Correlation coefficients with the target variable (absolute value):
# bmi 0.586450
# s5 0.565883
# bp 0.441484
# ...
2.6.5 A Practical Example of Feature Engineering (Code Example)
"""
Feature engineering: creating new features
Purpose: Create more predictive derived features from existing features
Audience: Intermediate
Runtime: about 3 seconds
"""
import pandas as pd
import numpy as np
# 1. Sample data (house price prediction)
df = pd.DataFrame({
'area': [50, 60, 70, 80, 90],
'building_age': [10, 15, 5, 20, 8],
'station_distance': [500, 300, 800, 200, 600],
'rooms': [2, 3, 3, 4, 2],
'price': [3000, 3500, 4200, 4500, 3800] # in 10k yen
})
print("Original features:")
print(df)
print("\n" + "="*50 + "\n")
# 2. Create derived features
# (1) Interaction term: product of area and building age
df['area_x_building_age'] = df['area'] * df['building_age']
# (2) Ratio: price per unit area
df['price_per_area'] = df['price'] / df['area']
# (3) Polynomial: square of building age (aging deterioration is nonlinear)
df['building_age_sq'] = df['building_age'] ** 2
# (4) Categorization: convert station distance to "near/far"
df['near_station_flag'] = (df['station_distance'] < 400).astype(int)
# (5) Domain knowledge: area per room
df['area_per_room'] = df['area'] / df['rooms']
# (6) Log transform: log of station distance (reduce the impact of long distances)
df['log_station_distance'] = np.log1p(df['station_distance'])
print("After feature engineering:")
print(df)
print("\n" + "="*50 + "\n")
print("Statistics of the new features:")
print(df[['area_x_building_age', 'price_per_area', 'near_station_flag', 'area_per_room']].describe())
# Expected output:
# Original features:
# area building_age station_distance rooms price
# 0 50 10 500 2 3000
# 1 60 15 300 3 3500
# 2 70 5 800 3 4200
# 3 80 20 200 4 4500
# 4 90 8 600 2 3800
#
# After feature engineering:
# area building_age station_distance rooms price area_x_building_age price_per_area building_age_sq near_station_flag area_per_room log_station_distance
# 0 50 10 500 2 3000 500 60.0 100 0 25.0 6.215
# 1 60 15 300 3 3500 900 58.3 225 1 20.0 5.704
# 2 70 5 800 3 4200 350 60.0 25 0 23.3 6.685
# 3 80 20 200 4 4500 1600 56.2 400 1 20.0 5.298
# 4 90 8 600 2 3800 720 42.2 64 0 45.0 6.397
Chapter Summary
In this chapter, you systematically learned the fundamentals of machine learning. You acquired the following:
Knowledge and Skills Acquired
- Definition of machine learning: Technology that automatically learns patterns from data
- The three learning types: The differences and application scenarios of supervised, unsupervised, and reinforcement learning
- 20 key terms: Feature, label, model, loss function, overfitting, etc.
- Four major frameworks: When to use scikit-learn, PyTorch, TensorFlow, and XGBoost
- The machine learning ecosystem: The overall picture from data collection to production operation
- The 7-step workflow: Problem formulation, data collection, EDA, preprocessing, feature engineering, training, evaluation
- Types of features and their transformation: Numerical, categorical, text, time-series, and image features
Bridge to the Next Chapter
In Chapter 2, you learned the theory and concepts of machine learning. In the next Chapter 3, you will actually write Python code and implement six machine learning models. Starting from setting up the environment, you will experience data preprocessing, model training, and evaluation hands-on.
What you will learn in Chapter 3:
- Setting up a Python environment (Anaconda / venv / Google Colab)
- Full implementation of six models (linear regression, logistic regression, decision tree, random forest, SVM, KNN)
- Hyperparameter tuning (Grid Search, Random Search)
- A hands-on project with the Titanic dataset
Exercises
Exercise 1 (Difficulty: easy)
Explain the difference between supervised learning and unsupervised learning, with concrete examples.
Hint
Focus on the presence or absence of "correct labels." Supervised learning requires pairs of inputs and correct answers, whereas unsupervised learning discovers patterns without correct answers.
Sample Answer
Supervised Learning
Definition: A method that learns from labeled data (pairs of inputs and correct answers).
Concrete examples:
- Spam detection: Email body (input) → spam/not spam (correct label)
- House price prediction: Size, building age (input) → price (correct label)
- Image recognition: Image of a cat (input) → the label "cat" (correct answer)
Unsupervised Learning
Definition: A method that discovers hidden patterns or structure from unlabeled data.
Concrete examples:
- Customer segmentation: Automatically grouping customers from purchase history (no correct answers)
- Anomaly detection: Detecting patterns that differ from the norm (no normal/anomaly labels)
- Recommendation systems: Discovering similar products (no correct answers, only similarity)
Main Differences
| Item | Supervised Learning | Unsupervised Learning |
|---|---|---|
| Data | Labeled | Unlabeled |
| Objective | Prediction / classification | Pattern discovery |
| Evaluation | Accuracy, F1 score, etc. | Silhouette coefficient, etc. |
Exercise 2 (Difficulty: easy)
Explain what overfitting is and why it is a problem. Also, list three methods to prevent overfitting.
Hint
Focus on the performance gap between the training data and the test data. If training accuracy is high but test accuracy is low, overfitting is likely.
Sample Answer
What is Overfitting?
Definition: A phenomenon in which the model fits the training data too closely and its predictive performance on unseen data declines.
Symptoms:
- Accuracy on training data: 95% (high)
- Accuracy on test data: 70% (low)
Causes:
- The model is too complex (too many parameters)
- There is too little training data
- It learns even the noise and outliers
Why it is a problem:
- Prediction accuracy in the real world is low (unusable in production)
- It merely "memorizes" the patterns of the training data and does not generalize
- It cannot handle new data
Methods to Prevent Overfitting
- Increase the amount of data: Increasing the training data lets the model learn general patterns
- Regularization: Constrain parameters with L1 regularization (Lasso) or L2 regularization (Ridge)
- Cross-validation: Accurately evaluate generalization performance with K-fold cross-validation
- Simplify the model: Limit the depth of decision trees, reduce the number of layers in a neural network
- Dropout: Randomly disable some nodes in a neural network
- Early Stopping: Stop training when the validation error begins to increase
Exercise 3 (Difficulty: medium)
You want to predict customer purchases on an e-commerce site. For the following two problem settings, determine whether each is a regression problem or a classification problem, and answer with the reasons.
- (A) You want to predict the amount a customer will spend next month
- (B) You want to predict whether a customer will make a purchase within one month
Hint
If the output is a continuous value (a number), it is regression; if it is a category (Yes/No, high/medium/low, etc.), it is classification.
Sample Answer
(A) You want to predict the amount a customer will spend next month
Answer: Regression problem
Reason:
- The output is a continuous value (0 yen, 3,500 yen, 15,000 yen, etc.)
- It predicts a concrete numeric value
- Evaluation metrics: MAE (mean absolute error), RMSE (root mean squared error)
Applicable models:
- Linear Regression
- Random Forest Regressor
- XGBoost Regression
(B) You want to predict whether a customer will make a purchase within one month
Answer: Classification problem
Reason:
- The output is a category (will purchase / will not, Yes/No)
- Binary Classification
- Evaluation metrics: Accuracy, F1 score, AUC-ROC
Applicable models:
- Logistic Regression
- Random Forest Classifier
- XGBoost Classification
Business Perspective
- (A) Regression: Useful for marketing budget allocation and inventory planning
- (B) Classification: Useful for narrowing down target customers and distributing coupons
Exercise 4 (Difficulty: medium)
In feature engineering, explain two methods for numericalizing the categorical variable "prefecture." Also state the advantages and disadvantages of each.
Hint
The representative methods for numericalizing categorical variables are Label Encoding and One-Hot Encoding. Focus on the presence or absence of an ordering relationship.
Sample Answer
Method 1: Label Encoding
Method: Assign a unique integer to each prefecture
Tokyo → 0
Osaka → 1
Fukuoka → 2
Hokkaido → 3
...
Advantages:
- Memory-efficient (only one column needed)
- Easy to implement
- Does not increase the data size
Disadvantages:
- An incorrect ordering relationship is introduced: Meaningless magnitude relations such as Osaka(1) > Tokyo(0)
- Inappropriate for linear models (prefectures have no ordering)
- Usable with tree-based models (Random Forest, XGBoost)
Method 2: One-Hot Encoding
Method: Convert each prefecture into an independent binary column
Tokyo → [1, 0, 0, 0, ...] (Tokyo column is 1, others are 0)
Osaka → [0, 1, 0, 0, ...] (Osaka column is 1, others are 0)
Fukuoka → [0, 0, 1, 0, ...] (Fukuoka column is 1, others are 0)
Hokkaido → [0, 0, 0, 1, ...] (Hokkaido column is 1, others are 0)
Advantages:
- Does not create an ordering relationship: Each prefecture is treated as an independent feature
- Can be learned correctly even with linear models
- High interpretability (the "effect of Tokyo" is clear)
Disadvantages:
- The number of features increases (47 columns for 47 prefectures)
- High memory consumption
- When there are many categories (thousands or more), the curse of dimensionality
Usage Guide
| Condition | Recommended Method |
|---|---|
| Few categories (<100) | One-Hot Encoding |
| Many categories (>100) | Label Encoding + tree-based model |
| Using a linear model | One-Hot Encoding required |
| Using a tree-based model | Either works (Label Encoding is more efficient) |
Recommendation for Prefectures
Recommended: One-Hot Encoding
- The number of categories is 47, which is moderate
- There is no ordering relationship (Tokyo > Osaka makes no sense)
- High interpretability (you can analyze the effect by region)
References
- Mitchell, T. M. (1997). Machine Learning. McGraw-Hill. ISBN: 0070428077
- Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. ISBN: 0387310738
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press. https://www.deeplearningbook.org/
- scikit-learn Documentation. (2024). https://scikit-learn.org/stable/
- Koichi Kato (2018). Machine Learning at Work. O'Reilly Japan. ISBN: 4873118255
- Sebastian Raschka (2019). Python Machine Learning (3rd ed.). Packt Publishing. ISBN: 1789955750
- Aurélien Géron (2019). Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow (2nd ed.). O'Reilly Media. ISBN: 1492032646