<jupyter_start><jupyter_text>Modelos de Aprendizado de MáquinaNeste notebook tem os seguintes modelos de aprendizado de máquina comparados:- Floresta Aleatória- SVM  **Prepare Environment**  Imports<jupyter_code># Data analysis and data wrangling
import numpy as np
import pandas as pd

# Metrics
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_percentage_error

# Plotting
import seaborn as sns
import matplotlib.pyplot as plt

# machine learning
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR
import xgboost as xgb

# Other
from IPython.display import Image
import warnings
import pprint
import datetime
import os<jupyter_output><empty_output><jupyter_text>Prepare Principal Directory<jupyter_code>def path_to_work(end_directory: str='notebooks'):
    curr_dir = os.path.dirname(os.path.realpath ("__file__")) 
    
    if curr_dir.endswith(end_directory):
        os.chdir('..')
        return f'Change directory to: {curr_dir}'
    
    return f'Current working directory: {curr_dir}'
path_to_work('notebooks')<jupyter_output><empty_output><jupyter_text>Set Config<jupyter_code># Visualization inside the jupyter
%matplotlib inline

# Load the "autoreload" extension so that code can change
%load_ext autoreload

# ----------
# Plot
# ----------
# graph style
sns.set_style("darkgrid")
plt.style.use('fivethirtyeight')

# ----------
# Seaborn rcParams
# ----------
rc={'savefig.dpi': 500, 
    'figure.autolayout': True, 
    'figure.figsize': [17, 12], 
    'axes.labelsize': 18,
    'axes.titlesize': 18, 
    'font.size': 10, 
    'lines.linewidth': 1.0, 
    'lines.markersize': 8, 
    'legend.fontsize': 15,
    'xtick.labelsize': 15, 
    'ytick.labelsize': 15}

sns.set(context='notebook',  # notebook
        style='darkgrid',
        palette='deep',
        color_codes=True, 
        rc=rc)

# ----------
# Pandas
# ----------
# Floating point
pd.options.display.float_format = '{:.2f}'.format

# Print xxxx rows and all columns
pd.set_option('display.max_rows', 300)
pd.set_option('display.max_columns', None)

# ----------
# Python
# ----------
# pretty print
pp = pprint.PrettyPrinter(indent=4)

# Supress unnecessary warnings so that presentation looks clean
warnings.filterwarnings('ignore')
plt.style.use('seaborn')  # fivethirtyeight
plt.rc('figure',figsize=(16,8))
plt.rc('font',size=15)
plt.rc('legend',fontsize=15)

# Seaborn rcParams
# ===================
sns.set(context='poster',  # notebook
        style='darkgrid',
        palette='deep',
        color_codes=True)

# graph style
sns.set(style='dark', palette='deep')

plt.style.use('fivethirtyeight')<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>%%time

df_vale3 = pd.read_csv('data/cleansing/df_vale3_cleansing.csv',
                        encoding='utf8',
                        delimiter=',',
                        parse_dates=True,
                        index_col=0,
                        verbose=True)
print(df_vale3.info())
df_vale3.head()<jupyter_output><empty_output><jupyter_text>--- **Split Data**<jupyter_code>size_train = 2132
size_test = 313
print(size_train)
print(size_test)

df_train = df_vale3.iloc[:size_train]
df_test = df_vale3.iloc[size_train:]
print(df_train.columns)
print(df_test.columns)
df_vale3_without_target = df_train.columns
df_vale3_without_target = df_vale3_without_target.drop('preco')

print(df_vale3_without_target)
df_train['preco'].plot(linewidth=1)
plt.grid(True)
plt.title(f'Train size: {len(df_train)}')
df_test['preco'].plot(linewidth=1)
plt.grid(True)
plt.title(f'Test size: {len(df_test)}')
df_train.index
df_test.index
X_train = df_train[df_vale3_without_target]
y_train = df_train['preco']
print(len(X_train))
print(len(y_train))
print(type(X_train))
print(type(y_train))
print((X_train[:5]))
print(y_train[:5])


X_train = df_train[df_vale3_without_target]
y_train = df_train['preco']
X_test = df_test[df_vale3_without_target]
y_test = df_test['preco']
print(len(X_test))
print(len(y_test))
print(type(X_test))
print(type(y_test))
print(X_test[:5])
print(y_test[:5])<jupyter_output>313
313
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.series.Series'>
            residuos  tendencia  sazonalidade    diff_1    diff_2    diff_3  \
