<jupyter_start><jupyter_text>REGRESSÃO LOGÍSTICA<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve, auc
x = df.drop(["Target", "Retorno"], axis = 1)
y = df["Target"]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.5, shuffle = False)
modelo = LogisticRegression()
modelo.fit(x_train, y_train)
y_pred_train = modelo.predict(x_train)
y_pred_test = modelo.predict(x_test)
print("R2: ",modelo.score(x_train, y_train))

resultados = pd.DataFrame({"Previsto" : y_pred_test, "Observado" : y_test})
print("Avaliando a base de treinamento:")
print(confusion_matrix(y_train, y_pred_train))
print("\n")
# Avaliando a base de teste
print("Avaliando a base de test:")
print(confusion_matrix(y_test, y_pred_test))
# Calculando a ROC e o Gini - Treinamento
fpr_train, tpr_train, thresholds_train = roc_curve(y_train, y_pred_train)
roc_auc_train = auc(fpr_train, tpr_train)

print("AUC - Treinamento : %f" % roc_auc_train)
print("Gini - Treinamento: ", round(2*roc_auc_train-1,2))
print("\n")

# Calculando a ROC e o Gini - Teste
fpr_test, tpr_test, thresholds_test = roc_curve(y_test, y_pred_test)
roc_auc_test = auc(fpr_test, tpr_test)

print("AUC - Teste : %f" % roc_auc_test)
print("Gini - Teste: ", round(2*roc_auc_test-1,2))<jupyter_output>AUC - Treinamento : 0.533816
Gini - Treinamento:  0.07


AUC - Teste : 0.454545
Gini - Teste:  -0.09<jupyter_text>Plot ROC<jupyter_code>#define metrics
y_pred_proba = modelo.predict_proba(x_test)[::,1]
fpr, tpr, _ = roc_curve(y_test,  y_pred_proba)

#create ROC curve
plt.plot(fpr,tpr)
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.title("ROC - Receiver operating characteristic")
plt.show()<jupyter_output><empty_output><jupyter_text>Backtest<jupyter_code>x = df.drop("Target", axis = 1)
y = df["Target"]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.5, shuffle = False)
retorno = []
for i in range(len(resultados["Previsto"])):
    if resultados["Previsto"][i] == True:
        retorno.append(x["Retorno"][i])
    else:
        retorno.append(-x["Retorno"][i])

resultados["Retorno"] = retorno
resultados["Ret Acumulado"] = np.cumprod(1 + np.array(retorno))
resultados["Retorno_BH_acum"] = np.cumprod(1 + x["Retorno"])
resultados.head()
resultados[["Ret Acumulado", "Retorno_BH_acum"]].plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1434327155519742, 'fraction_numerical': 0.014504431909750202, 'mean_word_length': 6.570121951219512, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '131060', 'license': 'MIT', 'n_tokens_mistral': 1043, 'n_tokens_neox': 996, 'n_words': 189, 'path': 'sklearn/projetos/.ipynb_checkpoints/reg_log-checkpoint.ipynb', 'repo_name': 'bruno-marcelino10/estudos-com-py', 'size': '2482'}