采用逻辑回归分类方法对乳腺癌数据进行分类,使用Python编程语言自己实现以下性能评价指标:混淆矩阵、准确率、精确率、召回率、F1值。
时间: 2023-07-10 09:06:40 浏览: 134
好的,下面是Python代码实现:
首先,我们需要导入相关的库:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
```
然后,我们加载乳腺癌数据集:
```python
data = load_breast_cancer()
X = data.data
y = data.target
```
接着,我们将数据集分成训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
我们使用逻辑回归模型进行训练:
```python
model = LogisticRegression()
model.fit(X_train, y_train)
```
现在,我们可以用模型对测试集进行预测:
```python
y_pred = model.predict(X_test)
```
接下来,我们可以根据预测结果计算性能评价指标:
混淆矩阵:
```python
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
```
准确率、精确率、召回率、F1值:
```python
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 score:", f1)
```
完整的代码如下:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
data = load_breast_cancer()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
print("Confusion matrix:")
print(cm)
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 score:", f1)
```
希望对你有所帮助!
阅读全文