In [ ]:
import pandas as pd
from sklearn.model_selection import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
In [ ]:
df= pd.read_csv('diabetes.csv')
df.head()
Out[ ]:
| age | gender | sudden_weight_loss | genital_thrush | visual blurring | itching | irritability | delayed_healing | alopecia | obesity | result | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 40 | Male | No | No | No | Yes | No | Yes | Yes | Yes | Positive |
| 1 | 58 | Male | No | No | Yes | No | No | No | Yes | No | Positive |
| 2 | 41 | Male | No | No | No | Yes | No | Yes | Yes | No | Positive |
| 3 | 45 | Male | Yes | Yes | No | Yes | No | Yes | No | No | Positive |
| 4 | 60 | Male | Yes | No | Yes | Yes | Yes | Yes | Yes | Yes | Positive |
In [ ]:
len(df)
Out[ ]:
520
In [ ]:
#distribution of positive and negative
df['result'].value_counts()
Out[ ]:
| count | |
|---|---|
| result | |
| Positive | 320 |
| Negative | 200 |
In [ ]:
sns.catplot(data=df,x='result',y='age', kind='box', hue='result')
plt.show()
In [ ]:
#construct the features and label
features= df.iloc[:, 0:10]
features.head()
Out[ ]:
| age | gender | sudden_weight_loss | genital_thrush | visual blurring | itching | irritability | delayed_healing | alopecia | obesity | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 40 | Male | No | No | No | Yes | No | Yes | Yes | Yes |
| 1 | 58 | Male | No | No | Yes | No | No | No | Yes | No |
| 2 | 41 | Male | No | No | No | Yes | No | Yes | Yes | No |
| 3 | 45 | Male | Yes | Yes | No | Yes | No | Yes | No | No |
| 4 | 60 | Male | Yes | No | Yes | Yes | Yes | Yes | Yes | Yes |
In [ ]:
label= df.iloc[:, -1]
label.head()
Out[ ]:
| result | |
|---|---|
| 0 | Positive |
| 1 | Positive |
| 2 | Positive |
| 3 | Positive |
| 4 | Positive |
In [ ]:
# dummy variables
dm_feature= pd.get_dummies(data=features, drop_first=True, dtype=float)
dm_feature.head()
Out[ ]:
| age | gender_Male | sudden_weight_loss_Yes | genital_thrush_Yes | visual blurring_Yes | itching_Yes | irritability_Yes | delayed_healing_Yes | alopecia_Yes | obesity_Yes | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 40 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 |
| 1 | 58 | 1.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
| 2 | 41 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 |
| 3 | 45 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 0.0 |
| 4 | 60 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
In [ ]:
#training and testing set , testing 20%
train_x, test_x, train_y, test_y= train_test_split(dm_feature, label, test_size=0.2, random_state=42)
In [ ]:
#define naive bayes model
NB_model= MultinomialNB()
NB_model.fit(train_x, train_y)
Out[ ]:
MultinomialNB()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MultinomialNB()
In [ ]:
pred_y= NB_model.predict(test_x)
print (pred_y[0:5]) #just looking at first 5 results
['Negative' 'Positive' 'Negative' 'Positive' 'Positive']
In [ ]:
print(test_y[0:5]) #just looking at first 5 results
275 Negative 93 Positive 6 Positive 167 Positive 90 Positive Name: result, dtype: object
In [ ]:
#accuracy
accuracy_score = accuracy_score(test_y, pred_y)
accuracy_score
Out[ ]:
0.7884615384615384
In [ ]: