Scikit-learn (sklearn) 库详细介绍
Scikit-learn (简称 sklearn) 是 Python 中最流行的机器学习库之一,它建立在 NumPy、SciPy 和 Matplotlib 之上,提供了简单高效的数据挖掘和数据分析工具。
1. 主要特点
- 简单易用:一致的 API 设计,学习曲线平缓
- 功能全面:覆盖了从数据预处理到模型评估的完整机器学习流程
- 高效性能:基于 NumPy 和 SciPy 实现,运算效率高
- 开源免费:BSD 许可证下的开源项目
- 文档完善:提供详细的文档和丰富的示例
2. 主要功能模块
2.1 数据预处理 (sklearn.preprocessing)
from sklearn import preprocessing# 标准化
scaler = preprocessing.StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)# 归一化
normalizer = preprocessing.MinMaxScaler().fit(X_train)
X_train_normalized = normalizer.transform(X_train)# 编码分类特征
encoder = preprocessing.OneHotEncoder().fit(X_categorical)
X_encoded = encoder.transform(X_categorical)
2.2 特征选择 (sklearn.feature_selection)
from sklearn.feature_selection import SelectKBest, chi2# 选择K个最好的特征
selector = SelectKBest(chi2, k=10)
X_new = selector.fit_transform(X, y)
2.3 模型选择 (sklearn.model_selection)
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV# 数据集划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)# 交叉验证
scores = cross_val_score(model, X, y, cv=5)# 网格搜索调参
param_grid = {'C': [0.1, 1, 10], 'gamma': [0.01, 0.1]}
grid_search = GridSearchCV(SVC(), param_grid, cv=5)
grid_search.fit(X_train, y_train)
2.4 监督学习算法
线性模型 (sklearn.linear_model)
from sklearn.linear_model import LinearRegression, LogisticRegression# 线性回归
lr = LinearRegression().fit(X_train, y_train)# 逻辑回归
logreg = LogisticRegression().fit(X_train, y_train)
支持向量机 (sklearn.svm)
from sklearn.svm import SVC, SVR# 分类
svc = SVC(kernel='rbf', C=1.0).fit(X_train, y_train)# 回归
svr = SVR(kernel='linear').fit(X_train, y_train)
决策树 (sklearn.tree)
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor# 分类树
dt_clf = DecisionTreeClassifier(max_depth=5).fit(X_train, y_train)# 回归树
dt_reg = DecisionTreeRegressor().fit(X_train, y_train)
集成方法 (sklearn.ensemble)
from sklearn.ensemble import RandomForestClassifier, GradientBoostingRegressor# 随机森林
rf = RandomForestClassifier(n_estimators=100).fit(X_train, y_train)# 梯度提升树
gbr = GradientBoostingRegressor().fit(X_train, y_train)
2.5 无监督学习算法
聚类 (sklearn.cluster)
from sklearn.cluster import KMeans, DBSCAN# K均值聚类
kmeans = KMeans(n_clusters=3).fit(X)# DBSCAN聚类
dbscan = DBSCAN(eps=0.5).fit(X)
降维 (sklearn.decomposition)
from sklearn.decomposition import PCA, TruncatedSVD# 主成分分析
pca = PCA(n_components=2).fit(X)
X_pca = pca.transform(X)# 截断SVD
svd = TruncatedSVD(n_components=100).fit(X)
2.6 模型评估 (sklearn.metrics)
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report# 分类评估
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))# 回归评估
from sklearn.metrics import mean_squared_error, r2_score
print("MSE:", mean_squared_error(y_test, y_pred))
print("R2 Score:", r2_score(y_test, y_pred))
3. 工作流程示例
# 1. 导入必要的库和模块
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report# 2. 加载数据集
iris = load_iris()
X, y = iris.data, iris.target# 3. 数据预处理
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)# 4. 训练模型
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)# 5. 评估模型
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))
4. 实用技巧
- 1.管道(Pipeline)使用:将多个处理步骤组合在一起
from sklearn.pipeline import Pipeline pipe = Pipeline([('scaler', StandardScaler()),('selector', SelectKBest(k=10)),('classifier', RandomForestClassifier()) ])
- 2.特征联合:组合不同类型的特征
from sklearn.pipeline import FeatureUnion from sklearn.decomposition import PCA from sklearn.feature_selection import SelectKBestcombined_features = FeatureUnion([("pca", PCA(n_components=3)),("univ_select", SelectKBest(k=1)) ])
- 3.自定义转换器:创建自己的预处理步骤
from sklearn.base import BaseEstimator, TransformerMixinclass MyTransformer(BaseEstimator, TransformerMixin):def fit(self, X, y=None):return selfdef transform(self, X):# 实现你的转换逻辑return X_transformed
5. 版本与安装
当前最新稳定版本:1.4.x (截至2025年8月)
安装方法:
pip install -U scikit-learn
Scikit-learn 是机器学习入门和实践的强大工具,特别适合快速原型开发和生产环境部署。它的设计哲学强调API一致性、易用性和良好的文档,使其成为Python机器学习生态系统的核心组件之一。