Scikit-learn 是一个功能强大的机器学习库,支持数据预处理、分类、回归、聚类、降维等功能,广泛用于模型开发与评估。以下是 Scikit-learn 的常用方法及详细说明。
1. 安装与导入
安装 Scikit-learn:
pip install scikit-learn
导入基本模块:
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import accuracy_score, mean_squared_error
2. 数据预处理
2.1 数据标准化
用于将数据缩放到均值为 0,标准差为 1。
import numpy as np
from sklearn.preprocessing import StandardScaler
# 示例数据
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
scaler = StandardScaler()
scaled_data = scaler.fit_transform(data)
print(scaled_data)
输出结果
[[-1.22474487 -1.22474487 -1.22474487]
[ 0. 0. 0. ]
[ 1.22474487 1.22474487 1.22474487]]
2.2 标签编码
用于将类别变量转换为数值。
from sklearn.preprocessing import LabelEncoder
labels = ['cat', 'dog', 'mouse', 'cat']
encoder = LabelEncoder()
encoded_labels = encoder.fit_transform(labels)
print(encoded_labels)
输出结果
[0 1 2 0]
2.3 特征选择
选择重要的特征以优化模型。
from sklearn import datasets
from sklearn.feature_selection import SelectKBest, f_classif
X, y = datasets.load_iris(return_X_y=True)
selector = SelectKBest(score_func=f_classif, k=2)
X_selected = selector.fit_transform(X, y)
print(X_selected[:5])
输出结果
[[1.4 0.2]
[1.4 0.2]
[1.3 0.2]
[1.5 0.2]
[1.4 0.2]]
3. 模型训练与评估
3.1 线性回归
适用于连续目标变量。
from sklearn import datasets
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from MSE import mean_squared_error
# 生成样本数据
X, y = datasets.make_regression(n_samples=100, n_features=1, noise=10, random_state=42)
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 模型训练
model = LinearRegression()
model.fit(X_train, y_train)
# 模型评估
y_pred = model.predict(X_test)
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
输出结果
Cost (MSE): 4.28125
Mean Squared Error: 52.101113265935126
3.2 逻辑回归
适用于分类问题。
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
# 加载数据
X, y = datasets.load_iris(return_X_y=True)
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 模型训练
clf = LogisticRegression(max_iter=200)
clf.fit(X_train, y_train)
# 模型评估
y_pred = clf.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
输出结果
Accuracy: 1.0
3.3 支持向量机
用于分类和回归。
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
# 加载数据
X, y = datasets.load_iris(return_X_y=True)
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 模型训练
svc = SVC(kernel='linear')
svc.fit(X_train, y_train)
# 模型评估
y_pred = svc.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
输出结果
Accuracy: 1.0
4. 无监督学习
4.1 K-means 聚类
from sklearn import datasets
from sklearn.cluster import KMeans
# 生成数据
X, _ = datasets.make_blobs(n_samples=300, centers=4, cluster_std=0.6, random_state=42)
# 模型训练
kmeans = KMeans(n_clusters=4, random_state=42, n_init=10)
kmeans.fit(X)
# 可视化
import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red', marker='X')
plt.title("K-means Clustering")
plt.show()
4.2 PCA 降维
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn.decomposition import PCA
# 加载数据
X, y = datasets.load_iris(return_X_y=True)
# 降维
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
# 可视化
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y, cmap='viridis')
plt.title("PCA Dimensionality Reduction")
plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")
plt.show()
5. 模型优化
5.1 交叉验证
from sklearn import datasets
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
X, y = datasets.load_iris(return_X_y=True)
# 线性回归交叉验证
scores = cross_val_score(LinearRegression(), X, y, cv=5, scoring='neg_mean_squared_error')
print("Cross-validated MSE:", -scores.mean())
输出结果
Cross-validated MSE: 0.0689701255462469
5.2 网格搜索
from sklearn import datasets
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.svm import SVC
# 加载数据
X, y = datasets.load_iris(return_X_y=True)
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 参数网格
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}
grid = GridSearchCV(SVC(), param_grid, cv=5)
grid.fit(X_train, y_train)
print("Best parameters:", grid.best_params_)
输出结果
Best parameters: {'C': 1, 'kernel': 'linear'}
6. 总结
Scikit-learn 提供了丰富的机器学习工具和简单的 API,可实现从数据预处理到模型评估的完整流程。通过上述方法,您可以快速构建和优化机器学习模型,从简单线性回归到复杂的非监督学习和降维方法。