<jupyter_start><jupyter_text>HIV-1 protease cleavage Data SetAuthor: <NAME> E-mail: <EMAIL><jupyter_code>import pandas as pd

import numpy as np

from sklearn import tree
from sklearn import metrics
from sklearn import svm
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, RobustScaler
from sklearn.model_selection import train_test_split, cross_val_score, cross_validate
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

import matplotlib.pyplot as plt

import seaborn as sns
df_lc = pd.read_csv('hpcdata_label_coded.csv', engine='c')
df_lc.head()
# Cria uma matriz de dados para plotagem
data_plot = {}

# Separa os dados de treinamento e teste
X = df_lc[['cod_seq1', 'cod_seq2', 'cod_seq3', 'cod_seq4', 'cod_seq5', 'cod_seq6',
           'cod_seq7', 'cod_seq8']]
y = df_lc['cod_target']

# Split dataset into training set and test set
# 70% training and 30% test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 100, shuffle=True, stratify=y)
# Testa o modelo de Decision Tree
model_dt = DecisionTreeClassifier(criterion = 'entropy',
                                  splitter='best',
                                  max_depth=None,
                                  min_samples_split=2,
                                  min_samples_leaf=1,
                                  min_weight_fraction_leaf=0.,
                                  max_features=None,
                                  random_state = 100,
                                  max_leaf_nodes=None,
                                  min_impurity_decrease=0,
                                  #min_impurity_split=0,
                                  class_weight=None,
                                  presort=False)

mdt_fit = model_dt.fit(X_train, y_train)
y_pred = model_dt.predict(X_test)
mac = metrics.accuracy_score(y_test, y_pred)
mcr = classification_report(y_test,y_pred)
mas = accuracy_score(y_test, y_pred)
mcm = confusion_matrix(y_test,y_pred)

print("Accuracy:", mac)

print("\nClassifcation Report")
print(mcr)  

print("\nAccuracy Score")
print(mas)

print("\nConfusion Matrix")
print(mcm)

scores = cross_val_score(model_dt, X, y, cv=10, scoring='accuracy')
print(scores)

msm = scores.mean()
mss = scores.std() * 2
print("Accuracy: %0.2f (+/- %0.2f)" % (msm, mss))

cv_results = cross_validate(model_dt, X, y, scoring=['accuracy', 'brier_score_loss'],
                            cv=10, n_jobs=-1, verbose=1)

data_plot.update({1:['Decision Tree', mac, mcr, mas, mcm, msm, mss]})
# Testa o modelo de Random Forest
model_rf = RandomForestClassifier(n_estimators= 160,
                                  criterion='entropy',
                                  max_features = 'auto',
                                  max_depth = None,
                                  min_samples_split = 2,
                                  min_samples_leaf = 1,
                                  min_weight_fraction_leaf = 0,
                                  max_leaf_nodes = None,
                                  min_impurity_decrease = 0,
                                  bootstrap = True,
                                  oob_score = True,
                                  n_jobs = -1,
                                  random_state = 100,
                                  verbose = 1,
                                  warm_start = False,
                                  class_weight = None)
mrf_fit = model_rf.fit(X_train, y_train)
y_pred = model_rf.predict(X_test)
mac = metrics.accuracy_score(y_test, y_pred)
mcr = classification_report(y_test,y_pred)
mas = accuracy_score(y_test, y_pred)
mcm = confusion_matrix(y_test,y_pred)

print("Accuracy:", mac)

print("\nClassifcation Report")
print(mcr)  

print("\nAccuracy Score")
print(mas)

print("\nConfusion Matrix")
print(mcm)

scores = cross_val_score(model_rf, X, y, cv=10, scoring='accuracy')
print(scores)

msm = scores.mean()
mss = scores.std() * 2
print("Accuracy: %0.2f (+/- %0.2f)" % (msm, mss))

cv_results = cross_validate(model_rf, X, y, scoring=['accuracy', 'brier_score_loss'],
                            cv=10, n_jobs=-1, verbose=1)

data_plot.update({2:['Random Forest', mac, mcr, mas, mcm, msm, mss]})
# Testa o modelo SVM/SVC
model_svc = SVC(C=1.0,
                kernel='rbf', 
                degree=3, 
                gamma='auto', 
                coef0=0.0, 
                shrinking=True, 
                probability=True, 
                tol=0.001, 
                cache_size=200, 
                class_weight=None, 
                verbose=True, 
                max_iter=-1, 
                decision_function_shape='ovr', 
                random_state=100)

mss_fit = model_svc.fit(X_train, y_train)
y_pred = model_svc.predict(X_test)

mac = metrics.accuracy_score(y_test, y_pred)
mcr = classification_report(y_test,y_pred)
mas = accuracy_score(y_test, y_pred)
mcm = confusion_matrix(y_test,y_pred)

print("Accuracy:", mac)

print("\nClassifcation Report")
print(mcr)  

print("\nAccuracy Score")
print(mas)

print("\nConfusion Matrix")
print(mcm)

scores = cross_val_score(model_svc, X, y, cv=10, scoring='accuracy')
print(scores)

