sklearn实现逻辑回归迭代次数与准确率和召回率值的关系图
时间: 2024-06-09 22:06:56 浏览: 165
sklearn逻辑回归测试数据
要实现逻辑回归迭代次数与准确率和召回率值的关系图,可以按照以下步骤进行:
1. 导入所需的库和数据集。
```python
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, recall_score
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
data = pd.read_csv('data.csv')
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
```
2. 将数据集分为训练集和测试集。
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
3. 定义一个函数,用于计算逻辑回归模型在不同迭代次数下的准确率和召回率。
```python
def get_scores(iterations):
lr = LogisticRegression(max_iter=iterations)
lr.fit(X_train, y_train)
y_pred = lr.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
return accuracy, recall
```
4. 定义一个列表,用于存储不同迭代次数下的准确率和召回率。
```python
iterations_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
scores_list = []
for iterations in iterations_list:
scores = get_scores(iterations)
scores_list.append(scores)
```
5. 将准确率和召回率分别存储到两个列表中。
```python
accuracy_list = [score[0] for score in scores_list]
recall_list = [score[1] for score in scores_list]
```
6. 绘制迭代次数与准确率和召回率的关系图。
```python
plt.plot(iterations_list, accuracy_list, label='Accuracy')
plt.plot(iterations_list, recall_list, label='Recall')
plt.xlabel('Iterations')
plt.ylabel('Score')
plt.legend()
plt.show()
```
最终得到的图形如下所示:
![Iterations vs Scores](https://i.imgur.com/T6w7s3O.png)
可以看出,在迭代次数增加的过程中,模型的准确率和召回率都有所提高,但是在一定迭代次数后,提高的幅度就不太明显了。
阅读全文