<jupyter_start><jupyter_text>DNN<jupyter_code>import os
import numpy as np
import keras as K
import tensorflow as tf
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
os.environ['TF_CPP_MIN_LOG_LEVEL']= '2'
target_var = 'Cancer'
features = list(df_final_select_train.columns)
# 目標變量的類別
Class = train[target_var].unique()
# 目標變量的類別字典
Class_dict = dict(zip(Class, range(len(Class))))
# 增加一列target, 將目標變量進行編碼
train['target'] = train[target_var].apply(lambda x: Class_dict[x])
# 對目標變量進行0-1編碼(One-hot Encoding)
lb = LabelBinarizer()
lb.fit(list(Class_dict.values()))
transformed_labels = lb.transform(train['target'])
label2 = np.zeros((291,1))

for i in range(len(transformed_labels)):
  if transformed_labels[i][0] == 0:
    label2[i][0] = np.int(1)
  elif transformed_labels[i][0] == 1:
    label2[i][0] = np.int(0)

transformed_labels = np.concatenate((transformed_labels, label2),axis=1)

y_bin_labels_train = []  # 對多分類進行0-1編碼的變量
for i in range(transformed_labels.shape[1]):
    y_bin_labels_train.append( 'y' + str(i))
    train['y' + str(i)] = transformed_labels[:, i]
target_var = 'Cancer'
features = list(df_final_select_test.columns)
# 目標變量的類別
Class = test[target_var].unique()
# 目標變量的類別字典
Class_dict = dict(zip(Class, range(len(Class))))
# 增加一列target, 將目標變量進行編碼
test['target'] = test[target_var].apply(lambda x: Class_dict[x])
# 對目標變量進行0-1編碼(One-hot Encoding)
lb = LabelBinarizer()
lb.fit(list(Class_dict.values()))
transformed_labels = []
transformed_labels = lb.transform(test['target'])
label3 = np.zeros((123,1))

for i in range(len(transformed_labels)):
  if transformed_labels[i][0] == 0:
    label3[i][0] = np.int(1)
  elif transformed_labels[i][0] == 1:
    label3[i][0] = np.int(0)

transformed_labels = np.concatenate((transformed_labels, label3),axis=1)
y_bin_labels_test = []  # 對多分類進行0-1編碼的變量
for i in range(transformed_labels.shape[1]):
    y_bin_labels_test.append( 'y' + str(i))
    test['y' + str(i)] = transformed_labels[:, i]
# 將數據集分為訓練集和測試集
train_x = df_final_select_train[features]
test_x = df_final_select_test[features]
train_y = train[y_bin_labels_train]
test_y = test[y_bin_labels_test]
np.random.seed(5)
# model
init = K.initializers.glorot_uniform(seed=1)
model = K.models.Sequential()
model.add(K.layers.Dense(units=200, input_dim=707, kernel_initializer=init, activation='relu'))
# model.add(K.layers.Dense(units=2048, kernel_initializer=init, activation= 'relu'))
# model.add(K.layers.Dense(units=1024, kernel_initializer=init, activation= 'relu'))
model.add(K.layers.Dense(units=2, kernel_initializer=init, activation='relu'))
# 訓練模型
opt = tf.keras.optimizers.Adam(learning_rate=0.0005)
model.compile(optimizer=opt, loss='categorical_crossentropy',
        metrics=['accuracy'])

h = model.fit(train_x, train_y, epochs=50, batch_size=4,
      validation_data=(test_x, test_y))
# summarize history for accuracy
plt.plot(h.history['accuracy'])
plt.plot(h.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>PH-CNNreference:https://github.com/dfioravanti/phylogenetic-cnnhttps://www.tensorflow.org/tutorials/images/cnn<jupyter_code>import os
from tensorflow.keras import datasets, layers, models<jupyter_output><empty_output><jupyter_text>Import coordinate data<jupyter_code>!gdown --id '1E97dcVIaeaRQB7O0MpENzg33AwLOdM7y' --output ordinate_train.csv
C_train = pd.read_csv("ordinate_train.csv")
C_train.head()
not_select=['index', 'Merged']
C_final_train = C_train.drop(not_select,axis=1)
C_final_train.info()
C_final_train.head()
!gdown --id '1myMxkf61hmEYglDUO9K_Qm9NI2Utsc0J' --output ordinate_test.csv
C_test = pd.read_csv("ordinate_test.csv")
C_test.head()
not_select=['index', 'Merged']
C_final_test = C_test.drop(not_select,axis=1)
C_final_test.info()
C_final_test.head()
X_final_train = pd.concat([df_final_select_train, C_final_train], axis=1)
X_final_test = pd.concat([df_final_select_test, C_final_test], axis=1)
print(X_final_train.shape)
print(X_final_test.shape)
target_var = 'Cancer'
features = list(X_final_train.columns)
# 目標變量的類別
Class = train[target_var].unique()
# 目標變量的類別字典
Class_dict = dict(zip(Class, range(len(Class))))
# 增加一列target, 將目標變量進行編碼
train['target'] = train[target_var].apply(lambda x: Class_dict[x])
# 對目標變量進行0-1編碼(One-hot Encoding)
lb = LabelBinarizer()
lb.fit(list(Class_dict.values()))
transformed_labels = lb.transform(train['target'])
label2 = np.zeros((291,1))

for i in range(len(transformed_labels)):
  if transformed_labels[i][0] == 0:
    label2[i][0] = np.int(1)
  elif transformed_labels[i][0] == 1:
    label2[i][0] = np.int(0)

transformed_labels = np.concatenate((transformed_labels, label2),axis=1)
y_bin_labels_train = []  # 對多分類進行0-1編碼的變量
for i in range(transformed_labels.shape[1]):
    y_bin_labels_train.append( 'y' + str(i))
    train['y' + str(i)] = transformed_labels[:, i]
target_var = 'Cancer'
features = list(X_final_test.columns)
# 目標變量的類別
Class = test[target_var].unique()
# 目標變量的類別字典
Class_dict = dict(zip(Class, range(len(Class))))
# 增加一列target, 將目標變量進行編碼
test['target'] = test[target_var].apply(lambda x: Class_dict[x])
# 對目標變量進行0-1編碼(One-hot Encoding)
lb = LabelBinarizer()
lb.fit(list(Class_dict.values()))
transformed_labels = lb.transform(test['target'])
label2 = np.zeros((123,1))

for i in range(len(transformed_labels)):
  if transformed_labels[i][0] == 0:
    label2[i][0] = np.int(1)
  elif transformed_labels[i][0] == 1:
    label2[i][0] = np.int(0)

transformed_labels = np.concatenate((transformed_labels, label2),axis=1)
y_bin_labels_test = []  # 對多分類進行0-1編碼的變量
for i in range(transformed_labels.shape[1]):
    y_bin_labels_test.append( 'y' + str(i))
    test['y' + str(i)] = transformed_labels[:, i]
# 將數據集分為訓練集和測試集
train_x = X_final_train[features]
test_x = X_final_test[features]
train_y = train[y_bin_labels_train]
test_y = test[y_bin_labels_test]
train_x = train_x.values.reshape(291, 3, 3, 93)
train_x = train_x.reshape(291, 3, 3, 93)
test_x = test_x.values.reshape(123, 3, 3, 93)
test_x = test_x.reshape(123, 3, 3, 93)<jupyter_output><empty_output><jupyter_text>Model(Same as paper)<jupyter_code>model = K.models.Sequential()
model.add(K.layers.Conv2D(93, (1, 1), activation='relu', input_shape=train_x.shape[1:]))
model.add(K.layers.Conv2D(50, (1, 1), activation='relu', input_shape=train_x.shape[1:]))
model.add(K.layers.MaxPooling2D((2, 2)))
model.add(K.layers.Flatten())
model.add(K.layers.Dense(64, activation= 'relu'))
model.add(K.layers.Dropout(0.25))
model.add(K.layers.Dense(2,activation='sigmoid'))
model.summary()
opt = tf.keras.optimizers.Adam(learning_rate=0.0005)
model.compile(optimizer=opt, loss='categorical_crossentropy',
        metrics=['accuracy'])

h2 = model.fit(train_x, train_y, epochs=50, batch_size=4,
      validation_data=(test_x, test_y))
# summarize history for accuracy
plt.plot(h2.history['accuracy'])
plt.plot(h2.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Model 2Activation = relu  No Dropout<jupyter_code>train_x = train_x.reshape(291, 3, 3, 93)
test_x = test_x.reshape(123, 3, 3, 93)
model = K.models.Sequential()
model.add(K.layers.Conv2D(93, (3, 3), activation='relu', input_shape=train_x.shape[1:]))
model.add(K.layers.Conv2D(20, (1, 1), activation='relu', input_shape=train_x.shape[1:]))
# model.add(K.layers.MaxPooling2D((2, 2)))
model.add(K.layers.Flatten())
model.add(K.layers.Dense(32, activation= 'relu'))
model.add(K.layers.Dropout(0.15))
model.add(K.layers.Dense(2,activation='relu'))
model.summary()
opt = tf.keras.optimizers.Adam(learning_rate=0.0003, decay=1e-3)
model.compile(optimizer=opt, loss='categorical_crossentropy',
        metrics=['accuracy'])

h2 = model.fit(train_x, train_y, epochs=50, batch_size=4,
      validation_data=(test_x, test_y))
# summarize history for accuracy
plt.plot(h2.history['accuracy'])
plt.plot(h2.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16487108098293185, 'fraction_numerical': 0.02929427430093209, 'mean_word_length': 7.253746253746254, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '386124', 'license': 'MIT', 'n_tokens_mistral': 3523, 'n_tokens_neox': 3483, 'n_words': 497, 'path': 'Gastric_Cancer/Qiime2_Taxonomy_GastricCancer_0818_PhCNN_train291.ipynb', 'repo_name': 'ehjhihlo/Sinica_Project', 'size': '8261'}