<jupyter_start><jupyter_text>Importação das bibliotecas<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
import seaborn as sns
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn import metrics
from sklearn.model_selection import train_test_split
from tensorflow import keras
import tensorflow as tf
from Bio import SeqIO
tf.get_logger().setLevel('WARNING')<jupyter_output><empty_output><jupyter_text>Carregando os dados<jupyter_code>Train_Data = pd.read_csv("Dados/heart.csv")
Train_Data<jupyter_output><empty_output><jupyter_text>Descrição das colunas 1. __Age__ is the age of candidate.2. __Sex__ has numeric values. 1 denotes male and 0 denotes female.3. __Chest Pain__ pain has values between 0-3. The types of angina that are described in the research paper. The higher the number, the lesser are the odds of heart attack.4. __Resting blood pressure__ is normal pressure with no exercise.5. __Cholesterol__ means the blockage for blood supply in the blood vessels.6. __Fasting Blood Pressure__ is blood sugar taken after a long gap between a meal and the test. Typically, it's taken before any meal in the morning. 1 denotes > 120 mg/dl 7. __Rest ECG (electrocardiographic) results__ means ECG values taken while person is on rest which means no exercise and normal functioning of heart is happening. Values 0,1,2.8. __The Maximum Heart Rate achieved__.9. __Exercise induced angina__ is chest pain while exercising or doing any physical activity. 1 denotes presence and 0 denotes absence.10. __Oldpeak__ = ST Depression induced by exercise relative to rest. ST Depression is the difference between value of ECG at rest and after exercise.11. __ST Slope__ is the tangent to the depression value.12. __The number of major blood vessels (0-3) supplying blood to heart blocked__.13. __The Types of thalassemia__.14. __Heart attack prediction__ where 1 denotes Heart attack occured and 0 where it din't take occur.<jupyter_code>Train_Data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 303 entries, 0 to 302
Data columns (total 14 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   age       303 non-null    int64  
 1   sex       303 non-null    int64  
 2   cp        303 non-null    int64  
 3   trestbps  303 non-null    int64  
 4   chol      303 non-null    int64  
 5   fbs       303 non-null    int64  
 6   restecg   303 non-null    int64  
 7   thalach   303 non-null    int64  
 8   exang     303 non-null    int64  
 9   oldpeak   303 non-null    float64
 10  slope     303 non-null    int64  
 11  ca        303 non-null    int64  
 12  thal      303 non-null    int64  
 13  target    303 non-null    int64  
dtypes: float64(1), int64(13)
memory usage: 33.3 KB<jupyter_text>Checar se as classes estão balanceadas<jupyter_code>Train_Data['target'].value_counts()<jupyter_output><empty_output><jupyter_text>Checar se existem dados duplicados<jupyter_code>print("Duplicated values: ", Train_Data.duplicated().sum())
duplicate = Train_Data[Train_Data.duplicated()]
duplicate<jupyter_output>Duplicated values:  1<jupyter_text>Remover dados duplicados, deixando apenas a primeira ocorrência<jupyter_code>Train_Data.drop_duplicates(keep='first',inplace=True) 
Train_Data<jupyter_output><empty_output><jupyter_text>Checar se existem dados faltantes<jupyter_code>print("Missing values: ", Train_Data.isnull().sum().sum())<jupyter_output>Missing values:  0<jupyter_text>Checar se existem correlações entre os atributos e a saída<jupyter_code>corr = Train_Data.corr()
corr.style.background_gradient(cmap = 'coolwarm')<jupyter_output><empty_output><jupyter_text>Rede neural<jupyter_code>from sklearn.neural_network import MLPClassifier<jupyter_output><empty_output><jupyter_text>Dividir atributos e saída<jupyter_code>X=Train_Data[Train_Data.columns[Train_Data.columns!='target']]
X.head()
Y=Train_Data['target']<jupyter_output><empty_output><jupyter_text>Dividir treinamento e teste<jupyter_code>train_x, test_x, train_y,test_y=train_test_split(X,Y,random_state=0,test_size=0.2)
print(train_y.shape, test_y.shape)<jupyter_output>(241,) (61,)<jupyter_text>Rede neural multicamadas sklearn<jupyter_code>def MLP(train_x, train_y, test_x, test_y, n_neurons):
    reg = MLPClassifier(hidden_layer_sizes=(n_neurons,n_neurons,n_neurons), activation="relu" ,random_state=1, max_iter=200).fit(train_x, train_y)
    y_pred=reg.predict(test_x)

    model_accuracy = round(accuracy_score(test_y, y_pred),2)
    #confusion matrix
    cm = confusion_matrix(test_y, y_pred)
    return model_accuracy,cm, y_pred
accuracy,conf_marix,y_pred = MLP(train_x, train_y, test_x, test_y, 512)
print("Model Accuracy:" + str(accuracy) + "\n" + "Confusion Matrix:\n" + str(conf_marix))
def error_plot(train_x, train_y, test_x, test_y):

    error = []

    # Calculando o erro variando o número de neurônios, incrementando em base 2 (16,32,64,128,256 e 512)
    n_neurons = 16
    while n_neurons <=512:
        accuracy,conf_marix,y_pred = MLP(train_x, train_y, test_x ,test_y, n_neurons)
        error.append(np.mean(y_pred != test_y))
        n_neurons *= 2
        
    plt.figure(figsize=(12, 6))
    plt.plot(error, color='red', linestyle='dashed', marker='o',
             markerfacecolor='blue', markersize=10)
    plt.title('Error Rate Neurons Quantity')
    plt.xlabel('Neurons Quantity')
    plt.ylabel('Mean Error')
error_plot(train_x, train_y, test_x, test_y)<jupyter_output>/home/thais/anaconda3/lib/python3.7/site-packages/sklearn/neural_network/_multilayer_perceptron.py:585: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)<jupyter_text>Rede neural multicamadas keras<jupyter_code>def NN(train_x, train_y, test_x, test_y, n_neurons):
    
    tf.random.set_seed(2)
    
    NN_model = keras.Sequential([
    keras.layers.Dense(n_neurons, activation='relu', input_shape=train_x.shape),
    keras.layers.Dense(n_neurons, activation='relu'),
    keras.layers.Dense(n_neurons, activation='relu'),
    keras.layers.Dense(1, activation='sigmoid')
    ])
    
    NN_model.compile(
    loss='binary_crossentropy',
    optimizer='adamax',
    metrics=['accuracy']
    )
    
    
    train_history = NN_model.fit(
    train_x, train_y,
    validation_data=(test_x, test_y),
    batch_size = 8,
    shuffle=True,
    epochs = 30,
    verbose = 0
    )
    
    
    results = NN_model.evaluate(test_x, test_y)

    return results
