.使用Python的scikit-learn库来实现一个逻辑回归模型。你需要使用糖尿病数据集(Diabetes dataset)来训练模型,并预测患者是否患有糖尿病。 提示: 目标变量的值大于median的视为1,小于等于median的视为0
时间: 2024-10-26 15:17:17 浏览: 18
在Python中,我们可以使用scikit-learn库中的`LogisticRegression`类来创建逻辑回归模型。首先,需要加载所需的库和数据集。这里我们假设已经安装了`pandas`、`numpy`和`scikit-learn`。以下是实现步骤:
1. 导入所需库:
```python
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import MinMaxScaler
```
2. 加载糖尿病数据集(如果数据集未直接包含在scikit-learn中,你可以从其他地方获取数据并清洗预处理):
```python
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
```
3. 对目标变量进行二分类转换(这里通过中位数划分0和1):
```python
median = np.median(y)
y_binary = [1 if value > median else 0 for value in y]
```
4. 划分特征(X)和标签(y_binary)到训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2, random_state=42)
```
5. 数据标准化(为了提高模型性能):
```python
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
```
6. 创建逻辑回归模型并拟合数据:
```python
model = LogisticRegression()
model.fit(X_train_scaled, y_train)
```
7. 预测新数据:
```python
predictions = model.predict(X_test_scaled)
```
8. 评估模型性能(如准确率,精确率,召回率等):
```python
from sklearn.metrics import accuracy_score, precision_score, recall_score
print("Accuracy:", accuracy_score(y_test, predictions))
print("Precision:", precision_score(y_test, predictions))
print("Recall:", recall_score(y_test, predictions))
```
阅读全文