<jupyter_start><jupyter_text>Considerações* Este código foi altamente baseado na implementação de Roger Bellavista, disponível em https://www.kaggle.com/rogerbellavista/randomforestregressor-mae-0-0922-rmse-0-2314<jupyter_code>import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
# DataSet Read
df = pd.read_csv("../datasets/MiningProcess_Flotation_Plant_Database.csv",decimal=",")
# Delete date column
df= df.drop(df.columns[[0]], axis=1)
df.head()
df['% Silica Concentrate'].describe()<jupyter_output><empty_output><jupyter_text>Model training* Random Forest Regression* Always without % Iron Concentrate* Number of estimator automaticly selected* No prunning (the dataset is big, but we dont care for performace or interpretation)<jupyter_code>from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
import math
import sklearn.metrics as metrics

# split 70% of training 30% of test
train, test = train_test_split(df, test_size=0.3)
X= train.drop(['% Silica Concentrate','% Iron Concentrate'], axis=1)
y= train['% Silica Concentrate']
# Random Forest
model = RandomForestRegressor()
model.fit(X,y)<jupyter_output>/home/benitez/anaconda3/lib/python3.7/site-packages/sklearn/ensemble/forest.py:246: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)<jupyter_text>Modelo 2: regression tree* Foco: interpretabilidade* depth criteria: interpretable. WE NEED TO TEST OVERFIT<jupyter_code>model2 = DecisionTreeRegressor (criterion='mae', max_depth=10)
model2.fit(X,y)<jupyter_output><empty_output><jupyter_text>Evaluation 2* Repeat without % Iron Concentrate* The features importance are more "distributed"* The most important is % silica Feed (0.124), Ore Pulp pH (0.121), % iron Feed (0.105)<jupyter_code># TEST SET

y2_hat = model2.predict(X2)

print ("MAE:                ", metrics.mean_absolute_error(y,y2_hat))
print ("RMSE:               ", math.sqrt(metrics.mean_squared_error(y,y2_hat)))
print ("r2:                 ", model2.score(X,y))

# Feature analysis
print ("=====================================")
print ("FEATURE IMPORTANCE:")
for i in range(model2.feature_importances_.size):
    print (X.columns[i], "=", model2.feature_importances_[i])
# Previsto vs real
line = np.arange(0, 5, 0.01)

plt.scatter(y2,y2_hat, Alpha=0.2)
plt.scatter(line,line)
plt.grid(True)
# Plot das 3 variáveis mais importantes 
f, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True,figsize=(15,6))
ax1.scatter(X2['% Silica Feed'],y2, Alpha=0.02)
ax1.set_ylabel ('Output (% Silica)')
ax1.set_xlabel ('% Silica Feed')
ax2.scatter(X2['Ore Pulp pH'],y2, Alpha=0.02)
ax2.set_ylabel ('Output (% Silica)')
ax2.set_xlabel ('Ore Pulp pH')
ax3.scatter(X2['% Iron Feed'],y2, Alpha=0.02)
ax3.set_ylabel ('Output (% Silica)')
ax3.set_xlabel ('% Iron Feed')
# Plot das proximas 3 variáveis "mais" importantes 
f, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True,figsize=(15,6))
ax1.scatter(X2['Amina Flow'],y2, Alpha=0.02)
ax1.set_ylabel ('Output (% Silica)')
ax1.set_xlabel ('Amina Flow')
ax2.scatter(X2['Flotation Column 04 Air Flow'],y2, Alpha=0.02)
ax2.set_ylabel ('Output (% Silica)')
ax2.set_xlabel ('Flotation Column 04 Air Flow')
ax3.scatter(X2['Flotation Column 01 Level'],y2, Alpha=0.02)
ax3.set_ylabel ('Output (% Silica)')
ax3.set_xlabel ('Flotation Column 01 Level')
X2= train.drop(['% Silica Concentrate','% Iron Concentrate'], axis=1)
y2= train['% Silica Concentrate']


plt.scatter(X2['% Silica Feed'],X2['Ore Pulp pH'], Alpha=0.5, c=y2)
from sklearn.tree import export_graphviz
# Export as dot file
export_graphviz (model2, out_file='tree3.dot', 
                feature_names = X2.columns,
                rounded = True, 
                proportion = False,
                precision = 2,
                filled = True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14422834049002273, 'fraction_numerical': 0.042687547360444555, 'mean_word_length': 5.2757527733755945, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '323797', 'license': 'MIT', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1417, 'n_words': 349, 'path': 'artificial_intelligence/08 - Mining Dataset/.ipynb_checkpoints/simple regression V2-checkpoint.ipynb', 'repo_name': 'LeonardoSanBenitez/LorisWeb', 'size': '3959'}