通过python用逻辑回归模型来复现内置乳腺癌数据集的分类可视化
时间: 2023-09-05 14:07:51 浏览: 118
可以,首先需要导入必要的模块、数据集和预处理数据。在导入模块后,利用sklearn库内置的load_breast_cancer()函数加载内置乳腺癌数据集,利用train_test_split()函数将数据集分成训练集和测试集,并利用StandardScaler()函数标准化数据。随后,利用LogisticRegression()函数创建逻辑回归模型,调用fit()函数进行训练,并利用predict()函数进行预测。最后,利用matplotlib库进行可视化。以下是示例代码:
```
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
# 加载数据集
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, random_state=42)
# 数据标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 创建逻辑回归模型
lr = LogisticRegression()
lr.fit(X_train_scaled, y_train)
# 预测分类结果
y_pred = lr.predict(X_test_scaled)
# 计算模型得分
score = lr.score(X_test_scaled, y_test)
print('模型得分:', score)
# 可视化分类结果
fig, ax = plt.subplots(figsize=(10, 7))
ax.scatter(X_test_scaled[:, 0], X_test_scaled[:, 1], c=y_pred, cmap='viridis')
ax.set(xlabel='Feature 0', ylabel='Feature 1', title='Breast Cancer Dataset Classification')
plt.show()
```
阅读全文