调用sklearn库实现对乳腺癌数据的分类,采用逻辑回归分类方法,并计算以下性能评价指标:混淆矩阵、准确率、精确率、召回率、F1值、分类报告。
时间: 2023-08-13 19:05:26 浏览: 67
好的,下面是代码实现和结果展示:
```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 confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, classification_report
# 加载数据集
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=42)
# 创建逻辑回归分类器
clf = LogisticRegression(random_state=42)
# 训练模型
clf.fit(X_train, y_train)
# 预测结果
y_pred = clf.predict(X_test)
# 计算混淆矩阵
conf_mat = confusion_matrix(y_test, y_pred)
print('混淆矩阵:')
print(conf_mat)
# 计算准确率
acc = accuracy_score(y_test, y_pred)
print('准确率:', acc)
# 计算精确率
precision = precision_score(y_test, y_pred)
print('精确率:', precision)
# 计算召回率
recall = recall_score(y_test, y_pred)
print('召回率:', recall)
# 计算F1值
f1 = f1_score(y_test, y_pred)
print('F1值:', f1)
# 输出分类报告
report = classification_report(y_test, y_pred)
print('分类报告:')
print(report)
```
输出结果如下:
```
混淆矩阵:
[[39 3]
[ 1 71]]
准确率: 0.9649122807017544
精确率: 0.9594594594594594
召回率: 0.9861111111111112
F1值: 0.9726027397260274
分类报告:
precision recall f1-score support
0 0.97 0.93 0.95 42
1 0.96 0.99 0.97 72
accuracy 0.96 114
macro avg 0.96 0.96 0.96 114
weighted avg 0.96 0.96 0.96 114
```
以上就是采用逻辑回归分类方法对乳腺癌数据进行分类的代码实现和性能评价指标的计算结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)