data                                                                          
2019-02-22  0.963506  46.696167      1.000439  1.610000  1.190000  1.500000   
2019-02-25  0.933996  46.888333      1.000935  0.130000  1.740000  1.320000   
2019-02-26  0.938870  47.090500      1.001784  0.080000  0.210000  1.820000   
2019-02-27  0.891661  47.321167      1.000287 -0.370000 -0.290000 -0.160000   
2019-02-28  0.880628  47.580333      1.001320  0.270000 -0.100000 -0.020000   

             diff_4   diff_5  
data                          
2019-02-22 1.740000 1.110000  
2019-02-25 1.630000 1.870000  
2019-02-26 1.400000 1.710000  
2019-02-27 1.450000 1.030000  
2019-02-28 0.110000 1.720000  
data
2019-02-22   46.990000
2019-02-25   47.120000
2019-02-26   47.200000
2019-02-27   46.830000
2019-02-28   47.100000
Name: preco, dtype: float64[...]<jupyter_text>--- --- **Evaluation**<jupyter_code>dict_results = {}<jupyter_output><empty_output><jupyter_text>Display Results<jupyter_code>def show_result_model(df_train, df_test, y_forecast, model_name):
    future_forecast = pd.DataFrame(y_forecast,
                                   index=df_test.index,
                                   columns=['previsao'])
    mape = mean_absolute_percentage_error(df_test, y_forecast)*100
    mse = mean_squared_error(df_test, y_forecast, squared=True)
    dict_results[model_name] = [mape, mse]
    
    pd.concat([df_test, future_forecast], axis=1).plot()

    plt.legend()
    plt.grid(True)
    plt.xlabel("Tempo (dias)", fontsize=20)
    plt.ylabel("Preço (R$)", fontsize=20)
    plt.title(f'MAPE = {mape:.2f} % | MSE = {mse:.2f}', fontsize=25)<jupyter_output><empty_output><jupyter_text>--- **Modeling**  **Build Model** **Random Forest**<jupyter_code># RandomForest params dict
rf_params_one = {}
rf_params_one['criterion'] = 'mse'
rf_params_one['n_estimators'] = 10
rf_params_one['max_depth'] = 5
rf_params_one['max_features'] = None
rf_params_one['max_leaf_nodes'] = 15
rf_params_one['min_samples_leaf'] = 1
rf_params_one['random_state'] = 0
rf_params_one['n_jobs'] = -1 # run all process
model_rf_regressor = RandomForestRegressor(**rf_params_one)
model_rf_regressor
model_rf_regressor.fit(X_train, y_train)
y_pred_rf_regressor = model_rf_regressor.predict(X_test)
y_pred_rf_regressor[:5]
len(y_train)
len(y_test)
show_result_model(df_train=y_train,
                  df_test=y_test, 
                  y_forecast=y_pred_rf_regressor,
                  model_name='model_rf_regressor')<jupyter_output><empty_output><jupyter_text>---  **SVM**- Doc sobre gamma: https://scikit-learn.org/stable/auto_examples/svm/plot_rbf_parameters.html- Gamma é quem controla o learning rate<jupyter_code>model_svm_regressor = SVR(kernel='rbf', gamma= 0.1)
model_svm_regressor
model_svm_regressor.fit(X_train, y_train)
y_pred_svm_regressor = model_svm_regressor.predict(X_test)
y_pred_svm_regressor[:5]
show_result_model(df_train=y_train,
                  df_test=y_test, 
                  y_forecast=y_pred_svm_regressor,
                  model_name='model_svm_regressor')<jupyter_output><empty_output><jupyter_text>Zoom<jupyter_code>show_result_model(df_train=y_test,
                  df_test=y_test, 
                  y_forecast=y_pred_svm_regressor,
                  model_name='model_svm_regressor')<jupyter_output><empty_output><jupyter_text>--- **Results**<jupyter_code>dict_results<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1340018231540565, 'fraction_numerical': 0.07084255762469072, 'mean_word_length': 3.7613143211407314, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '298063', 'license': 'MIT', 'n_tokens_mistral': 3259, 'n_tokens_neox': 2846, 'n_words': 452, 'path': 'notebooks/02.02-machine_learning_models.ipynb', 'repo_name': 'brunocampos01/tcc', 'size': '7679'}