Python代码,良性恶性肿瘤数据,直接从sklearn导入如下 from sklearn.datasets import load_breast_cancer cancer = load_breast_cancer() 划分测试与训练集,用logistic回归完成分类任务
时间: 2023-12-30 22:06:10 浏览: 137
Python数据集乳腺癌数据集(from sklearn.datasets import load-breast-cancer)
5星 · 资源好评率100%
好的,以下是Python代码实现:
```
# 导入模块
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 加载数据集
cancer = load_breast_cancer()
# 划分训练集和测试集,将数据集划分为7:3
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, test_size=0.3, random_state=0)
# 初始化逻辑回归模型
logistic = LogisticRegression()
# 训练模型
logistic.fit(X_train, y_train)
# 预测结果
y_pred = logistic.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
# 输出准确率
print('Accuracy:', accuracy)
```
这段代码首先导入了必要的模块,加载了sklearn的乳腺癌数据集,然后将数据集划分为训练集和测试集,使用逻辑回归模型进行训练和预测,并计算了准确率。最后输出了准确率。
阅读全文