Python scikit-learn机器学习教程

360影视 动漫周边 2025-05-07 17:36 2

摘要:以下是scikit-learn的快速入门教程,涵盖机器学习的基本流程和常见操作,帮助你快速上手使用Python进行机器学习任务。

以下是scikit-learn的快速入门教程,涵盖机器学习的基本流程和常见操作,帮助你快速上手使用Python进行机器学习任务。

1. 环境安装

bash

pip install numpy pandas matplotlib scikit-learn

2. 加载数据

Scikit-learn提供内置数据集:

python

from sklearn import datasets

# 加载鸢尾花数据集

iris = datasets.load_iris

X = iris.data # 特征矩阵 (150个样本, 4个特征)

y = iris.target # 目标变量 (3种类别)

# 加载波士顿房价数据集(回归任务)

boston = datasets.load_boston

3. 数据预处理

划分训练集和测试集

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

)

特征标准化

python

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler

X_train_scaled = scaler.fit_transform(X_train) # 仅在训练集上fit

X_test_scaled = scaler.transform(X_test) # 使用训练集的参数转换测试集

4. 选择模型

分类示例:支持向量机 (SVM)

python

from sklearn.svm import SVC

model = SVC(kernel='linear', C=1.0)

model.fit(X_train_scaled, y_train)

回归示例:线性回归

python

from sklearn.linear_model import LinearRegression

model = LinearRegression

model.fit(X_train_scaled, y_train)

5. 模型评估

分类任务指标

python

from sklearn.metrics import Accuracy_score, classification_report

y_pred = model.predict(X_test_scaled)

print("Accuracy:", accuracy_score(y_test, y_pred))

print(classification_report(y_test, y_pred))

回归任务指标

python

from sklearn.metrics import mean_squared_error, r2_score

y_pred = model.predict(X_test_scaled)

print("MSE:", mean_squared_error(y_test, y_pred))

print("R²:", r2_score(y_test, y_pred))

6. 超参数调优

网格搜索交叉验证

python

from sklearn.model_selection import GridSearchCV

param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}

grid_search = GridSearchCV(SVC, param_grid, cv=5)

grid_search.fit(X_train_scaled, y_train)

print("最佳参数:", grid_search.best_params_)

best_model = grid_search.best_estimator_

7. 特征工程

PCA降维

python

from sklearn.decomposition import PCA

pca = PCA(n_components=2)

X_pca = pca.fit_transform(X_train_scaled)

特征选择

python

from sklearn.feature_selection import SelectKBest, f_classif

selector = SelectKBest(f_classif, k=2)

X_selected = selector.fit_transform(X_train_scaled, y_train)

8. 保存和加载模型

python

import joblib

# 保存模型

joblib.dump(model, 'iris_classifier.pkl')

# 加载模型

loaded_model = joblib.load('iris_classifier.pkl')

完整示例流程(鸢尾花分类)

python

# 加载数据

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score

# 数据准备

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.2, random_state=42)

# 预处理

scaler = StandardScaler

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = scaler.transform(X_test)

# 训练模型

model = SVC(kernel='rbf', C=1.0)

model.fit(X_train_scaled, y_train)

# 预测与评估

y_pred = model.predict(X_test_scaled)

print("测试集准确率:", accuracy_score(y_test, y_pred))

过拟合:增加正则化参数(如C值减小)、减少特征数量、使用交叉验证欠拟合:增加模型复杂度(如使用非线性核)、增加特征工程数据泄露:确保预处理步骤仅在训练集上fit,再应用到测试集类别不平衡:使用class_weight参数或过采样技术(如SMOTE)

通过以上步骤,你可以快速实现大多数机器学习任务。建议从内置数据集开始练习,逐步尝试自定义数据集的完整流程。

来源:老客数据一点号

相关推荐