[Practical Project] Python implements deep neural network RNN-LSTM classification model (medical disease diagnosis)

Note: This is a practical machine learning project (with data + code + video + documents ). If you need data + complete code, you can go directly to the end of the article.

1.Project background

       With the continuous deepening of Internet+, we have entered the era of artificial intelligence. As a branch of artificial intelligence, machine learning is increasingly being used in all walks of life, and it has also received more and more application in clinical medical testing. application. Based on the increasing amount of test data in clinical medicine, building a machine learning model to make more intelligent predictions has become the mission of today's era. This model is also based on some historical disease data for modeling and prediction.

2. Collect data

This data is simulated data and is divided into two parts of data:

Training data set: data.csv

Test data set: test.csv

In actual applications, you can just replace it according to your own data.

特征数据:age、gender、body_mass_index、heart_failure hypertension、       chronic_obstructic_pulmonary_disease、       chronic_liver_disease、……renal_toxic_drug        

Tag data: acute_kidney_disease

3. Data preprocessing

1) Original data description :

2) Data integrity and data type viewing:

3) Number of missing values ​​in data:

  

You can see that there are no missing values ​​in the data.

4. Exploratory data analysis

1) Display the distribution of age features:

2) Display the distribution of gender characteristics:

3) Display the distribution of heart_failure features:

The remaining features can be analyzed by yourself.

4) Correlation analysis

  

Note: A positive value indicates a positive correlation, and a negative value indicates a negative correlation. The larger the value, the stronger the correlation between variables.

5. Feature engineering

1) Feature data and label data are split, acute_kidney_disease is label data, and data other than acute_kidney_disease is feature data;

2) Split the data set into a training set and a trial set

The data set has been divided in advance and can be read directly.

6.LSTM modeling  

1) A brief introduction to neural network LSTM:

The LSTM network is a variant of RNN and is currently a more common recurrent neural network structure. The whole process is Long Short-Term Memory, which is translated into Chinese as a "long 'short memory'" network. When reading, there should be a slight pause after "long", and do not read it as "long or short" memory network, because in that case, you will not know whether the memory is long or short. In essence, it is still a short memory network, but it uses a certain method to extend the "short memory" as much as possible.

In short, LSTM is a recurrent neural network carrying a memory track, which is an improvement specially made to solve the problem of gradient disappearance. The memory track it adds is a way to carry information across multiple time steps. One can first imagine a conveyor belt parallel to the time series processing process. The information in the sequence can "jump" on the conveyor belt at any position, and then be transmitted to a later time step, and "jump" over intact when needed , accept processing. This is the principle of LSTM: just like the memory memory in the brain, information is saved for later use. When we recall the past, earlier information will come to mind again, and will not disappear without a trace as time goes by.

This idea is very similar to the residual connection. The difference is that the residual connection solves the problem of gradient disappearance between layers, while LSTM solves the problem of message disappearance during loop processing within the loop layer and neuron layer.

Simply put, the C track will carry information across time steps. Its value at different time steps is Ct. This information will be operated with the input connection and the loop connection (ie, dot product with the weight matrix, then add a bias, and add an activation process), thereby affecting the transmission to the next The status of a time step is shown in the figure on the right.

LSTM - adds a memory track that carries information earlier in the sequence

2) Establish an LSTM classification model with the following model parameters:

serial number

parameter

1

loss='binary_crossentropy'

2

optimizer='adam'

3

metrics=['acc']

Other parameters are set based on specific data.

3) Neural network structure and summary

Neural network structure diagram:

Neural Network Summary:

You can see the type, shape and parameters of each layer of the network.

7. Model evaluation

1) The evaluation indicators mainly use precision rate, recall rate, F1

serial number

Evaluation indicator name

Evaluation indicator value

1

precision

98.74%

2

recall rate

100.00%

3

F1

99.37%

As can be seen from the above table, this model works well.

2)Loss and accuracy graph

 loss = history.history['loss']
    val_loss = history.history['val_loss']
    epochs = range(1, len(loss) + 1)
    plt.figure(figsize=(12, 4))
    plt.subplot(1, 2, 1)
    plt.plot(epochs, loss, 'r', label='Training loss')
    plt.plot(epochs, val_loss, 'b', label='Test loss')
    plt.title('Training and Test loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    plt.subplot(1, 2, 2)
    plt.plot(epochs, acc, 'r', label='Training acc')
    plt.plot(epochs, val_acc, 'b', label='Test acc')
    plt.title('Training and Test accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend()

3) ROC curve drawing

Training set ROC curve chart:

fpr, tpr, threshold = roc_curve(y_data, y_score)

    roc_auc = auc(fpr, tpr)

    plt.figure()
    lw = 2
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title(title + ' RNN-LSTM Model ')
    plt.legend(loc="lower right")

Test set ROC curve chart:

8.Clinical application

Based on the characteristic data of the test set, predict whether these patients will have related diseases; based on the prediction results: prevent people who may suffer from such diseases in the future.

The prediction results are as follows:

 

features = ['age']
fig = plt.subplots(figsize=(15, 15))
for i, j in enumerate(features):
    plt.subplots_adjust(hspace=1.0)
    sns.countplot(x=j, data=data_train)
    plt.title("No. of age")

# 本次机器学习项目实战所需的资料,项目资源如下:

# 链接:https://pan.baidu.com/s/1dW3S1a6KGdUHK90W-lmA4w 
# 提取码:bcbp

fig = plt.subplots(figsize=(15, 15))
for i, j in enumerate(features):
    plt.subplots_adjust(hspace=1.0)
    sns.countplot(x=j, data=data_train)
    plt.title("No. of gender")

Guess you like

Origin blog.csdn.net/weixin_42163563/article/details/119767919#comments_28565663