Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Machine Learning Tutorial

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 enables machines to make decisions and predictions through 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 how to recognize traffic signs, pedestrians, and obstacles to achieve autonomous driving.


The difference between machine learning and traditional programming

Traditional programming Machine learning
Programmers write explicit rules Computers learn rules from data
Suitable for situations where the problem is clear and the rules are well-defined Suitable for complex situations where rules are difficult to define explicitly
Example: writing a calculator program Example: writing a program to recognize spam emails


The three key elements of machine learning

Machine learning consists of 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 usually learns.

2. Algorithms

Algorithms are the learning methods of machine learning; different algorithms are suited to different types of problems.

3. Model

A model is the result of learning, just like the knowledge a student acquires.


Example

Next, we will understand the basic workflow of machine learning through a simple example.

We will use Python to create a simple linear regression model to predict house prices.

Example

Import the required libraries

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"

)

-------------------------- Set Chinese font start --------------------------

plt.

rcParams

[

'font.sans-serif'

]

=

[

   

Windows first

   

'SimHei'

,

'Microsoft YaHei'

,

   

macOS first

   

'PingFang SC'

,

'Heiti TC'

,

   

Linux first

   

'WenQuanYi Micro Hei'

,

'DejaVu Sans'

]

Fix the issue of minus signs displaying as squares

plt.

rcParams

[

'axes.unicode_minus'

]

=

False

-------------------------- Set Chinese font end --------------------------

1. Prepare data

Suppose 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 the 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[0]:.2f} ten thousand yuan"

)

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 points'

)

plt.

xlabel

(

'House area (square meters)'

)

plt.

ylabel

(

'House price (ten thousand yuan)'

)

plt.

title

(

'RUNOOB Machine Learning Test -- House Area and Price Relationship'

)

plt.

legend

(

)

plt.

grid

(

True

)

plt.

show

(

)

Running result:

85 平方米的房屋预测价格:255.00 万元

This example demonstrates the basic workflow of machine learning:

  1. Prepare data (house area and price)
  2. Choose an algorithm (linear regression)
  3. Train the model (let the computer learn the relationship between area and price)
  4. Use the model to predict (predict the price for a new area)

The output graph is as follows: