通过python用逻辑回归模型来复现内置乳腺癌数据集的分类结果
时间: 2023-09-04 10:17:25 浏览: 57
可以使用scikit-learn库中的load_breast_cancer函数加载内置乳腺癌数据集,之后使用train_test_split函数将数据集分为训练集和测试集,再使用LogisticRegression类来构建逻辑回归模型,并在训练集上进行训练。最后,在测试集上评估模型的性能。以下是大致的代码示例:
```python
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# 加载数据集
data = load_breast_cancer()
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=0)
# 构建逻辑回归模型
lr = LogisticRegression(max_iter=1000)
# 在训练集上训练模型
lr.fit(X_train, y_train)
# 在测试集上评估模型性能
score = lr.score(X_test, y_test)
print(f"Accuracy on test set: {score}")
```
注意,这只是一个简单的示例,实际上可以对模型进行很多修改和优化,以提高其性能。