<jupyter_start><jupyter_text>Carga dos dados<jupyter_code>import pandas as pd 
temperaturas = pd.read_csv('temps.csv')
temperaturas.head()<jupyter_output><empty_output><jupyter_text>Análise Exploratória<jupyter_code>print('Dimensões dos dataset temps: ', temperaturas.shape)
temperaturas.describe()
print('Quantos dias do ano foram utilizados: ', 348/365 * 100, '%')
temperaturas.info()
from datetime import datetime

years = temperaturas['year']
months = temperaturas['month']
days = temperaturas['day']

# Formato americano para a data: yyyy-mm-dd
dates=[str(year) + '-' + str(month) + '-' + str(day) for year, month, day in zip(years, months, days)]
dates=[datetime.strptime(date, '%Y-%m-%d') for date in dates]
import matplotlib.pyplot as plt
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))
fig.autofmt_xdate(rotation=45)

_ = ax1.plot(dates, temperaturas['actual'])
ax1.set_xlabel(''); ax1.set_ylabel('Temperatura'); ax1.set_title('Atual')

_ = ax2.plot(dates, temperaturas['temp_1'])
ax2.set_xlabel(''); ax2.set_ylabel('Temperatura'); ax2.set_title('Dia anterior')

_ = ax3.plot(dates, temperaturas['temp_2'])
ax3.set_xlabel(''); ax3.set_ylabel('Temperatura'); ax3.set_title('Dois dias atrás')

_ = ax4.plot(dates, temperaturas['friend'])
ax4.set_xlabel(''); ax4.set_ylabel('Temperatura'); ax4.set_title('Estimada pelo amigo')<jupyter_output><empty_output><jupyter_text>Preparação dos dados<jupyter_code># Transforma o dado categorico em númerico (dias da semana)
temperaturas = pd.get_dummies(temperaturas)
temperaturas.head()
print('Dimensões do dataset temps: ', temperaturas.shape)<jupyter_output>Dimensões do dataset temps:  (348, 15)<jupyter_text>Treinamento e Teste do Modelo<jupyter_code>X = temperaturas.drop('actual', axis=1).values
y = temperaturas['actual'].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=42)
print('X_train shape', X_train.shape)
print('X_test shape', X_train.shape)
print('y_train shape', y_train.shape)
print('y_test shape', y_test.shape)<jupyter_output>X_train shape (278, 14)
X_test shape (278, 14)
y_train shape (278,)
y_test shape (70,)<jupyter_text>Base de comparação<jupyter_code>import numpy as np
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error


colunas = list(temperaturas.columns)
y_hist = X_test[:, colunas.index('average')]
print('MAE :', mean_absolute_error(y_test, y_hist))
print('MSE :', mean_squared_error(y_test, y_hist))
print('RMSE:', np.sqrt(mean_squared_error(y_test, y_hist)))<jupyter_output>MAE : 5.085714285714285
MSE : 55.10257142857142
RMSE: 7.42311063022581<jupyter_text>Treinamento do modelo<jupyter_code>from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
print('MAE :', mean_absolute_error(y_test, y_pred))
print('MSE :', mean_squared_error(y_test, y_pred))
print('RMSE:', np.sqrt(mean_squared_error(y_test, y_pred)))
from sklearn.tree import plot_tree
fig = plt.figure(figsize=(25, 20))
# Escolhe uma arvore em toda a floresta, nesse caso a de número 5
_ = plot_tree(rf.estimators_[5], filled=True, feature_names=colunas, max_depth=2)<jupyter_output><empty_output><jupyter_text>Importância das variáveis independentes<jupyter_code>importancias = list(rf.feature_importances_)
variaveis_importancias = [(coluna, importancia) for coluna, importancia in zip(colunas, importancias)]
variaveis_importancias = sorted(variaveis_importancias, key=lambda x:x[1], reverse=True)
for v, i in variaveis_importancias:
    print(f'Variavel: {v:10} - Importancia: {i}')
X_valores = list(range(len(importancias)))
_=plt.bar(X_valores, importancias)
colunas = colunas[:-1]
_=plt.xticks(X_valores, colunas, rotation='vertical')<jupyter_output><empty_output><jupyter_text>Variaveis mais importantes<jupyter_code>rf_imp = RandomForestRegressor(n_estimators=100, random_state=42)
indices = [colunas.index('temp_1'), colunas.index('average')]
X_train_imp = X_train[:, indices]
X_test_imp = X_test[:, indices]
rf_imp.fit(X_train_imp, y_train)
y_pred = rf_imp.predict(X_test_imp)
print('MAE :', mean_absolute_error(y_test, y_pred))
print('MSE :', mean_squared_error(y_test, y_pred))
print('RMSE:', np.sqrt(mean_squared_error(y_test, y_pred)))<jupyter_output>MAE : 3.901814285714284
MSE : 26.735925360317452
RMSE: 5.170679390594378<jupyter_text>Avaliação de outros modelos<jupyter_code>from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
RANDOM_STATE = 42

def build_regressors():
    regressors = []

    regressors.append(
        ('lr', 
        LinearRegression(),
        {'fit_intercept': [True, False]})
    )

    regressors.append(
        ('svr',
        SVR(),
        {'C': [0.1, 1, 10],
         'gamma': [0.00001, 0.0001, 0.001, 0.01, 1],
         'epsilon': [0.0001, 0.005, 0.001, 1, 5, 10]})
    )

    return regressors
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score

train_results = []
test_results = []
names = []

scoring = 'neg_root_mean_squared_error'

regressors = build_regressors()
kfold = KFold(n_splits=3, shuffle=True, random_state=RANDOM_STATE)

for name, regressor, params in regressors:
    print(f">>> pregressor : {name}")

    # Treinamento
    gs = GridSearchCV(regressor, param_grid=params, scoring=scoring, verbose=1)
    gs.fit(X_train, y_train)
    cv_results = cross_val_score(gs, X_train, y_train, cv=kfold, scoring=scoring) # análise
    names.append(name)
    train_results.append(abs(cv_results))

    # Teste
    y_pred = gs.predict(X_test)
    rmse = mean_squared_error(y_test, y_pred) ** 0.5
    test_results.append(rmse)


fig, ax = plt.subplots()
fig.suptitle('Desempenho em treinamento')
_ = plt.boxplot(train_results)
ax.set_xticklabels(names)
ax.set_ylabel('RMSE')
ax.set_xlabel('Regressores')
fig, ax = plt.subplots()
fig.suptitle('Desempenho em test')
_ = plt.bar(names, test_results)
ax.set_ylabel('RMSE')
ax.set_xlabel('Regressores')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13730569948186527, 'fraction_numerical': 0.036917098445595854, 'mean_word_length': 6.898976982097187, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '495927', 'license': 'MIT', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2393, 'n_words': 429, 'path': 'aprendizado-de-maquina-i/regressao/temps.ipynb', 'repo_name': 'andredarcie/my-data-science-notebooks', 'size': '6176'}