Regressão Random Forest – Prática em Python

Eai meu povo, tudo bem?

Aqui está o codigo que usamos no video abaixo, um exemplo simples de Random Forest Regression.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  18 15:01:05 2021

@author: rafaeldontalgoncalez
"""

######################################
# Importando as bibliotecas
######################################

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

######################################
# Importa o dataset
######################################

dataset = pd.read_csv('efetividade_vacina.csv')
X = dataset.iloc[:, 0].values.reshape(-1,1)
y = dataset.iloc[:, -1].values


######################################
# Treinando o modelo para Regressao Random Forest
######################################

regressor = RandomForestRegressor(n_estimators=10)
regressor.fit(X,y)


######################################
# Imprime a Regressao Random Forest
######################################


plt.scatter(X, y, color = 'red')
plt.plot(X, regressor.predict(X), color = 'blue')
plt.title('Regressao Random Forest')
plt.xlabel('Idade')
plt.ylabel('Efetividade da Vacina (%)')
plt.show()


######################################
# Prevendo resultados para Regressao Random Forest
######################################

print('5 anos =', regressor.predict([[5]]))
print('6 anos =', regressor.predict([[6]]))
print('8 anos =', regressor.predict([[8]]))
print('11 anos =', regressor.predict([[11]]))
print('12 anos =', regressor.predict([[12]]))
print('20 anos =', regressor.predict([[20]]))
print('27 anos =', regressor.predict([[27]]))
print('28 anos =', regressor.predict([[28]]))
print('31 anos =', regressor.predict([[31]]))
print('33 anos =', regressor.predict([[33]]))
print('37 anos =', regressor.predict([[37]]))
print('43 anos =', regressor.predict([[43]]))
print('47 anos =', regressor.predict([[47]]))
print('48 anos =', regressor.predict([[48]]))
print('78 anos =', regressor.predict([[78]]))

efetividade_vacina.