Scikit-learn机器学习入门:从零开始构建分类模型
# 机器学习入门:Scikit-learn实战指南
> 作者:机器学习实战
> 标签:#Python #机器学习 #Scikit-learn #实战教程
> 阅读时间:12分钟
> 难度:⭐⭐⭐⭐ (适合有Python基础)
## 前言
机器学习不再是遥不可及的技术。Scikit-learn让Python开发者能快速构建ML模型。
本文通过实战案例,带你从零开始掌握Scikit-learn核心技能!
---
## 1. Scikit-learn简介
### 1.1 什么是Scikit-learn?
Scikit-learn(sklearn)是Python最流行的机器学习库,提供:
- 监督学习:分类、回归
- 无监督学习:聚类、降维
- 模型选择与评估
- 数据预处理
### 1.2 安装
```bash
pip install scikit-learn
```
---
## 2. 数据预处理
### 2.1 标准化
```python
from sklearn.preprocessing import StandardScaler
import numpy as np
# 创建数据
X = np.array([[1, -1, 2],
[2, 0, 0],
[0, 1, -1]])
# 标准化(均值0,方差1)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print("原始数据:\n", X)
print("标准化后:\n", X_scaled)
```
### 2.2 归一化
```python
from sklearn.preprocessing import MinMaxScaler
# 归一化到[0, 1]
scaler = MinMaxScaler()
X_normalized = scaler.fit_transform(X)
```
### 2.3 编码分类变量
```python
from sklearn.preprocessing import LabelEncoder
# 标签编码
le = LabelEncoder()
y = le.fit_transform(['cat', 'dog', 'cat', 'dog', 'bird'])
print(y) # [1 2 1 2 0]
# 独热编码
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder()
Y = ohe.fit_transform(y.reshape(-1, 1)).toarray()
```
---
## 3. 监督学习:分类
### 3.1 鸢尾花分类(经典案例)
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
# 1. 加载数据
iris = load_iris()
X, y = iris.data, iris.target
# 2. 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# 3. 创建模型
knn = KNeighborsClassifier(n_neighbors=3)
# 4. 训练模型
knn.fit(X_train, y_train)
# 5. 预测
y_pred = knn.predict(X_test)
# 6. 评估
print("准确率:", accuracy_score(y_test, y_pred))
print("\n分类报告:\n", classification_report(y_test, y_pred))
```
### 3.2 支持向量机(SVM)
```python
from sklearn.svm import SVC
# 创建SVM分类器
svm = SVC(kernel='rbf', C=1.0, gamma='auto')
# 训练
svm.fit(X_train, y_train)
# 预测
y_pred = svm.predict(X_test)
# 评估
print("SVM准确率:", accuracy_score(y_test, y_pred))
```
### 3.3 随机森林
```python
from sklearn.ensemble import RandomForestClassifier
# 创建随机森林
rf = RandomForestClassifier(
n_estimators=100, # 树的数量
max_depth=5, # 最大深度
random_state=42
)
# 训练
rf.fit(X_train, y_train)
# 特征重要性
print("特征重要性:", rf.feature_importances_)
# 预测
y_pred = rf.predict(X_test)
```
---
## 4. 监督学习:回归
### 4.1 线性回归
```python
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import numpy as np
# 创建示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])
# 创建模型
model = LinearRegression()
# 训练
model.fit(X, y)
# 预测
y_pred = model.predict(X)
# 评估
print("系数:", model.coef_)
print("截距:", model.intercept_)
print("MSE:", mean_squared_error(y, y_pred))
print("R²:", r2_score(y, y_pred))
```
### 4.2 决策树回归
```python
from sklearn.tree import DecisionTreeRegressor
# 创建模型
dt = DecisionTreeRegressor(max_depth=3)
# 训练
dt.fit(X_train, y_train)
# 预测
y_pred = dt.predict(X_test)
```
---
## 5. 无监督学习:聚类
### 5.1 K-Means聚类
```python
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 创建数据
X = np.array([[1, 1], [1, 2], [5, 5], [5, 6]])
# 创建K-Means模型
kmeans = KMeans(n_clusters=2, random_state=42)
# 训练
kmeans.fit(X)
# 预测聚类标签
labels = kmeans.labels_
centers = kmeans.cluster_centers_
print("聚类标签:", labels)
print("聚类中心:\n", centers)
```
### 5.2 层次聚类
```python
from sklearn.cluster import AgglomerativeClustering
# 创建模型
agg = AgglomerativeClustering(n_clusters=2)
# 训练
agg.fit(X)
# 预测
labels = agg.labels_
```
---
## 6. 模型评估与选择
### 6.1 交叉验证
```python
from sklearn.model_selection import cross_val_score
# 5折交叉验证
scores = cross_val_score(knn, X, y, cv=5)
print("每折得分:", scores)
print("平均得分:", scores.mean())
print("标准差:", scores.std())
```
### 6.2 网格搜索(超参数优化)
```python
from sklearn.model_selection import GridSearchCV
# 定义参数网格
param_grid = {
'n_neighbors': [3, 5, 7, 9],
'weights': ['uniform', 'distance']
}
# 创建网格搜索
grid_search = GridSearchCV(
KNeighborsClassifier(),
param_grid,
cv=5,
scoring='accuracy'
)
# 训练
grid_search.fit(X_train, y_train)
# 最佳参数
print("最佳参数:", grid_search.best_params_)
print("最佳得分:", grid_search.best_score_)
# 使用最佳模型预测
y_pred = grid_search.predict(X_test)
```
---
## 7. 实战项目:房价预测
### 7.1 数据加载
```python
import pandas as pd
from sklearn.datasets import fetch_california_housing
# 加载加州房价数据集
housing = fetch_california_housing()
X = pd.DataFrame(housing.data, columns=housing.feature_names)
y = housing.target
# 查看数据
print(X.head())
print(X.describe())
```
### 7.2 数据划分
```python
from sklearn.model_selection import train_test_split
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
```
### 7.3 特征工程
```python
from sklearn.preprocessing import StandardScaler
# 标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
```
### 7.4 模型训练
```python
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
# 多个模型
models = {
'Linear Regression': LinearRegression(),
'Random Forest': RandomForestRegressor(n_estimators=100, random_state=42),
'SVR': SVR(kernel='rbf')
}
# 训练和评估
from sklearn.metrics import mean_squared_error, r2_score
for name, model in models.items():
model.fit(X_train_scaled, y_train)
y_pred = model.predict(X_test_scaled)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"{name}:")
print(f" MSE: {mse:.2f}")
print(f" R²: {r2:.2f}\n")
```
### 7.5 模型优化
```python
# 网格搜索优化随机森林
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [None, 10, 20],
'min_samples_split': [2, 5, 10]
}
grid_search = GridSearchCV(
RandomForestRegressor(random_state=42),
param_grid,
cv=5,
scoring='neg_mean_squared_error'
)
grid_search.fit(X_train_scaled, y_train)
print("最佳参数:", grid_search.best_params_)
print("最佳MSE:", -grid_search.best_score_)
```
---
## 8. 模型保存与加载
### 8.1 保存模型
```python
import joblib
# 保存模型
joblib.dump(grid_search.best_estimator_, 'house_price_model.pkl')
print("模型已保存!")
```
### 8.2 加载模型
```python
# 加载模型
loaded_model = joblib.load('house_price_model.pkl')
# 使用模型预测
new_data = [[8.3252, 41.0, 6.9841, 1.0238, 322.0, 2.55, 37.88, -122.23]]
prediction = loaded_model.predict(new_data)
print("预测房价:", prediction[0])
```
---
## 9. Pipeline完整流程
```python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 创建Pipeline
pipeline = Pipeline([
('scaler', StandardScaler()),
('classifier', RandomForestClassifier(n_estimators=100))
])
# 加载数据
iris = load_iris()
X, y = iris.data, iris.target
# 划分数据
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# 训练
pipeline.fit(X_train, y_train)
# 预测
y_pred = pipeline.predict(X_test)
# 评估
print("准确率:", accuracy_score(y_test, y_pred))
```
---
## 10. 常见问题与解决
### 10.1 过拟合
**症状**:训练集准确率99%,测试集准确率70%
**解决方案**:
- 增加训练数据
- 使用正则化
- 减少模型复杂度
- 交叉验证
```python
from sklearn.linear_model import Ridge
# 使用正则化
ridge = Ridge(alpha=1.0) # alpha是正则化强度
ridge.fit(X_train, y_train)
```
### 10.2 数据不平衡
**症状**:分类器总是预测多数类
**解决方案**:
- 调整类别权重
- 过采样/欠采样
- 使用合适的评估指标(F1-score)
```python
from sklearn.svm import SVC
# 设置类别权重
svc = SVC(class_weight='balanced')
svc.fit(X_train, y_train)
```
---
## 总结
**Scikit-learn核心要点**:
1. 数据预处理(标准化、编码)
2. 选择合适的模型
3. 训练与评估
4. 超参数优化
5. 模型保存与部署
**学习路径**:
1. 掌握基础API
2. 理解核心算法
3. 实战项目练习
4. 深入特定领域(NLP、CV)
**推荐资源**:
- 官方文档:https://scikit-learn.org/
- 《Python机器学习基础教程》
- Kaggle实战竞赛
**下一篇预告**:《VS Code插件推荐:提高效率50%》
---
**如果这篇文章对你有帮助,请点赞、收藏、转发!**
**有问题欢迎评论区讨论!** 💪
---
持续更新机器学习实战教程,欢迎关注!
更多推荐
所有评论(0)