msm = scores.mean()
mss = scores.std() * 2
print("Accuracy: %0.2f (+/- %0.2f)" % (msm, mss))

cv_results = cross_validate(model_svc, X, y, scoring=['accuracy', 'brier_score_loss'],
                            cv=10, n_jobs=-1, verbose=1)

data_plot.update({3:['SVM/SVC', mac, mcr, mas, mcm, msm, mss]})
# Testa o modelo Linear SVC
model_lss = LinearSVC(penalty='l2',
                      loss='squared_hinge',
                      dual = False, 
                      tol=0.0001, 
                      C=1.0, 
                      multi_class='ovr', 
                      fit_intercept=True, 
                      intercept_scaling=1, 
                      class_weight = 'balanced', 
                      verbose = 1, 
                      random_state = 100, 
                      max_iter=1000)

mls_fit = model_lss.fit(X_train, y_train)
y_pred = model_lss.predict(X_test)

mac = metrics.accuracy_score(y_test, y_pred)
mcr = classification_report(y_test,y_pred)
mas = accuracy_score(y_test, y_pred)
mcm = confusion_matrix(y_test,y_pred)

print("Accuracy:", mac)

print("\nClassifcation Report")
print(mcr)  

print("\nAccuracy Score")
print(mas)

print("\nConfusion Matrix")
print(mcm)

scores = cross_val_score(model_lss, X, y, cv=10, scoring='accuracy')
print(scores)

msm = scores.mean()
mss = scores.std() * 2
print("Accuracy: %0.2f (+/- %0.2f)" % (msm, mss))

data_plot.update({4:['SVM/LinearSVM', mac, mcr, mas, mcm, msm, mss]})
algoritmos = []
accuracy = []
accuracy_mean = []
accuracy_std = []
for k, v in data_plot.items():
    algoritmos.append(v[0])
    accuracy.append(v[1])
    accuracy_mean.append(v[5])
    accuracy_std.append(v[6])
    
plt.figure(figsize=(12,8))
plt.plot(algoritmos, accuracy, color='blue', linewidth=3.0, label='Acurácia');
plt.plot(algoritmos, accuracy_mean, 'k--', color='green', linewidth=3.0, label='Acurácia média');
plt.plot(algoritmos, accuracy_std, color='orange', linewidth=3.0, label='Desvio padrão');
plt.title('Acurácia obtida por algoritmo')
plt.xlabel('Algoritmo')
plt.ylabel('Acurácia')
plt.grid(True)
plt.legend();
algoritmos = []
tn = []
fp = []
fn = []
tp = []
for k, v in data_plot.items():
    algoritmos.append(v[0])
    tn.append(v[4][0][0])
    fp.append(v[4][1][1])
    fn.append(v[4][1][0])
    tp.append(v[4][1][1])

print(algoritmos)
print(tn)
print(fp)
print(fn)
print(tp)

def scalable_data_one(data):
    np_data = np.array(data)
    data_mean = np_data.mean()
    return [(item/data_mean) for item in data]

def scalable_data(data):
    np_data = np.array(data)
    data_sum = np_data.sum()
    return [(item/data_sum) for item in data]

def scalable_data_two(data):
    np_data = np.array(data)
    data_sum = np_data.sum()
    return [((item/data_sum) - 0.001) for item in data]

tn = scalable_data(tn)
fp = scalable_data(fp)
fn = scalable_data(fn)
tp = scalable_data_two(tp)

print(tn)
print(fp)
print(fn)
print(tp)

plt.figure(figsize=(12,8))
plt.plot(algoritmos, tn, color='#00BFFF', linewidth=3.0, label='True Negative')
plt.plot(algoritmos, fp, color='#00FF00', linewidth=4.0, label='False Positive')
plt.plot(algoritmos, fn, color='#FF8C00', linewidth=3.0, label='False Negative')
plt.plot(algoritmos, tp, color='#FF1493', linewidth=4.0, label='True Positive')
plt.title('Matriz de confusão por algoritmo')
plt.xlabel('Algoritmo')
plt.ylabel('Matriz de confusão')
plt.grid(True)
plt.legend();<jupyter_output>['Decision Tree', 'Random Forest', 'SVM/SVC', 'SVM/LinearSVM']
[1421, 1546, 1559, 862]
[291, 251, 184, 197]
[117, 157, 224, 211]
[291, 251, 184, 197]
[0.2637342242019302, 0.28693392724573125, 0.28934669636228655, 0.15998515219005197]
[0.3152762730227519, 0.27193932827735645, 0.19934994582881907, 0.2134344528710726]
[0.16502115655853314, 0.22143864598025387, 0.3159379407616361, 0.29760225669957685]
[0.3142762730227519, 0.27093932827735645, 0.19834994582881907, 0.2124344528710726]
{'fraction_non_alphanumeric': 0.12843339485397895, 'fraction_numerical': 0.054608620128107695, 'mean_word_length': 2.65845909451946, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '230055', 'license': 'Apache-2.0', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3227, 'n_words': 633, 'path': 'jupyter/ML T3 PART 2 - PPGCC-DC-UFSCar.ipynb', 'repo_name': 'darosantos/RFNS', 'size': '9241'}