Python Machine Learning Libraries
This chapter introduces in detail the four most core Python libraries in machine learning: NumPy, Pandas, Matplotlib, and Scikit-learn.
Machine learning libraries are like a professional toolbox, each with a specific purpose; used together, they can accomplish complex machine learning tasks.

The Roles of the Four Core Libraries
- Numpy: the foundation of numerical computing, providing efficient array operations
- Pandas: a powerful tool for data processing, providing data structures and analysis tools
- Matplotlib: the brush of data visualization, creating all kinds of charts
- scikit-learn: the Swiss Army knife of machine learning, providing a complete ML toolchain
NumPy: The Foundation of Numerical Computing
What is NumPy?
NumPy is like a calculator for mathematical computation, but vastly more powerful. It is the foundational library for Python scientific computing, providing efficient multidimensional array objects.
Core Concepts of NumPy
1. Arrays
Example
Basic NumPy Array Operations
import
numpy
as
np
Different ways to create arrays
(
"=== NumPy Array Creation ==="
)
Create from a list
arr1
=
np.
array
(
[
1
,
2
,
3
,
4
,
5
]
)
(
f
"From a list: {arr1}"
)
Create an arithmetic sequence array
arr2
=
np.
arange
(
0
,
10
,
2
)
0 to 10, step size 2
(
f
"Arithmetic sequence array: {arr2}"
)
Create an evenly spaced array
arr3
=
np.
linspace
(
0
,
1
,
5
)
0 to 1, 5 points
(
f
"Evenly spaced array: {arr3}"
)
Create special arrays
zeros_arr
=
np.
zeros
(
(
2
,
3
)
)
A 2-row, 3-column zero array
ones_arr
=
np.
ones
(
(
2
,
3
)
)
A 2-row, 3-column ones array
identity_arr
=
np.
eye
(
3
)
3x3 identity matrix
(
f
"Zero array:\n{zeros_arr}"
)
(
f
"Ones array:\n{ones_arr}"
)
(
f
"Identity matrix:\n{identity_arr}"
)
#### 2. Array Operations
## 实例
# Basic array operations
print
(
"\n=== 数组基本操作 ==="
)
# Array properties
arr
=
np.
array
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
)
print
(
f
"数组:\n{arr}"
)
print
(
f
"形状:{arr.shape}"
)
print
(
f
"维度:{arr.ndim}"
)
print
(
f
"元素个数:{arr.size}"
)
print
(
f
"数据类型:{arr.dtype}"
)
# Array indexing and slicing
print
(
f
"第一行:{arr[0]}"
)
print
(
f
"第一列:{arr[:, 0]}"
)
print
(
f
"元素[1,2]:{arr[1, 2]}"
)
# Array operations
arr1
=
np.
array
(
[
1
,
2
,
3
]
)
arr2
=
np.
array
(
[
4
,
5
,
6
]
)
print
(
f
"加法:{arr1 + arr2}"
)
print
(
f
"乘法:{arr1 * arr2}"
)
print
(
f
"点积:{np.dot(arr1, arr2)}"
)
# Statistics function
data
=
np.
array
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
)
print
(
f
"均值:{np.mean(data)}"
)
print
(
f
"标准差:{np.std(data)}"
)
print
(
f
"最大值:{np.max(data)}"
)
print
(
f
"最小值:{np.min(data)}"
)
print
(
f
"中位数:{np.median(data)}"
)
#### NumPy Practical Application Examples
## 实例
# NumPy practical application: simple linear regression
def
numpy_linear_regression
(
)
:
"""使用 NumPy 实现简单线性回归"""
# Generate sample data
np.
random
.
seed
(
42
)
X
=
2
* np.
random
.
rand
(
100
,
1
)
# 特征
y
=
4
+
3
* X + np.
random
.
randn
(
100
,
1
)
# Label + Noise
# Add x0 = 1 to X
X_b
=
np.
c_
[
np.
ones
(
(
100
,
1
)
)
,
X
]
# Add bias term
# Solve using normal equation: θ = (X^T * X)^(-1) * X^T * y
theta_best
=
np.
linalg
.
inv
(
X_b.
T
.
dot
(
X_b
)
)
.
dot
(
X_b.
T
)
.
dot
(
y
)
print
(
"=== NumPy 线性回归示例 ==="
)
print
(
f
"学习到的参数:截距={theta_best[0][0]:.2f}, 斜率={theta_best[1][0]:.2f}"
)
# 预测
X_new
=
np.
array
(
[
[
0
]
,
[
2
]
]
)
X_new_b
=
np.
c_
[
np.
ones
(
(
2
,
1
)
)
,
X_new
]
y_predict
=
X_new_b.
dot
(
theta_best
)
print
(
f
"预测结果:X=0 时 y={y_predict[0][0]:.2f}, X=2 时 y={y_predict[1][0]:.2f}"
)
return
theta_best
,
X
,
y
# Running example
theta
,
X
,
y
=
numpy_linear_regression
(
)
---
## Pandas:数据处理的利器
### 什么是 Pandas?
Pandas is like a Swiss Army knife for data processing, providing powerful data structures and data analysis tools, especially suitable for handling tabular data.
### Pandas' Core Data Structures
#### 1. Series (One-dimensional data)
## 实例
# Basic Pandas Series Operations
import
pandas
as
pd
print
(
"=== Pandas Series ==="
)
# Create Series from list
s1
=
pd.
Series
(
[
1
,
2
,
3
,
4
,
5
]
)
print
(
f
"从列表创建:\n{s1}"
)
# Series with index
s2
=
pd.
Series
(
[
10
,
20
,
30
]
,
index
=
[
'a'
,
'b'
,
'c'
]
)
print
(
f
"\n带索引的 Series:\n{s2}"
)
# Create Series from dictionary
s3
=
pd.
Series
(
{
'数学'
:
90
,
'英语'
:
85
,
'物理'
:
88
}
)
print
(
f
"\n从字典创建:\n{s3}"
)
# Series Operations
print
(
f
"\n访问元素:s2['b'] = {s2['b']}"
)
print
(
f
"切片:s2[0:2] =\n{s2[0:2]}"
)
print
(
f
"统计信息:\n{s2.describe()}"
)
#### 2. DataFrame (two-dimensional data)
## 实例
# Pandas DataFrame Basic Operations
print
(
"\n=== Pandas DataFrame ==="
)
# Create DataFrame
data
=
{
'姓名'
:
[
'张三'
,
'李四'
,
'王五'
,
'赵六'
]
,
'年龄'
:
[
25
,
30
,
35
,
28
]
,
'城市'
:
[
'北京'
,
'上海'
,
'广州'
,
'深圳'
]
,
'薪资'
:
[
15000
,
20000
,
18000
,
22000
]
}
df
=
pd.
DataFrame
(
data
)
print
(
"原始 DataFrame:"
)
print
(
df
)
# Basic DataFrame Operations
print
(
f
"\nDataFrame 形状:{df.shape}"
)
print
(
f
"\n列名:{list(df.columns)}"
)
print
(
f
"\n数据类型:\n{df.dtypes}"
)
# Select data
print
(
f
"\n选择'姓名'列:\n{df['姓名']}"
)
print
(
f
"\n选择前两行:\n{df.head(2)}"
)
print
(
f
"\n选择年龄大于28的行:\n{df[df['年龄'] > 28]}"
)
# Statistics
print
(
f
"\n数值列的统计信息:\n{df.describe()}"
)
# Add new column
df
[
'年薪'
]
=
df
[
'薪资'
]
*
12
print
(
f
"\n添加年薪列后:\n{df}"
)
#### Pandas Data Processing Example
## 实例
# Complete Pandas Data Processing Example
def
pandas_data_processing
(
)
:
"""演示 Pandas 数据处理的完整流程"""
print
(
"=== Pandas 数据处理示例 ==="
)
# 1. Create example data
np.
random
.
seed
(
42
)
n_samples
=
1000
data
=
{
'学生ID'
:
range
(
1
,
n_samples +
1
)
,
'姓名'
:
[
f
'学生{i}'
for
i
in
range
(
1
,
n_samples +
1
)
]
,
'年龄'
: np.
random
.
randint
(
18
,
25
,
n_samples
)
,
'性别'
: np.
random
.
choice
(
[
'男'
,
'女'
]
,
n_samples
)
,
'数学成绩'
: np.
random
.
normal
(
75
,
15
,
n_samples
)
,
'英语成绩'
: np.
random
.
normal
(
80
,
12
,
n_samples
)
,
'物理成绩'
: np.
random
.
normal
(
72
,
18
,
n_samples
)
,
'班级'
: np.
random
.
choice
(
[
'一班'
,
'二班'
,
'三班'
]
,
n_samples
)
}
df
=
pd.
DataFrame
(
data
)
# 2. Data Cleaning
print
(
"原始数据形状:"
,
df.
shape
)
# Handle outliers (scores should be between 0-100)
score_columns
=
[
'数学成绩'
,
'英语成绩'
,
'物理成绩'
]
for
col
in
score_columns:
df
[
col
]
=
df
[
col
]
.
clip
(
0
,
100
)
# 3. Feature Engineering
# Calculate total score and average score
df
[
'总分'
]
=
df
[
score_columns
]
.
sum
(
axis
=
1
)
df
[
'平均分'
]
=
df
[
score_columns
]
.
mean
(
axis
=
1
)
# Add level
def
get_grade
(
score
)
:
if
score
>=
90
:
return
'A'
elif
score
>=
80
:
return
'B'
elif
score
>=
70
:
return
'C'
elif
score
>=
60
:
return
'D'
else
:
return
'F'
df
[
'等级'
]
=
df
[
'平均分'
]
.
apply
(
get_grade
)
# 4. Data Analysis
print
(
"\n=== 数据分析结果 ==="
)
# Basic statistics
print
(
"各科平均分:"
)
print
(
df
[
score_columns
]
.
mean
(
)
)
# Analyze by class
print
(
"\n各班级平均分:"
)
class_avg
=
df.
groupby
(
'班级'
)
[
'平均分'
]
.
mean
(
)
print
(
class_avg
)
# Analyze by gender
print
(
"\n性别分布:"
)
gender_count
=
df
[
'性别'
]
.
value_counts
(
)
print
(
gender_count
)
# Level distribution
print
(
"\n等级分布:"
)
grade_dist
=
df
[
'等级'
]
.
value_counts
(
)
.
sort_index
(
)
print
(
grade_dist
)
# 5. Data Filtering
print
(
"\n=== 特定数据筛选 ==="
)
# Excellent students (average score > 85)
excellent_students
=
df
[
df
[
'平均分'
]
>
85
]
.
head
(
5
)
print
(
"优秀学生(前5名):"
)
print
(
excellent_students
[
[
'姓名'
,
'平均分'
,
'等级'
]
]
)
# Highest scoring student in each class
print
(
"\n各班级最高分学生:"
)
top_students
=
df.
loc
[
df.
groupby
(
'班级'
)
[
'平均分'
]
.
idxmax
(
)
]
print
(
top_students
[
[
'班级'
,
'姓名'
,
'平均分'
]
]
)
return
df
# Running example
student_df
=
pandas_data_processing
(
)
---
## Matplotlib: The paintbrush for data visualization
### 什么是 Matplotlib?
Matplotlib is like a data artist's paintbrush, capable of transforming dry data into intuitive charts, helping us understand patterns and relationships in the data.
### Matplotlib 基础图表
## 实例
# Matplotlib Basic Plot Examples
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
# Set Chinese font (to prevent Chinese from displaying as boxes)
plt.
rcParams
[
'font.sans-serif'
]
=
[
'SimHei'
,
'Arial Unicode MS'
]
plt.
rcParams
[
'axes.unicode_minus'
]
=
False
def
matplotlib_basic_charts
(
)
:
"""演示 Matplotlib 基础图表"""
print
(
"=== Matplotlib 基础图表示例 ==="
)
# 1. 折线图
plt.
figure
(
figsize
=
(
12
,
8
)
)
plt.
subplot
(
2
,
3
,
1
)
x
=
np.
linspace
(
0
,
10
,
100
)
y1
=
np.
sin
(
x
)
y2
=
np.
cos
(
x
)
plt.
plot
(
x
,
y1
,
label
=
'sin(x)'
)
plt.
plot
(
x
,
y2
,
label
=
'cos(x)'
)
plt.
title
(
'三角函数'
)
plt.
xlabel
(
'x'
)
plt.
ylabel
(
'y'
)
plt.
legend
(
)
plt.
grid
(
True
)
# 2. 散点图
plt.
subplot
(
2
,
3
,
2
)
np.
random
.
seed
(
42
)
x
=
np.
random
.
randn
(
100
)
y
=
2
* x + np.
random
.
randn
(
100
)
*
0.5
plt.
scatter
(
x
,
y
,
alpha
=
0.6
,
c
=
'blue'
)
plt.
title
(
'散点图'
)
plt.
xlabel
(
'X'
)
plt.
ylabel
(
'Y'
)
# 3. 柱状图
plt.
subplot
(
2
,
3
,
3
)
categories
=
[
'A'
,
'B'
,
'C'
,
'D'
,
'E'
]
values
=
[
23
,
45
,
56
,
78
,
32
]
plt.
bar
(
categories
,
values
,
color
=
[
'red'
,
'green'
,
'blue'
,
'orange'
,
'purple'
]
)
plt.
title
(
'柱状图'
)
plt.
xlabel
(
'类别'
)
plt.
ylabel
(
'数值'
)
# 4. 直方图
plt.
subplot
(
2
,
3
,
4
)
data
=
np.
random
.
normal
(
100
,
15
,
1000
)
plt.
hist
(
data
,
bins
=
30
,
alpha
=
0.7
,
color
=
'skyblue'
,
edgecolor
=
'black'
)
plt.
title
(
'直方图'
)
plt.
xlabel
(
'数值'
)
plt.
ylabel
(
'频数'
)
# 5. 饼图
plt.
subplot
(
2
,
3
,
5
)
sizes
=
[
30
,
25
,
20
,
15
,
10
]
labels
=
[
'A'
,
'B'
,
'C'
,
'D'
,
'E'
]
colors
=
[
'gold'
,
'lightcoral'
,
'lightskyblue'
,
'lightgreen'
,
'plum'
]
plt.
pie
(
sizes
,
labels
=
labels
,
colors
=
colors
,
autopct
=
'%1.1f%%'
,
startangle
=
90
)
plt.
title
(
'饼图'
)
# 6. 箱线图
plt.
subplot
(
2
,
3
,
6
)
data1
=
np.
random
.
normal
(
0
,
1
,
100
)
data2
=
np.
random
.
normal
(
2
,
1
,
100
)
data3
=
np.
random
.
normal
(
-
2
,
1
,
100
)
plt.
boxplot
(
[
data1
,
data2
,
data3
]
,
labels
=
[
'组1'
,
'组2'
,
'组3'
]
)
plt.
title
(
'箱线图'
)
plt.
ylabel
(
'数值'
)
plt.
tight_layout
(
)
plt.
show
(
)
print
(
"图表已显示!"
)
# Running example
matplotlib_basic_charts
(
)
#### Advanced Visualization Example
## 实例
# Advanced visualization example
def
advanced_visualization
(
)
:
"""演示高级可视化技巧"""
print
(
"=== 高级可视化示例 ==="
)
# Create more complex data
np.
random
.
seed
(
42
)
n_points
=
200
# Generate related data
x
=
np.
random
.
randn
(
n_points
)
y
=
2
* x + np.
random
.
randn
(
n_points
)
*
0.5
colors
=
np.
random
.
rand
(
n_points
)
sizes
=
1000
* np.
random
.
rand
(
n_points
)
# 1. 气泡图
plt.
figure
(
figsize
=
(
15
,
5
)
)
plt.
subplot
(
1
,
3
,
1
)
scatter
=
plt.
scatter
(
x
,
y
,
c
=
colors
,
s
=
sizes
,
alpha
=
0.6
,
cmap
=
'viridis'
)
plt.
colorbar
(
scatter
,
label
=
'颜色值'
)
plt.
title
(
'气泡图'
)
plt.
xlabel
(
'X'
)
plt.
ylabel
(
'Y'
)
# 2. 热力图
plt.
subplot
(
1
,
3
,
2
)
data
=
np.
random
.
randn
(
10
,
10
)
im
=
plt.
imshow
(
data
,
cmap
=
'coolwarm'
,
aspect
=
'auto'
)
plt.
colorbar
(
im
,
label
=
'数值'
)
plt.
title
(
'热力图'
)
# 3. Subgraph Combination
plt.
subplot
(
1
,
3
,
3
)
# Create subplot
gs
=
plt.
GridSpec
(
2
,
2
,
subplot_kw
=
{
'projection'
:
'polar'
}
)
ax1
=
plt.
subplot
(
gs
[
0
,
0
]
)
theta
=
np.
linspace
(
0
,
2
*np.
pi
,
100
)
r
=
np.
sin
(
3
*theta
)
ax1.
plot
(
theta
,
r
)
ax1.
set_title
(
'极坐标图'
)
ax2
=
plt.
subplot
(
gs
[
0
,
1
]
)
categories
=
[
'A'
,
'B'
,
'C'
,
'D'
]
values
=
[
15
,
30
,
45
,
10
]
ax2.
bar
(
categories
,
values
)
ax2.
set_title
(
'柱状图'
)
ax3
=
plt.
subplot
(
gs
[
1
,
:
]
)
x_line
=
np.
linspace
(
0
,
10
,
100
)
y_line1
=
np.
sin
(
x_line
)
y_line2
=
np.
cos
(
x_line
)
ax3.
plot
(
x_line
,
y_line1
,
label
=
'sin'
)
ax3.
plot
(
x_line
,
y_line2
,
label
=
'cos'
)
ax3.
set_title
(
'组合图'
)
ax3.
legend
(
)
plt.
tight_layout
(
)
plt.
show
(
)
print
(
"高级图表已显示!"
)
# Running example
advanced_visualization
(
)
---
## Scikit-learn: The Swiss Army Knife of Machine Learning
### 什么是 Scikit-learn?
Scikit-learn is like a toolbox for machine learning, providing a complete toolchain from data preprocessing to model training and evaluation, and is the de facto standard for machine learning in Python.
### Scikit-learn 核心功能
## 实例
# Scikit-learn core functionality example
from
sklearn.
datasets
import
make_classification
,
load_iris
from
sklearn.
model_selection
import
train_test_split
from
sklearn.
preprocessing
import
StandardScaler
,
LabelEncoder
from
sklearn.
linear_model
import
LogisticRegression
from
sklearn.
ensemble
import
RandomForestClassifier
from
sklearn.
svm
import
SVC
from
sklearn.
metrics
import
accuracy_score
,
classification_report
,
confusion_matrix
def
scikit_learn_basics
(
)
:
"""演示 Scikit-learn 的核心功能"""
print
(
"=== Scikit-learn 核心功能示例 ==="
)
# 1. Data Generation
X
,
y
=
make_classification
(
n_samples
=
1000
,
n_features
=
20
,
n_classes
=
3
,
n_informative
=
15
,
random_state
=
42
)
print
(
f
"数据形状:X={X.shape}, y={y.shape}"
)
print
(
f
"类别分布:{np.bincount(y)}"
)
# 2. Data Splitting
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.2
,
random_state
=
42
,
stratify
=
y
)
print
(
f
"训练集大小:{X_train.shape[0]}"
)
print
(
f
"测试集大小:{X_test.shape[0]}"
)
# 3. Data Preprocessing
scaler
=
StandardScaler
(
)
X_train_scaled
=
scaler.
fit_transform
(
X_train
)
X_test_scaled
=
scaler.
transform
(
X_test
)
print
(
"数据标准化完成"
)
# 4. Model Training and Comparison
models
=
{
'逻辑回归'
: LogisticRegression
(
random_state
=
42
)
,
'随机森林'
: RandomForestClassifier
(
n_estimators
=
100
,
random_state
=
42
)
,
'支持向量机'
: SVC
(
random_state
=
42
)
}
results
=
{
}
for
name
,
model
in
models.
items
(
)
:
print
(
f
"\n训练 {name}..."
)
# Train model
model.
fit
(
X_train_scaled
,
y_train
)
# 预测
y_pred
=
model.
predict
(
X_test_scaled
)
# 评估
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
results
[
name
]
=
accuracy
print
(
f
"{name} 准确率:{accuracy:.4f}"
)
print
(
f
"分类报告:\n{classification_report(y_test, y_pred)}"
)
# 5. Result Comparison
print
(
"\n=== 模型比较 ==="
)
for
name
,
accuracy
in
results.
items
(
)
:
print
(
f
"{name}: {accuracy:.4f}"
)
best_model
=
max
(
results
,
key
=
results.
get
)
print
(
f
"\n最佳模型:{best_model}"
)
return
models
[
best_model
]
# Running example
best_model
=
scikit_learn_basics
(
)
### 完整的机器学习流程
## 实例
# Complete machine learning workflow example
def
complete_ml_pipeline
(
)
:
"""演示完整的机器学习流程"""
print
(
"=== 完整机器学习流程 ==="
)
# 1. Load data
iris
=
load_iris
(
)
X
=
iris.
data
y
=
iris.
target
feature_names
=
iris.
feature_names
target_names
=
iris.
target_names
print
(
f
"数据集:{iris.DESCR.split('\n')[0]}"
)
print
(
f
"特征数量:{len(feature_names)}"
)
print
(
f
"类别数量:{len(target_names)}"
)
# 2. Data Exploration
df
=
pd.
DataFrame
(
X
,
columns
=
feature_names
)
df
[
'target'
]
=
y
print
(
"\n数据预览:"
)
print
(
df.
head
(
)
)
print
(
"\n数据统计:"
)
print
(
df.
describe
(
)
)
# 3. Data Visualization
plt.
figure
(
figsize
=
(
12
,
4
)
)
plt.
subplot
(
1
,
2
,
1
)
for
i
,
target_name
in
enumerate
(
target_names
)
:
plt.
scatter
(
df
[
df
[
'target'
]
==
i
]
[
'sepal length (cm)'
]
,
df
[
df
[
'target'
]
==
i
]
[
'sepal width (cm)'
]
,
label
=
target_name
)
plt.
xlabel
(
'花萼长度'
)
plt.
ylabel
(
'花萼宽度'
)
plt.
title
(
'花萼尺寸分布'
)
plt.
legend
(
)
plt.
subplot
(
1
,
2
,
2
)
for
i
,
target_name
in
enumerate
(
target_names
)
:
plt.
scatter
(
df
[
df
[
'target'
]
==
i
]
[
'petal length (cm)'
]
,
df
[
df
[
'target'
]
==
i
]
[
'petal width (cm)'
]
,
label
=
target_name
)
plt.
xlabel
(
'花瓣长度'
)
plt.
ylabel
(
'花瓣宽度'
)
plt.
title
(
'花瓣尺寸分布'
)
plt.
legend
(
)
plt.
tight_layout
(
)
plt.
show
(
)
# 4. Data Preparation
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.3
,
random_state
=
42
,
stratify
=
y
)
# 5. Model Training
from
sklearn.
ensemble
import
RandomForestClassifier
model
=
RandomForestClassifier
(
n_estimators
=
100
,
random_state
=
42
)
model.
fit
(
X_train
,
y_train
)
# 6. Model Evaluation
y_pred
=
model.
predict
(
X_test
)
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
print
(
f
"\n模型准确率:{accuracy:.4f}"
)
print
(
"\n混淆矩阵:"
)
print
(
confusion_matrix
(
y_test
,
y_pred
)
)
print
(
"\n分类报告:"
)
print
(
classification_report
(
y_test
,
y_pred
,
target_names
=
target_names
)
)
# 7. Feature Importance
feature_importance
=
model.
feature_importances_
feature_df
=
pd.
DataFrame
(
{
'特征'
: feature_names
,
'重要性'
: feature_importance
}
)
.
sort_values
(
'重要性'
,
ascending
=
False
)
print
(
"\n特征重要性:"
)
print
(
feature_df
)
# 8. Feature Importance Visualization
plt.
figure
(
figsize
=
(
8
,
4
)
)
plt.
bar
(
feature_df
[
'特征'
]
,
feature_df
[
'重要性'
]
)
plt.
title
(
'特征重要性'
)
plt.
xlabel
(
'特征'
)
plt.
ylabel
(
'重要性'
)
plt.
xticks
(
rotation
=
45
)
plt.
tight_layout
(
)
plt.
show
(
)
return
model
,
feature_df
# Running example
trained_model
,
feature_importance
=
complete_ml_pipeline
(
)
---
## Four Libraries Collaboration Example
## 实例
# Four libraries collaboration: A complete machine learning project
def
four_libraries_integration
(
)
:
"""演示 NumPy、Pandas、Matplotlib、Scikit-learn 的协同工作"""
print
(
"=== 四库协同工作示例 ==="
)
# 1. NumPy: Generate simulated data
np.
random
.
seed
(
42
)
n_samples
=
500
# Generate features
study_hours
=
np.
random
.
uniform
(
1
,
10
,
n_samples
)
# Study time
sleep_hours
=
np.
random
.
uniform
(
5
,
9
,
n_samples
)
# Sleep time
practice_tests
=
np.
random
.
randint
(
0
,
20
,
n_samples
)
# Number of practice questions
# Generate labels (exam scores), based on a linear combination of features plus noise
exam_scores
=
(
5
* study_hours +
3
* sleep_hours +
2
* practice_tests +
np.
random
.
normal
(
0
,
10
,
n_samples
)
)
# Ensure score is within 0-100 range
exam_scores
=
np.
clip
(
exam_scores
,
0
,
100
)
# 2. Pandas: Creating a DataFrame and performing data processing
df
=
pd.
DataFrame
(
{
'学习时间'
: study_hours
,
'睡眠时间'
: sleep_hours
,
'练习题数'
: practice_tests
,
'考试成绩'
: exam_scores
}
)
# Add level column
df
[
'等级'
]
=
pd.
cut
(
df
[
'考试成绩'
]
,
bins
=
[
0
,
60
,
70
,
80
,
90
,
100
]
,
labels
=
[
'F'
,
'D'
,
'C'
,
'B'
,
'A'
]
)
print
(
"数据预览:"
)
print
(
df.
head
(
)
)
print
(
f
"\n数据形状:{df.shape}"
)
print
(
f
"\n等级分布:"
)
print
(
df
[
'等级'
]
.
value_counts
(
)
.
sort_index
(
)
)
# 3. Matplotlib: Data Visualization
plt.
figure
(
figsize
=
(
15
,
10
)
)
# Subplot 1: Feature Distribution
plt.
subplot
(
2
,
3
,
1
)
df
[
[
'学习时间'
,
'睡眠时间'
,
'练习题数'
]
]
.
hist
(
bins
=
20
,
ax
=
plt.
gca
(
)
)
plt.
title
(
'特征分布'
)
# Subplot 2: Grade Distribution
plt.
subplot
(
2
,
3
,
2
)
plt.
hist
(
df
[
'考试成绩'
]
,
bins
=
20
,
alpha
=
0.7
,
color
=
'skyblue'
)
plt.
title
(
'考试成绩分布'
)
plt.
xlabel
(
'分数'
)
plt.
ylabel
(
'频数'
)
# Subplot 3: Study Time vs Score
plt.
subplot
(
2
,
3
,
3
)
plt.
scatter
(
df
[
'学习时间'
]
,
df
[
'考试成绩'
]
,
alpha
=
0.6
)
plt.
xlabel
(
'学习时间'
)
plt.
ylabel
(
'考试成绩'
)
plt.
title
(
'学习时间与成绩关系'
)
# Subplot 4: Sleep Time vs Grades
plt.
subplot
(
2
,
3
,
4
)
plt.
scatter
(
df
[
'睡眠时间'
]
,
df
[
'考试成绩'
]
,
alpha
=
0.6
,
color
=
'orange'
)
plt.
xlabel
(
'睡眠时间'
)
plt.
ylabel
(
'考试成绩'
)
plt.
title
(
'睡眠时间与成绩关系'
)
# Subplot 5: Number of exercises vs Score
plt.
subplot
(
2
,
3
,
5
)
plt.
scatter
(
df
[
'练习题数'
]
,
df
[
'考试成绩'
]
,
alpha
=
0.6
,
color
=
'green'
)
plt.
xlabel
(
'练习题数'
)
plt.
ylabel
(
'考试成绩'
)
plt.
title
(
'练习题数与成绩关系'
)
# Subplot 6: Level distribution pie chart
plt.
subplot
(
2
,
3
,
6
)
grade_counts
=
df
[
'等级'
]
.
value_counts
(
)
plt.
pie
(
grade_counts.
values
,
labels
=
grade_counts.
index
,
autopct
=
'%1.1f%%'
)
plt.
title
(
'等级分布'
)
plt.
tight_layout
(
)
plt.
show
(
)
# 4. Scikit-learn: Machine Learning Modeling
from
sklearn.
linear_model
import
LinearRegression
from
sklearn.
ensemble
import
RandomForestRegressor
from
sklearn.
metrics
import
mean_squared_error
,
r2_score
# Prepare data
X
=
df
[
[
'学习时间'
,
'睡眠时间'
,
'练习题数'
]
]
y
=
df
[
'考试成绩'
]
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.2
,
random_state
=
42
)
# Train linear regression model
lr_model
=
LinearRegression
(
)
lr_model.
fit
(
X_train
,
y_train
)
lr_pred
=
lr_model.
predict
(
X_test
)
lr_mse
=
mean_squared_error
(
y_test
,
lr_pred
)
lr_r2
=
r2_score
(
y_test
,
lr_pred
)
# Train Random Forest model
rf_model
=
RandomForestRegressor
(
n_estimators
=
100
,
random_state
=
42
)
rf_model.
fit
(
X_train
,
y_train
)
rf_pred
=
rf_model.
predict
(
X_test
)
rf_mse
=
mean_squared_error
(
y_test
,
rf_pred
)
rf_r2
=
r2_score
(
y_test
,
rf_pred
)
# Model Comparison
print
(
"\n=== 模型比较 ==="
)
print
(
f
"线性回归:MSE={lr_mse:.2f}, R²={lr_r2:.4f}"
)
print
(
f
"随机森林:MSE={rf_mse:.2f}, R²={rf_r2:.4f}"
)
# Linear Regression Coefficients
print
(
f
"\n线性回归系数:"
)
for
feature
,
coef
in
zip
(
X.
columns
,
lr_model.
coef_
)
:
print
(
f
"{feature}: {coef:.2f}"
)
# Random Forest Feature Importance
print
(
f
"\n随机森林特征重要性:"
)
for
feature
,
importance
in
zip
(
X.
columns
,
rf_model.
feature_importances_
)
:
print
(
f
"{feature}: {importance:.4f}"
)
# Visualize prediction results
plt.
figure
(
figsize
=
(
12
,
5
)
)
plt.
subplot
(
1
,
2
,
1
)
plt.
scatter
(
y_test
,
lr_pred
,
alpha
=
0.6
)
plt.
plot
(
[
y_test.
min
(
)
,
y_test.
max
(
)
]
,
[
y_test.
min
(
)
,
y_test.
max
(
)
]
,
'r--'
)
plt.
xlabel
(
'真实成绩'
)
plt.
ylabel
(
'预测成绩'
)
plt.
title
(
'线性回归预测结果'
)
plt.
subplot
(
1
,
2
,
2
)
plt.
scatter
(
y_test
,
rf_pred
,
alpha
=
0.6
)
plt.
plot
(
[
y_test.
min
(
)
,
y_test.
max
(
)
]
,
[
y_test.
min
(
)
,
y_test.
max
(
)
]
,
'r--'
)
plt.
xlabel
(
'真实成绩'
)
plt.
ylabel
(
'预测成绩'
)
plt.
title
(
'随机森林预测结果'
)
plt.
tight_layout
(
)
plt.
show
(
)
return
{
'data'
: df
,
'linear_model'
: lr_model
,
'rf_model'
: rf_model
,
'linear_metrics'
:
{
'mse'
: lr_mse
,
'r2'
: lr_r2
}
,
'rf_metrics'
:
{
'mse'
: rf_mse
,
'r2'
: rf_r2
}
}
# Run full example
results
=
four_libraries_integration
(
)