逻辑回归的sklearn实现可视化 乳腺癌分类
时间: 2024-03-21 07:14:37 浏览: 68
可以使用matplotlib库进行可视化,以下是一段逻辑回归的sklearn实现代码,可以通过调整参数来进行不同数据集的乳腺癌分类:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 加载数据集
cancer = load_breast_cancer()
X = cancer.data
y = cancer.target
# 数据集划分
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# 特征缩放
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 构造模型
logreg = LogisticRegression()
logreg.fit(X_train_scaled, y_train)
# 绘制可视化图形
plt.figure(figsize=(10, 6))
plt.plot(logreg.coef_.T, 'o', label="C=1")
plt.xticks(range(cancer.data.shape[1]), cancer.feature_names, rotation=90)
plt.hlines(0, 0, cancer.data.shape[1])
plt.ylabel("Coefficient magnitude")
plt.xlabel("Coefficient index")
plt.legend()
plt.show()
```
上述代码可以绘制出逻辑回归模型的系数,可以清晰地看到哪些特征对分类结果的影响更大。
阅读全文