# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] colab_type="text" id="vZe8oEJj2EZR"
#

# + colab={} colab_type="code" id="6Ks9_kkrCIQi"
import numpy as np
import pandas as pd

# +
# load data

dataX=pd.read_csv("trainX.csv")
dataA=pd.read_csv("trainA.csv")
dataY=pd.read_csv("trainY.csv")

# remove index

dataX=dataX.values[:,1:]
dataY=dataY.values[:,1:-1]
dataA=dataA.values[:,1:]

# + colab={} colab_type="code" id="F77bYonkF8AE"
# split cure or not cure
치료한환자데이터=pd.read_csv("2번 열을 제거한 치료한 환자 데이터.csv")
치료하지않은환자데이터=pd.read_csv("8번 열을 제거한 치료하지 않은 환자 데이터.csv")
# -

치료한환자데이터

치료하지않은환자데이터

data_CY=치료한환자데이터['time']
dataNCY=치료하지않은환자데이터['time']
data_CX = 치료한환자데이터.loc[:,['X0','X1','X3','X4','X5','X6','X7','X8','X9','X10','X11','X12','X13','X14','X15','X16']]
dataNCX = 치료하지않은환자데이터.loc[:,['X0','X1','X2','X3','X4','X5','X6','X7','X9','X10','X11','X12','X13','X14','X15','X16']]

data_CX

dataNCX

# + colab={} colab_type="code" id="8bTfqtUGPvsq"
from keras import models
from keras import layers
import tensorflow as tf


# +
def build_dnn_swish_adam():
  # 2. 모델의 구조를 BatchNormalization layer를 사용하여 만든다.
  X = tf.keras.layers.Input(shape=[16])
  
  H = tf.keras.layers.Dense(2048)(X)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 1
  
  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 2
  
  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 3

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 4

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 5

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 6

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 7
    
  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 8

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 9

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 10

  H = tf.keras.layers.Dense(2048)(X)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 11
  
  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 12
  
  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 13

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 14

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 15

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 16

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 17
    
  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 18

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 19

  H = tf.keras.layers.Dense(2048)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H) # Hidden layer 20
  
  Y = tf.keras.layers.Dense(1)(H)
  model = tf.keras.models.Model(X, Y)
  model.compile(loss='mse', optimizer='adam', metrics=['mean_absolute_error'])
  return model

def build_model_NC_100_stepper():
  # 2. 모델의 구조를 BatchNormalization layer를 사용하여 만든다.
  X = tf.keras.layers.Input(shape=[16])
  
  H = tf.keras.layers.Dense(2048)(X)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H)
  
  H = tf.keras.layers.Dense(1024)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H)
  
  H = tf.keras.layers.Dense(512)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H)

  H = tf.keras.layers.Dense(256)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H)

  H = tf.keras.layers.Dense(128)(H)
  H = tf.keras.layers.BatchNormalization()(H)
  H = tf.keras.layers.Activation('swish')(H)
  
  Y = tf.keras.layers.Dense(1)(H)
  model = tf.keras.models.Model(X, Y)
  model.compile(loss='mse', optimizer='adam', metrics=['mean_absolute_error'])
  return model


# -

# 치료 데이터
num_epochs = 10000
all_mae_histories_cured = []
k = 4
num_val_samples = len(data_CX) // k
for i in range(k):
    print('처리중인 폴드 #', i)
    # 검증 데이터 준비: k번째 분할
    val_data = data_CX[i * num_val_samples: (i + 1) * num_val_samples]
    val_targets = data_CY[i * num_val_samples: (i + 1) * num_val_samples]

    # 훈련 데이터 준비: 다른 분할 전체
    partial_train_data = np.concatenate(
        [data_CX[:i * num_val_samples],
         data_CX[(i + 1) * num_val_samples:]],
        axis=0)
    partial_train_targets = np.concatenate(
        [data_CY[:i * num_val_samples],
         data_CY[(i + 1) * num_val_samples:]],
        axis=0)

    # 케라스 모델 구성(컴파일 포함)
    model = build_dnn_swish_adam()
    # 모델 훈련(verbose=0 이므로 훈련 과정이 출력되지 않습니다)
    history = model.fit(partial_train_data, partial_train_targets,
                        validation_data=(val_data, val_targets),
                        epochs=num_epochs, batch_size=1, verbose=0)
    mae_history_cured = history.history['val_mean_absolute_error']
    all_mae_histories_cured.append(mae_history_cured)

# 치료하지 않은 데이터
num_epochs = 10000
all_mae_histories_non_cured = []
k = 4
num_val_samples = len(dataNCX) // k
for i in range(k):
    print('처리중인 폴드 #', i)
    # 검증 데이터 준비: k번째 분할
    val_data = dataNCX[i * num_val_samples: (i + 1) * num_val_samples]
    val_targets = dataNCY[i * num_val_samples: (i + 1) * num_val_samples]

    # 훈련 데이터 준비: 다른 분할 전체
    partial_train_data = np.concatenate(
        [dataNCX[:i * num_val_samples],
         dataNCX[(i + 1) * num_val_samples:]],
        axis=0)
    partial_train_targets = np.concatenate(
        [dataNCY[:i * num_val_samples],
         dataNCY[(i + 1) * num_val_samples:]],
        axis=0)

    # 케라스 모델 구성(컴파일 포함)
    model = build_dnn_swish_adam()
    # 모델 훈련(verbose=0 이므로 훈련 과정이 출력되지 않습니다)
    history = model.fit(partial_train_data, partial_train_targets,
                        validation_data=(val_data, val_targets),
                        epochs=num_epochs, batch_size=1, verbose=0)
    mae_history_non_cured = history.history['val_mean_absolute_error']
    all_mae_histories_non_cured.append(mae_history_non_cured)

# 치료 의사 결정 모델과 비치료 의사 결정 모델의 평균 MAE 값의 히스토리를 구한다.
average_mae_history_cured = [
    np.mean([x[i] for x in all_mae_histories_cured]) for i in range(num_epochs)]
average_mae_history_non_cured = [
    np.mean([x[i] for x in all_mae_histories_non_cured]) for i in range(num_epochs)]

import matplotlib.pyplot as plt

# 치료 의사 결정 모델 예상 생존 시간을 그래프로 출력한다.
plt.plot(range(1, len(average_mae_history_cured) + 1), average_mae_history_cured)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()

# 치료하지 않는 의사 결정 모델 예상 생존 시간을 그래프로 출력한다.
plt.plot(range(1, len(average_mae_history_non_cured) + 1), average_mae_history_non_cured)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()


def smooth_curve(points, factor=0.9):
  smoothed_points = []
  for point in points:
    if smoothed_points:
      previous = smoothed_points[-1]
      smoothed_points.append(previous * factor + point * (1 - factor))
    else:
      smoothed_points.append(point)
  return smoothed_points



# +
smooth_mae_history_cured = smooth_curve(average_mae_history_cured[10:])

plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history_cured)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()

# +
smooth_mae_history_non_cured = smooth_curve(average_mae_history_cured[10:])

plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history_non_cured)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()

# + colab={} colab_type="code" id="0oeHzl4DXr4P"
# 3.데이터로 모델을 학습(FIT)합니다.
model = build_dnn_swish_adam() # 최저 loss 값 : 0.0492
model.fit(data_CX, data_CY, epochs=30000, batch_size=16, verbose=0)
model.fit(data_CX, data_CY, epochs=1, batch_size=16, verbose=1)

# + colab={"base_uri": "https://localhost:8080/", "height": 386} colab_type="code" id="kzsyJB09b3t6" outputId="801dfd98-ec7d-4f5d-97cb-fefbf54ef5b4"
# 3.치료하지 않았고 생존시간이 100 이하인 환자 데이터로 모델을 학습(FIT)합니다.
model_NC = build_dnn_swish_adam() # 최저 loss 값 : 0.0159
model_NC.fit(dataNCX, dataNCY, epochs=30000, batch_size=16,verbose=0)
model.fit(dataNCX, dataNCY, epochs=1, batch_size=16, verbose=1)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="B6OBWoPGHuWw" outputId="a53a20ec-daaa-497f-d427-9bde00b08329"
치료_환자_파일경로 = '2번 열을 제거한 치료한 환자 테스트 데이터.csv'
비치료_환자_파일경로 = '8번 열을 제거한 치료하지 않은 환자 테스트 데이터.csv'
치료_환자_데이터 = pd.read_csv(치료_환자_파일경로)
비치료_환자_데이터 = pd.read_csv(비치료_환자_파일경로)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="iEzT2ja2IWrd" outputId="699e3367-7e90-4bc5-c92a-3c1339be05d0"
# 치료할 경우 생존 시간을 구한다.
cured = model.predict(치료_환자_데이터)
# 치료하지 않을 경우 생존 시간을 구한다.
non_cured = model_NC.predict(비치료_환자_데이터)

# 치료한 경우 생존 시간 > 치료하지 않은 경우 생존 시간 : 1, 
# 치료한 경우 생존 시간 > 치료하지 않은 경우 생존 시간 : 0
result = np.where(cured > non_cured, 1, 0)
print(result)

# + colab={} colab_type="code" id="xdWBw8y0NZMB"
import csv 

# 이차원 리스트 
with open('result_20200902_2.csv','w', newline='') as f: 
  makewrite = csv.writer(f) 
  
  for value in result: 
    makewrite.writerow(value)


# + colab={"base_uri": "https://localhost:8080/", "height": 88} colab_type="code" id="TQ9WCCfehqVH" outputId="909e27c3-fb09-4021-e84b-69715a0c87c0"
"""
from openpyxl import Workbook
 
write_wb = Workbook()
 
#이름이 있는 시트를 생성
#write_ws = write_wb.create_sheet('생성시트')
 
#Sheet1에다 입력
write_ws = write_wb.active
write_ws['A1'] = 'Title'
write_ws['B1'] = 'action'
 
#행 단위로 추가
# write_ws.append([1,2,3])
 
#셀 단위로 추가
i = 1
for value in result:
  write_ws.cell(i,2, value)
  i= i+1
write_wb.save('/content/drive/My Drive/traindata/result_20200824_3.csv')
"""

# + colab={} colab_type="code" id="RAlZ120_AXbj"


StarcoderdataJupyterScriptsDedupFiltered