Python案例如何用Scikit-learn做二分匹配

wen python案例 2

本文目录导读:

Python案例如何用Scikit-learn做二分匹配

  1. 基础二分匹配案例(以逻辑回归为例)
  2. 多模型比较案例
  3. 使用真实数据集(鸢尾花数据集)
  4. 模型优化(超参数调优)
  5. 实际应用示例:处理不平衡数据

我来详细讲解如何使用Scikit-learn进行二分匹配(Binary Classification)的完整案例。

基础二分匹配案例(以逻辑回归为例)

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import confusion_matrix, classification_report
import matplotlib.pyplot as plt
import seaborn as sns
# 1. 生成示例数据
np.random.seed(42)
n_samples = 1000
# 创建特征
X = np.random.randn(n_samples, 4)  # 4个特征
# 创建二元目标变量 (0或1)
y = (X[:, 0] + X[:, 1] * 2 - X[:, 2] * 0.5 + np.random.randn(n_samples) * 0.5 > 0).astype(int)
# 2. 分割数据
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)
# 3. 数据标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 4. 训练模型
model = LogisticRegression(random_state=42)
model.fit(X_train_scaled, y_train)
# 5. 预测
y_pred = model.predict(X_test_scaled)
y_pred_proba = model.predict_proba(X_test_scaled)[:, 1]
# 6. 评估模型
print("=== 模型性能评估 ===")
print(f"准确率: {accuracy_score(y_test, y_pred):.4f}")
print(f"精确率: {precision_score(y_test, y_pred):.4f}")
print(f"召回率: {recall_score(y_test, y_pred):.4f}")
print(f"F1分数: {f1_score(y_test, y_pred):.4f}")
print("\n=== 分类报告 ===")
print(classification_report(y_test, y_pred))
print("\n=== 混淆矩阵 ===")
cm = confusion_matrix(y_test, y_pred)
print(cm)
# 7. 特征重要性
feature_importance = pd.DataFrame({
    'feature': [f'Feature_{i}' for i in range(X.shape[1])],
    'importance': abs(model.coef_[0])
})
print("\n=== 特征重要性 ===")
print(feature_importance.sort_values('importance', ascending=False))
# 8. 可视化混淆矩阵
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')'Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()

多模型比较案例

from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
import warnings
warnings.filterwarnings('ignore')
# 定义多个分类器
classifiers = {
    'Logistic Regression': LogisticRegression(random_state=42),
    'K-Nearest Neighbors': KNeighborsClassifier(n_neighbors=5),
    'Decision Tree': DecisionTreeClassifier(random_state=42),
    'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42),
    'SVM': SVC(kernel='rbf', random_state=42),
    'Gradient Boosting': GradientBoostingClassifier(random_state=42)
}
# 比较不同模型
results = {}
for name, clf in classifiers.items():
    # 使用交叉验证
    cv_scores = cross_val_score(clf, X_train_scaled, y_train, cv=5, scoring='accuracy')
    results[name] = {
        'mean_accuracy': cv_scores.mean(),
        'std_accuracy': cv_scores.std()
    }
    # 训练和测试
    clf.fit(X_train_scaled, y_train)
    y_pred = clf.predict(X_test_scaled)
    accuracy = accuracy_score(y_test, y_pred)
    results[name]['test_accuracy'] = accuracy
# 显示结果
results_df = pd.DataFrame(results).T
print("\n=== 模型性能比较 ===")
print(results_df.round(4))
# 可视化比较
plt.figure(figsize=(12, 6))
models = list(results.keys())
accuracies = [results[m]['test_accuracy'] for m in models]
plt.bar(models, accuracies)
plt.xticks(rotation=45)
plt.ylabel('Test Accuracy')'Model Comparison')
plt.tight_layout()
plt.show()

使用真实数据集(鸢尾花数据集)

from sklearn.datasets import load_breast_cancer
# 加载乳腺癌数据集(二分匹配的经典数据集)
data = load_breast_cancer()
X = data.data
y = data.target
print("数据集信息:")
print(f"样本数: {X.shape[0]}")
print(f"特征数: {X.shape[1]}")
print(f"类别分布:\n{pd.Series(y).value_counts()}")
print(f"特征名称: {data.feature_names[:5]}...")
# 分割数据
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)
# 标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 使用随机森林(通常对二分匹配效果好)
rf_model = RandomForestClassifier(
    n_estimators=100,
    max_depth=10,
    random_state=42
)
rf_model.fit(X_train_scaled, y_train)
# 预测
y_pred = rf_model.predict(X_test_scaled)
y_pred_proba = rf_model.predict_proba(X_test_scaled)[:, 1]
# 详细评估
print("\n=== 详细模型评估 ===")
print(f"准确率: {accuracy_score(y_test, y_pred):.4f}")
print(f"AUC-ROC得分: {roc_auc_score(y_test, y_pred_proba):.4f}")
# 绘制ROC曲线
from sklearn.metrics import roc_curve, auc
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')'Receiver Operating Characteristic (ROC) Curve')
plt.legend(loc="lower right")
plt.show()
# 特征重要性排名
feature_importance = pd.DataFrame({
    'feature': data.feature_names,
    'importance': rf_model.feature_importances_
}).sort_values('importance', ascending=False)
print("\n=== 最重要的10个特征 ===")
print(feature_importance.head(10))

模型优化(超参数调优)

from sklearn.model_selection import GridSearchCV
# 定义参数网格
param_grid = {
    'n_estimators': [50, 100, 200],
    'max_depth': [5, 10, 15, None],
    'min_samples_split': [2, 5, 10],
    'min_samples_leaf': [1, 2, 4]
}
# 网格搜索
rf = RandomForestClassifier(random_state=42)
grid_search = GridSearchCV(
    rf, param_grid, cv=5, 
    scoring='accuracy', 
    n_jobs=-1,
    verbose=1
)
print("开始超参数优化...")
grid_search.fit(X_train_scaled, y_train)
print(f"\n最佳参数: {grid_search.best_params_}")
print(f"最佳交叉验证得分: {grid_search.best_score_:.4f}")
# 使用最佳模型
best_rf = grid_search.best_estimator_
y_pred_best = best_rf.predict(X_test_scaled)
print(f"测试集准确率: {accuracy_score(y_test, y_pred_best):.4f}")

实际应用示例:处理不平衡数据

from imblearn.over_sampling import SMOTE
from imblearn.pipeline import make_pipeline as make_pipeline_imb
# 创建不平衡数据(模拟实际场景)
np.random.seed(42)
n_samples = 1000
X_imb = np.random.randn(n_samples, 2)
y_imb = np.zeros(n_samples)
# 只有10%的正样本
y_imb[:100] = 1  
X_imb[y_imb==1] += 1.5
print("不平衡数据集分布:")
print(pd.Series(y_imb).value_counts(normalize=True))
# 分割数据
X_train_imb, X_test_imb, y_train_imb, y_test_imb = train_test_split(
    X_imb, y_imb, test_size=0.2, random_state=42, stratify=y_imb
)
# 使用SMOTE解决不平衡问题
smote_pipeline = make_pipeline_imb(
    StandardScaler(),
    SMOTE(random_state=42),
    RandomForestClassifier(random_state=42)
)
smote_pipeline.fit(X_train_imb, y_train_imb)
y_pred_imb = smote_pipeline.predict(X_test_imb)
print("\nSMOTE结果:")
print(classification_report(y_test_imb, y_pred_imb))
  1. 数据预处理:标准化对不同算法很重要
  2. 模型选择:根据数据特点选择合适的模型
  3. 交叉验证:避免过拟合
  4. 评估指标:不只看准确率,还要看精确率、召回率、F1分数
  5. 不平衡处理:使用SMOTE等技术处理不平衡数据
  6. 超参数优化:使用网格搜索找到最佳参数

这些案例覆盖了Scikit-learn进行二分匹配的主要方面,可以根据实际需求选择合适的方法。

抱歉,评论功能暂时关闭!