python
复制代码
# SVM 实战演示:学生是否及格分类任务
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
plt.rcParams['font.family'] = 'Arial Unicode MS' # Mac 用户可用
plt.rcParams['axes.unicode_minus'] = False
# 1. 构造数据(成绩 + 性别)
np.random.seed(42)
size = 100
scores = np.random.randint(40, 100, size)
genders = np.random.choice([0, 1], size=size)
labels = (scores >= 60).astype(int)
X = np.column_stack(((scores - scores.mean()) / scores.std(), genders))
y = labels
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 2. 训练两个 SVM 模型
models = {
'Linear SVM': SVC(kernel='linear'),
'RBF SVM': SVC(kernel='rbf')
}
# 3. 画图准备
def plot_decision_boundary(model, X, y, title):
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 300),
np.linspace(y_min, y_max, 300))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3, cmap=plt.cm.coolwarm)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm, edgecolors='k')
plt.xlabel("标准化成绩")
plt.ylabel("性别(0=女,1=男)")
plt.title(title)
plt.tight_layout()
plt.show()
# 4. 训练 & 可视化每个模型
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f"\\n=== {name} ===")
print("准确率:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
plot_decision_boundary(model, X, y, title=name + " 分类边界")