Machine Learning Tutorial
Machine Learning is a branch of Artificial Intelligence (AI) that enables computer systems to automatically learn and improve their performance using data and algorithms.
Machine learning allows machines to make decisions and predictions based on experience (data).
Machine learning has been widely applied in many fields, including recommendation systems, image recognition, speech recognition, financial analysis, and more.
For example, through machine learning, cars can learn to recognize traffic signs, pedestrians, and obstacles to achieve autonomous driving.
Differences between Machine Learning and Traditional Programming
| Traditional Programming | Machine Learning |
|---|---|
| Programmers write explicit rules | Computers learn rules from data |
| Suitable for problems with clear rules and well-defined issues | Suitable for complex situations where rules are difficult to define |
| Example: Writing a calculator program | Example: Writing a spam email recognition program |

Three Essential Elements of Machine Learning
Machine learning involves three basic elements:
1. Data
Data is the fuel of machine learning. The higher the quality and the greater the quantity of data, the better the model can usually learn.
- Training data: Data used to teach the model
- Test data: Data used to evaluate how well the model has learned
- Real data: New data encountered by the model in real-world applications
2. Algorithm
Algorithms are the learning methods of machine learning. Different algorithms are suitable for different types of problems.
- Supervised learning: Learning with labeled answers
- Unsupervised learning: No labeled answers; finding patterns on its own
- Reinforcement learning: Learning through trial and error and rewards
3. Model
A model is the result of learning, just like the knowledge a student acquires.
- Training process: The algorithm learns patterns from data
- Inference process: Using the learned patterns to make predictions
Example
Next, we will use a simple example to understand the basic workflow of machine learning.
We will use Python to create a simple linear regression model to predict house prices.
Example
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import seaborn as sns
# Set the chart style to make the charts look better
sns.set_style("whitegrid")
# -------------------------- Start of setting Chinese font --------------------------
plt.rcParams['font.sans-serif'] = [
# Windows preferred
'SimHei', 'Microsoft YaHei',
# macOS preferred
'PingFang SC', 'Heiti TC',
# Linux preferred
'WenQuanYi Micro Hei', 'DejaVu Sans'
]
# Fix the issue where negative signs display as squares
plt.rcParams['axes.unicode_minus'] = False
# -------------------------- End of setting Chinese font --------------------------
# 1. Prepare the data
# Assume we have data on house area and corresponding prices
# House area (square meters)
house_sizes = np.array([50, 60, 70, 80, 90, 100, 110, 120]).reshape(-1, 1)
# House price (ten thousand yuan)
house_prices = np.array([150, 180, 210, 240, 270, 300, 330, 360])
# 2. Create and train the model
# Create a linear regression model
model = LinearRegression()
# Train the model with data (learn the relationship between area and price)
model.fit(house_sizes, house_prices)
# 3. Use the model to make predictions
# Predict the price of an 85 square meter house
predicted_price = model.predict([[85]])
print(f"Predicted price for an 85 square meter house: {predicted_price)
# 4. Visualize the results
plt.scatter(house_sizes, house_prices, color='blue', label='Actual data')
plt.plot(house_sizes, model.predict(house_sizes), color='red', label='Prediction line')
plt.scatter([85], predicted_price, color='green', s=100, label='Prediction point')
plt.xlabel('House area (square meters)')
plt.ylabel('House price (ten thousand yuan)')
plt.title('EXAMPLE Machine Learning Test -- Relationship between House Area and Price')
plt.legend()
plt.grid(True)
plt.show()
Execution result:
85 平方米的房屋预测价格:255.00 万元
This example demonstrates the basic workflow of machine learning:
- Prepare data (house area and price)
- Choose an algorithm (linear regression)
- Train the model (let the computer learn the relationship between area and price)
- Use the model to predict (predict the price for a new area)
The output chart is as follows:
