تشخیص بیماران دیابتی با پایتون

با سلام به همه دوستان و عزیزانم، در این بخش می خوایم یک الگوریتم طراحی کنیم که در تشخیص و پیش بینی بیماران دیابتی استفاده میشه. در واقع این الگوریتم به این صورت عمل میکنه که اطلاعات اولیه از بیماران دیابتی دریافت می کنه و از روی اون دیابتی بودن یا نبودن افراد رو تشخیص میده.

خب برنامه ای که ما برای الگوریتم نویسی استفاده می کنیم Google Colab هست چون یک ابزار آماده برای کدنویسی پایتون حساب میشه و نیاز مارو از نصب کتابخونه ها برطرف میکنه و موتور ران مصتل به گوگل اون هم باعث شده سرعت پردازش نسبتا خوب باشه

بعد از اینکه وارد محیط گوگل کولب شدید یک New Notebook ایجاد کنید تا بتونید کدتونو اونجا بنویسید!

برای راحتی کار ما لینک دسترسی به این کد رو در اینجا میزاریم تا بتونید کد رو ببینید:

 

اما این کد دارای چندین بلوک می باشد که به صورت زیر میشه اون رو تفکیک کرد:

۱- فراخوانی کتابخانه ها:

 

#Load libraries
from keras.models import Sequential 
from keras.layers import Dense 
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

۲- لود کردن و بارگذاری دیتاست:

 

#Load the data
from google.colab import files
uploaded = files.upload()

۳- ذخیره دیتاست و پرینت یا نمایش ۷ سطر اول آن:

 

#Store the data set
df = pd.read_csv('diabetes.csv')

#Print the first 7 rows of data
df.head(7)

۴- نمایش ساختار دیتا ست (یعنی تعداد سطر و ستون ها):

 

#Show te shape ( number of rows and columns)
df.shape

۵- بررسی موارد تکراری در دیتاست و حذف آن ها:

 

#Check for duplicates and remove them
df.drop_duplicates(inplace = True)

۶- مجددا نمایش ساختار و تعداد سطر و ستون ها (که ببینیم آیا موارد تکراری بوده یا نه):

 

#Show the shape ( number of rows & columns)
df.shape

۷- نمایش تعداد دیتاهایی که در هر ستون وجود ندارند یا مقدار دهی نشده اند:

 

#Show the number of missing data for each column
df.isnull().sum()

۸- تبدیل دیتاست به یک آرایه به منظور پردازش بهتر دیتا:

 

#Convert the data into an array
dataset = df.values
dataset

۹- دریافت تمام سطرها از ۸ ستون اول دیتاست:

 

#Get all of the rows from the first eight columns of the data set
X = dataset[:, 0:8]
y = dataset[:,8]

۱۰- پردازش دیتاست:

 

#Process the data
from sklearn import preprocessing 
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
X_scale

۱۱- تبدیل دیتاست به دو بخش آموزش (۸۰ درصد) و تست (۲۰ درصد):

 

#Split the data into 80% training and 20% testing
X_train, X_test, y_train, y_test = train_test_split(X_scale, y, test_size=0.2, random_state=4)

۱۲- ساخت مدل:

 

#Build the model

model = Sequential([
    Dense(12, activation='relu', input_shape=(8,)),
    Dense(15, activation='relu'),
    Dense(1, activation='sigmoid')                 
])

۱۳- کامپایل کردن مدل:

 

#Compile the model
model.compile(
    optimizer= 'sgd',
      loss = 'binary_crossentropy',
      metrics=['accuracy']    
)

۱۴- آموزش مدل:

 

#Train the model

hist = model.fit( X_train, y_train, batch_size = 57, epochs=1000, validation_split=0.2)

۱۵- نمایش نمودار تلفات یا خطای مدل و صحت سنجی آن:

 

#Visualize the training loss and the validation loss to see if the model is overfitting
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc = 'upper right')
plt.show()

۱۶- نمایش نمودار دقت مدل و صحت سنجی آن:

 

#Visualize the training loss and the validation loss to see if the model is overfitting
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc = 'lower right')
plt.show()

۱۷- انجام یک پیش بینی:

 

#Make a prediction & print the actual values
prediction = model.predict(X_test)
prediction = [1 if y>=0.5 else 0 for y in prediction]
print(prediction)
print(y_test)

۱۸- اجرای مدل برروی دیتاست آموزش:

 

#Evaluate the model on the training data set
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

pred = model.predict(X_train)
pred = [1 if y>= 0.5 else 0 for y in pred]
print(classification_report(y_train, pred))
print('Confusion Matrix: \n', confusion_matrix(y_train, pred))
print()
print('Accuracy: ', accuracy_score(y_train, pred))

۱۹- اجرای مدل بر روی دیتاست تست:

 

#Evaluate the model on the test data set
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

pred = model.predict(X_test)
pred = [1 if y>= 0.5 else 0 for y in pred]
print(classification_report(y_test, pred))
print('Confusion Matrix: \n', confusion_matrix(y_test, pred))
print()
print('Accuracy: ', accuracy_score(y_test, pred))

 

[/vc_column_text][/vc_column][/vc_row]

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

قبلا حساب کاربری ایجاد کرده اید؟
گذرواژه خود را فراموش کرده اید؟
Loading...