accuracy = NN(train_x, train_y, test_x, test_y, 64)
print("\n\nModel Accuracy:" + str(accuracy[1]))
def accuracy_plot(train_x, train_y, test_x, test_y):

    accuracy = []

    # Calculando o erro variando o número de neurônios, incrementando em base 2 (16,32,64,128,256 e 512)
    n_neurons = 16
    while n_neurons <=512:
    
        results = NN(train_x, train_y, test_x, test_y, n_neurons)
        accuracy.append(np.mean(results[1]))
        n_neurons *= 2
        
        
    plt.figure(figsize=(12, 6))
    plt.plot(accuracy, color='red', linestyle='dashed', marker='o',
             markerfacecolor='blue', markersize=10)
    plt.title('Accuracy Rate Neurons Quantity')
    plt.xlabel('Neurons Quantity')
    plt.ylabel('Mean Accuracy')
accuracy_plot(train_x, train_y, test_x, test_y)<jupyter_output>WARNING:tensorflow:Model was constructed with shape (None, 241, 13) for input KerasTensor(type_spec=TensorSpec(shape=(None, 241, 13), dtype=tf.float32, name='dense_4_input'), name='dense_4_input', description="created by layer 'dense_4_input'"), but it was called on an input with incompatible shape (None, 13).
WARNING:tensorflow:Model was constructed with shape (None, 241, 13) for input KerasTensor(type_spec=TensorSpec(shape=(None, 241, 13), dtype=tf.float32, name='dense_4_input'), name='dense_4_input', description="created by layer 'dense_4_input'"), but it was called on an input with incompatible shape (None, 13).
WARNING:tensorflow:Model was constructed with shape (None, 241, 13) for input KerasTensor(type_spec=TensorSpec(shape=(None, 241, 13), dtype=tf.float32, name='dense_4_input'), name='dense_4_input', description="created by layer 'dense_4_input'"), but it was called on an input with incompatible shape (None, 13).
2/2 [==============================] - 0s 1ms/step - loss: 0.630[...]<jupyter_text>Normalizando os dados<jupyter_code>scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(train_x)
X_test_scaled = scaler.fit_transform(test_x)<jupyter_output><empty_output><jupyter_text>Visualizando normalização<jupyter_code>fig, ax = plt.subplots(ncols=3, figsize=(15,5))
sns.distplot(np.concatenate(X.values), ax=ax[0]).set_title('Original Data')
sns.distplot(np.concatenate(X_train_scaled), ax=ax[1]).set_title('Train Scaled Data')
sns.distplot(np.concatenate(X_test_scaled), ax=ax[2]).set_title('Test Scaled Data')
plt.tight_layout
plt.show()<jupyter_output><empty_output><jupyter_text>Redes neurais<jupyter_code>accuracy,conf_marix,y_pred = MLP(X_train_scaled, train_y, X_test_scaled ,test_y,64)
print("Model Accuracy:" + str(accuracy) + "\n" + "Confusion Matrix:\n" + str(conf_marix))
accuracy = NN(X_train_scaled, train_y, X_test_scaled ,test_y,64)
print("\n\nModel Accuracy:" + str(accuracy[1]))<jupyter_output>WARNING:tensorflow:Model was constructed with shape (None, 241, 13) for input KerasTensor(type_spec=TensorSpec(shape=(None, 241, 13), dtype=tf.float32, name='dense_28_input'), name='dense_28_input', description="created by layer 'dense_28_input'"), but it was called on an input with incompatible shape (None, 13).
WARNING:tensorflow:Model was constructed with shape (None, 241, 13) for input KerasTensor(type_spec=TensorSpec(shape=(None, 241, 13), dtype=tf.float32, name='dense_28_input'), name='dense_28_input', description="created by layer 'dense_28_input'"), but it was called on an input with incompatible shape (None, 13).
WARNING:tensorflow:Model was constructed with shape (None, 241, 13) for input KerasTensor(type_spec=TensorSpec(shape=(None, 241, 13), dtype=tf.float32, name='dense_28_input'), name='dense_28_input', description="created by layer 'dense_28_input'"), but it was called on an input with incompatible shape (None, 13).
2/2 [==============================] - 0s 2ms/step - lo[...]<jupyter_text>Usando arquivo Multi-Fasta ![Random Unsplash Image](https://www.researchgate.net/profile/Morteza-Hosseini-6/publication/309134977/figure/fig1/AS:417452136648705@1476539753111/A-sample-of-the-Multi-FASTA-file.png)<jupyter_code>with open('Dados/Crocodylus_porosus_train.fa') as fasta_file:
    identifiers = []
    sequences = []
    description = []
    for seq_record in SeqIO.parse(fasta_file, 'fasta'):
        identifiers.append(seq_record.id)
        sequences.append(seq_record.seq)
        description.append(seq_record.description)<jupyter_output><empty_output><jupyter_text>Usando contagem de tri-nucleotídeos<jupyter_code>def trinucleotides_counts(identifiers, sequences, description, out_file):
    
    output_file = open(out_file, 'w')
    mapa = {"A" : 0,"C" : 1,"T" : 2,"G" : 3}
    tri_nucleotides_counts =[[[0 for col in range(4)]for row in range(4)] for x in range(4)]
    toCompare = ["A","C","T","G"]
    
    for seq,ident,desc in zip(sequences,identifiers,description):
        for x in range(0,len(seq)-2,3):
            if(seq[x] in toCompare):
                first = mapa[seq[x]]
            if(seq[x+1] in toCompare):
                seccond = mapa[seq[x + 1]]
            if(seq[x+2] in toCompare):
                third = mapa[seq[x + 2]]
                tri_nucleotides_counts[first][seccond][third] += 1
                

        for i in range(0, 4):
            for j in range(0, 4):
                for l in range(0, 4):
                    if(i ==3 & j==3 & l==3):
                        if('ncrna' in desc):
                            output_file.writelines(str(tri_nucleotides_counts[i][j][l]) + ',' + "ncrna\n")
                        else:
                            output_file.writelines(str(tri_nucleotides_counts[i][j][l]) + ',' + "coding\n")
                    else:
                        output_file.writelines(str(tri_nucleotides_counts[i][j][l]) + ", ")
                        
        tri_nucleotides_counts =[[[0 for col in range(4)]for row in range(4)] for x in range(4)]

output_file = 'file_test.csv'
trinucleotides_counts(identifiers, sequences, description, output_file)
data = pd.read_csv('file_test.csv', header = None)
data<jupyter_output><empty_output><jupyter_text>Separando alvo (classe a ser predita) dos atributos<jupyter_code>Y=data[64]
Y
X = data[data.columns[data.columns!=64]].astype(float)
X
X["sum"] = X.sum(axis=1)
X["sum"] =  X["sum"] * 3
X<jupyter_output><empty_output><jupyter_text>Normalização pelo tamanho da sequência<jupyter_code>X = X.iloc[:,0:-1].div(X['sum'], axis=0)
X<jupyter_output><empty_output><jupyter_text>Dividindo entre treinamento e teste<jupyter_code>train_x, test_x, train_y,test_y=train_test_split(X,Y,random_state=0,test_size=0.2)
print(train_y.shape, test_y.shape)<jupyter_output>(5910,) (1478,)<jupyter_text>Redes neurais<jupyter_code>accuracy,conf_marix,y_pred = MLP(train_x, train_y, test_x, test_y, 64)
print("Model Accuracy:" + str(accuracy) + "\n" + "Confusion Matrix:\n" + str(conf_marix))
accuracy = NN(train_x, train_y.map({'coding': 1, 'ncrna': 0}), test_x ,test_y.map({'coding': 1, 'ncrna': 0}), 64)
print("Model Accuracy:" + str(accuracy[1]))<jupyter_output>WARNING:tensorflow:Model was constructed with shape (None, 5910, 64) for input KerasTensor(type_spec=TensorSpec(shape=(None, 5910, 64), dtype=tf.float32, name='dense_32_input'), name='dense_32_input', description="created by layer 'dense_32_input'"), but it was called on an input with incompatible shape (None, 64).
WARNING:tensorflow:Model was constructed with shape (None, 5910, 64) for input KerasTensor(type_spec=TensorSpec(shape=(None, 5910, 64), dtype=tf.float32, name='dense_32_input'), name='dense_32_input', description="created by layer 'dense_32_input'"), but it was called on an input with incompatible shape (None, 64).
WARNING:tensorflow:Model was constructed with shape (None, 5910, 64) for input KerasTensor(type_spec=TensorSpec(shape=(None, 5910, 64), dtype=tf.float32, name='dense_32_input'), name='dense_32_input', description="created by layer 'dense_32_input'"), but it was called on an input with incompatible shape (None, 64).
47/47 [==============================] - 0s 504us[...]
{'fraction_non_alphanumeric': 0.12461337548972438, 'fraction_numerical': 0.037115952986459554, 'mean_word_length': 4.803749501396091, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 82, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '437372', 'license': 'MIT', 'n_tokens_mistral': 5495, 'n_tokens_neox': 4959, 'n_words': 1170, 'path': 'Aula_Redes_Neurais_Bioinfo.ipynb', 'repo_name': 'thaisratis/Disciplina_ML_Bioinfo', 'size': '14549'}