bayes算法对数据diabetes = pd.read_csv('diabetes.csv')进行糖尿病预测
时间: 2023-08-11 16:08:49 浏览: 118
TC.zip_20news_similarr77_tc运行py_文本分类
对于使用贝叶斯算法进行糖尿病预测,你可以按照以下步骤进行操作:
1. 导入必要的库:
```python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
```
2. 加载数据集:
```python
diabetes = pd.read_csv('diabetes.csv')
```
3. 准备数据:
将数据集划分为特征(X)和目标变量(y):
```python
X = diabetes.drop('Outcome', axis=1)
y = diabetes['Outcome']
```
4. 划分训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
5. 创建并训练贝叶斯分类器模型:
```python
model = GaussianNB()
model.fit(X_train, y_train)
```
6. 预测测试集结果:
```python
y_pred = model.predict(X_test)
```
7. 评估模型性能:
```python
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
这样,你就可以使用贝叶斯算法对糖尿病数据进行预测,并评估模型的准确性。请确保已经安装了所需的库,并且数据文件 'diabetes.csv' 在同一目录下。
阅读全文