# ---
# 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
# ---

# +
import pandas as pd
import numpy as np

from sklearn.datasets import load_diabetes
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeRegressor

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

from graphviz import render
# -

# Importando o arquivo do dataset, gerando um DataFrame
full_data = pd.read_csv('modelo_ajustado1.csv')
full_data.shape

X = full_data.drop('G3', axis=1)
y = full_data['G3']

print(X.shape)
print(y.shape)

regr_1 = DecisionTreeRegressor()
regr_1.fit(X, y)

y_pred = regr_1.predict(X)

y_pred

from sklearn.metrics import r2_score
print(r2_score(y, regr_1.predict(X)))

from sklearn.metrics import mean_absolute_error
mean_absolute_error(y, y_pred)

regr_1 = DecisionTreeRegressor(max_depth=4)
regr_1.fit(X, y)

y_pred = regr_1.predict(X)

mean_absolute_error(y, y_pred)

# # Treinamento e Teste

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.20, random_state=32)

print('Treino e teste (X variáveis e y resposta):'
      '\n>> Treino X {trainX}\n>> Treino y {trainY}'
      '\n>> Teste X {testX}\n>> Teste y {testY}'.format(trainX=X_train.shape,
                                                        trainY=y_train.shape,
                                                        testX=X_test.shape, 
                                                        testY=y_test.shape))

# +
regr_1 = DecisionTreeRegressor(max_depth=4)
regr_1.fit(X_train, y_train)

y_pred = regr_1.predict(X_test)
# -

mean_absolute_error(y_test, y_pred)

# +
from sklearn.tree import export_graphviz
export_graphviz(regr_1, out_file='tree.dot')

render('dot', 'png', 'tree.dot')

# +
from sklearn import tree

# plotando árvore
plt.figure(figsize=(12,12))
tree.plot_tree(regr_1, fontsize=8)
plt.show()
# -

X

# # Importância das variáveis

from matplotlib import pyplot

# +
importance = regr_1.feature_importances_

# summarize feature importance
for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
# -

importance

features_n = X.columns

# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.xticks([x for x in range(len(importance))], features_n, rotation='vertical')
pyplot.show()

# # Análise dos Resultados



StarcoderdataJupyterScriptsDedupFiltered