1 Objetivo

Comparar modelos de supervisados a través de la aplicación de algoritmos de predicción de precios de automóviles determinando el estadístico del error cuadrático medio (rmse).

2 Descripción

  • Se cargan los datos previamente preparados de la dirección https://raw.githubusercontent.com/rpizarrog/Analisis-Inteligente-de-datos/main/datos/CarPrice_Assignment_Numericas_Preparado.csv

  • Se crean datos de entrenamiento al 80%

  • Se crean datos de validación al 20%

  • Se crea el modelo regresión múltiple con datos de entrenamiento

    • Con este modelo se responde a preguntas tales como:

      • ¿cuáles son variables que están por encima del 90% de confianza como predictores?,

      • ¿Cuál es el valor de R Square Adjusted o que tanto representan las variables dependientes al precio del vehículo?

    • Se generan predicciones con datos de validación

    • Se determina el estadístico RMSE para efectos de comparación

  • Se crea el modelo árboles de regresión con los datos de entrenamiento

    • Se identifica la importancia de las variables sobre el precio

    • Se visualiza el árbol de regresión y sus reglas de asociación

  • Se hacen predicciones con datos de validación

  • Se determinar el estadístico RMSE para efectos de comparación

  • Se construye el modelo bosques aleatorios con datos de entrenamiento y con 20 árboles simulados

    • Se identifica la importancia de las variables sobre el precio

    • Se generan predicciones con datos de validación

    • Se determina el estadístico RMSE para efectos de comparación

  • Al final del caso, se describe una interpretación personal

3 Desarrollo

3.1 Cargar librerías

# Tratamiento de datos
import numpy as np
import pandas as pd

# Gráficos
import matplotlib.pyplot as plt

# Preprocesado y moYdelado
from sklearn.model_selection import train_test_split

# Estadisticos y lineal múltiple
import statsmodels.api as sm # Estadísticas R Adjused
import seaborn as sns  # Gráficos
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures # Polinomial

# Arbol de regresion
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import plot_tree
from sklearn.tree import export_graphviz
from sklearn.tree import export_text
from sklearn.model_selection import GridSearchCV

# Random Forest
from sklearn.ensemble import RandomForestRegressor


# Metricas
from sklearn import metrics
from sklearn.metrics import mean_squared_error, r2_score

3.2 Cargar datos

datos = pd.read_csv("https://raw.githubusercontent.com/rpizarrog/Analisis-Inteligente-de-datos/main/datos/CarPrice_Assignment_Numericas_Preparado.csv")
datos
##      Unnamed: 0  symboling  wheelbase  ...  citympg  highwaympg    price
## 0             1          3       88.6  ...       21          27  13495.0
## 1             2          3       88.6  ...       21          27  16500.0
## 2             3          1       94.5  ...       19          26  16500.0
## 3             4          2       99.8  ...       24          30  13950.0
## 4             5          2       99.4  ...       18          22  17450.0
## ..          ...        ...        ...  ...      ...         ...      ...
## 200         201         -1      109.1  ...       23          28  16845.0
## 201         202         -1      109.1  ...       19          25  19045.0
## 202         203         -1      109.1  ...       18          23  21485.0
## 203         204         -1      109.1  ...       26          27  22470.0
## 204         205         -1      109.1  ...       19          25  22625.0
## 
## [205 rows x 16 columns]

3.3 Exploración de datos

print("Observaciones y variables: ", datos.shape)
## Observaciones y variables:  (205, 16)
print("Columnas y tipo de dato")
# datos.columns
## Columnas y tipo de dato
datos.dtypes
## Unnamed: 0            int64
## symboling             int64
## wheelbase           float64
## carlength           float64
## carwidth            float64
## carheight           float64
## curbweight            int64
## enginesize            int64
## boreratio           float64
## stroke              float64
## compressionratio    float64
## horsepower            int64
## peakrpm               int64
## citympg               int64
## highwaympg            int64
## price               float64
## dtype: object

3.4 Diccionario de datos

Col Nombre Descripción
1 Symboling Its assigned insurance risk rating, A value of +3 indicates that the auto is risky, -3 that it is probably pretty safe.(Categorical)
2 wheelbase Weelbase of car (Numeric). Distancia de ejes en pulgadas.
3 carlength Length of car (Numeric). Longitud
4 carwidth Width of car (Numeric). Amplitud
5 carheight height of car (Numeric). Altura
6 curbweight The weight of a car without occupants or baggage. (Numeric). Peso del auto
7 enginesize Size of car (Numeric). Tamaño del carro en …
8 boreratio Boreratio of car (Numeric). Eficiencia de motor
9 stroke Stroke or volume inside the engine (Numeric). Pistones, tiempos, combustión
10 compressionratio compression ratio of car (Numeric). Comprensión o medición de presión en motor
11 horsepower Horsepower (Numeric). Poder del carro
12 peakrpm car peak rpm (Numeric). Picos de revoluciones por minuto
13 citympg Mileage in city (Numeric). Consumo de gasolina
14 highwaympg Mileage on highway (Numeric). Consumo de gasolina
16

price

(Dependent variable)

Price of car (Numeric). Precio del carro en dólares

~Fuente: https://archive.ics.uci.edu/ml/datasets/Automobile~

3.5 Limpiar datos

Dejar solo las variables necesarias:

‘symboling’, ‘wheelbase’, ‘carlength’, ‘carwidth’, ‘carheight’, ‘curbweight’, ‘enginesize’, ‘boreratio’, ‘stroke’, ‘compressionratio’, ‘horsepower’, ‘peakrpm’, ‘citympg’, ‘highwaympg’, ‘price’


datos = datos[['symboling', 'wheelbase', 'carlength', 'carwidth', 'carheight', 'curbweight', 'enginesize', 'boreratio', 'stroke', 'compressionratio', 'horsepower', 'peakrpm', 'citympg', 'highwaympg', 'price']]
datos.describe()
##         symboling   wheelbase   carlength  ...     citympg  highwaympg         price
## count  205.000000  205.000000  205.000000  ...  205.000000  205.000000    205.000000
## mean     0.834146   98.756585  174.049268  ...   25.219512   30.751220  13276.710571
## std      1.245307    6.021776   12.337289  ...    6.542142    6.886443   7988.852332
## min     -2.000000   86.600000  141.100000  ...   13.000000   16.000000   5118.000000
## 25%      0.000000   94.500000  166.300000  ...   19.000000   25.000000   7788.000000
## 50%      1.000000   97.000000  173.200000  ...   24.000000   30.000000  10295.000000
## 75%      2.000000  102.400000  183.100000  ...   30.000000   34.000000  16503.000000
## max      3.000000  120.900000  208.100000  ...   49.000000   54.000000  45400.000000
## 
## [8 rows x 15 columns]
datos
##      symboling  wheelbase  carlength  ...  citympg  highwaympg    price
## 0            3       88.6      168.8  ...       21          27  13495.0
## 1            3       88.6      168.8  ...       21          27  16500.0
## 2            1       94.5      171.2  ...       19          26  16500.0
## 3            2       99.8      176.6  ...       24          30  13950.0
## 4            2       99.4      176.6  ...       18          22  17450.0
## ..         ...        ...        ...  ...      ...         ...      ...
## 200         -1      109.1      188.8  ...       23          28  16845.0
## 201         -1      109.1      188.8  ...       19          25  19045.0
## 202         -1      109.1      188.8  ...       18          23  21485.0
## 203         -1      109.1      188.8  ...       26          27  22470.0
## 204         -1      109.1      188.8  ...       19          25  22625.0
## 
## [205 rows x 15 columns]

3.5.1 Datos de entrenamiento y validación

Datos de entrenamiento al 80% de los datos y 20% los datos de validación. Semilla 1349

X_entrena, X_valida, Y_entrena, Y_valida = train_test_split(datos.drop(columns = "price"), datos['price'],train_size = 0.80,  random_state = 1349)

3.5.1.1 Datos de entrenamiento

X_entrena
##      symboling  wheelbase  carlength  ...  peakrpm  citympg  highwaympg
## 179          3      102.9      183.5  ...     5200       19          24
## 28          -1      103.3      174.6  ...     5000       24          30
## 132          3       99.1      186.6  ...     5250       21          28
## 116          0      107.9      186.7  ...     4150       28          33
## 123         -1      103.3      174.6  ...     5000       24          30
## ..         ...        ...        ...  ...      ...      ...         ...
## 194         -2      104.3      188.8  ...     5400       23          28
## 164          1       94.5      168.7  ...     4800       29          34
## 17           0      110.0      197.0  ...     5400       15          20
## 126          3       89.5      168.9  ...     5900       17          25
## 18           2       88.4      141.1  ...     5100       47          53
## 
## [164 rows x 14 columns]

3.5.1.2 Datos de validación

X_valida
##      symboling  wheelbase  carlength  ...  peakrpm  citympg  highwaympg
## 154          0       95.7      169.7  ...     4800       27          32
## 147          0       97.0      173.5  ...     5200       25          31
## 104          3       91.3      170.7  ...     5200       19          25
## 102          0      100.4      184.6  ...     5200       17          22
## 61           1       98.8      177.8  ...     4800       26          32
## 163          1       94.5      168.7  ...     4800       29          34
## 124          3       95.9      173.2  ...     5000       19          24
## 7            1      105.8      192.7  ...     5500       19          25
## 169          2       98.4      176.2  ...     4800       24          30
## 16           0      103.5      193.8  ...     5400       16          22
## 15           0      103.5      189.0  ...     5400       16          22
## 73           0      120.9      208.1  ...     4500       14          16
## 187          2       97.3      171.7  ...     4500       37          42
## 109          0      114.2      198.9  ...     5000       19          24
## 52           1       93.1      159.1  ...     5000       31          38
## 111          0      107.9      186.7  ...     5000       19          24
## 80           3       96.3      173.0  ...     5500       23          30
## 103          0      100.4      184.6  ...     5200       19          25
## 75           1      102.7      178.4  ...     5000       19          24
## 10           2      101.2      176.8  ...     5800       23          29
## 54           1       93.1      166.8  ...     5000       31          38
## 40           0       96.5      175.4  ...     5800       27          33
## 57           3       95.3      169.0  ...     6000       17          23
## 66           0      104.9      175.0  ...     4200       31          39
## 137          2       99.1      186.6  ...     5500       19          26
## 161          0       95.7      166.3  ...     4800       28          34
## 158          0       95.7      166.3  ...     4500       34          36
## 11           0      101.2      176.8  ...     5800       23          29
## 171          2       98.4      176.2  ...     4800       24          30
## 2            1       94.5      171.2  ...     5000       19          26
## 177         -1      102.4      175.6  ...     4200       27          32
## 184          2       97.3      171.7  ...     4800       37          46
## 21           1       93.7      157.3  ...     5500       37          41
## 82           3       95.9      173.2  ...     5000       19          24
## 125          3       94.5      168.9  ...     5500       19          27
## 6            1      105.8      192.7  ...     5500       19          25
## 65           0      104.9      175.0  ...     5000       19          27
## 48           0      113.0      199.6  ...     4750       15          19
## 42           1       96.5      169.1  ...     5500       25          31
## 27           1       93.7      157.3  ...     5500       24          30
## 79           1       93.0      157.3  ...     5500       24          30
## 
## [41 rows x 14 columns]

3.6 Modelos Supervisados

3.6.1 Modelo de regresión lineal múltiple. (RM)

Se construye el modelo de regresión lineal múltiple (rm)

modelo_rm = LinearRegression()
 
modelo_rm.fit(X_entrena,Y_entrena)
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

3.6.1.1 Coeficientes

Solo se muestran los coeficientes de: \(\beta_1, \beta_2, ...\beta_n\)

modelo_rm.coef_
## array([ 3.43157155e+02,  1.51903870e+01, -1.13534712e+02,  7.70362919e+02,
##         3.23536794e+02,  2.96799050e+00,  1.06906358e+02, -1.72051534e+03,
##        -2.99694085e+03,  2.51940047e+02,  3.52827086e+01,  1.70779936e+00,
##        -1.80810204e+02,  9.42771950e+01])
  • En modelos lineales múltiples el estadístico Adjusted R-squared: 0.8347 significa que las variables independientes explican aproximadamente el 83.47% de la variable dependiente precio.
print(modelo_rm.score(X_entrena, Y_entrena))
## 0.8686632699911141

3.6.1.2 Predicciones del modelo rm

predicciones_rm = modelo_rm.predict(X_valida)
print(predicciones_rm[:-1])
## [ 7425.97160548 10895.20899664 23933.35662532 22920.43759406
##  10065.07126253  6220.22581519 15712.58745231 19094.54494932
##  13151.99635195 26110.09792267 26086.57663728 38915.62637259
##  10214.92858711 14676.02944758  6598.50866682 17784.71681822
##  10262.34855333 21817.66621855 19100.88650115 12172.2469208
##   5771.82518678  7322.07510171  8332.73519914 13134.35459405
##  16833.14707877  6581.9476114   8785.55759535 11485.93261131
##  13635.77880361 18659.23590762  8754.65533182 10376.61435969
##   5261.83182286 15774.31246318 20408.53583706 18768.06599421
##  15128.55952034 30135.37992201 10019.21398334  8461.63499463]

3.6.1.3 Tabla comparativa

comparaciones = pd.DataFrame(X_valida)
comparaciones = comparaciones.assign(Precio_Real = Y_valida)
comparaciones = comparaciones.assign(Precio_Prediccion = predicciones_rm.flatten().tolist())
print(comparaciones)
##      symboling  wheelbase  ...  Precio_Real  Precio_Prediccion
## 154          0       95.7  ...       7898.0        7425.971605
## 147          0       97.0  ...      10198.0       10895.208997
## 104          3       91.3  ...      17199.0       23933.356625
## 102          0      100.4  ...      14399.0       22920.437594
## 61           1       98.8  ...      10595.0       10065.071263
## 163          1       94.5  ...       8058.0        6220.225815
## 124          3       95.9  ...      12764.0       15712.587452
## 7            1      105.8  ...      18920.0       19094.544949
## 169          2       98.4  ...       9989.0       13151.996352
## 16           0      103.5  ...      41315.0       26110.097923
## 15           0      103.5  ...      30760.0       26086.576637
## 73           0      120.9  ...      40960.0       38915.626373
## 187          2       97.3  ...       9495.0       10214.928587
## 109          0      114.2  ...      12440.0       14676.029448
## 52           1       93.1  ...       6795.0        6598.508667
## 111          0      107.9  ...      15580.0       17784.716818
## 80           3       96.3  ...       9959.0       10262.348553
## 103          0      100.4  ...      13499.0       21817.666219
## 75           1      102.7  ...      16503.0       19100.886501
## 10           2      101.2  ...      16430.0       12172.246921
## 54           1       93.1  ...       7395.0        5771.825187
## 40           0       96.5  ...      10295.0        7322.075102
## 57           3       95.3  ...      13645.0        8332.735199
## 66           0      104.9  ...      18344.0       13134.354594
## 137          2       99.1  ...      18620.0       16833.147079
## 161          0       95.7  ...       8358.0        6581.947611
## 158          0       95.7  ...       7898.0        8785.557595
## 11           0      101.2  ...      16925.0       11485.932611
## 171          2       98.4  ...      11549.0       13635.778804
## 2            1       94.5  ...      16500.0       18659.235908
## 177         -1      102.4  ...      11248.0        8754.655332
## 184          2       97.3  ...       7995.0       10376.614360
## 21           1       93.7  ...       5572.0        5261.831823
## 82           3       95.9  ...      12629.0       15774.312463
## 125          3       94.5  ...      22018.0       20408.535837
## 6            1      105.8  ...      17710.0       18768.065994
## 65           0      104.9  ...      18280.0       15128.559520
## 48           0      113.0  ...      35550.0       30135.379922
## 42           1       96.5  ...      10345.0       10019.213983
## 27           1       93.7  ...       8558.0        8461.634995
## 79           1       93.0  ...       7689.0        8379.181520
## 
## [41 rows x 16 columns]

3.6.1.4 RMSE modelo de rm

rmse_rm = mean_squared_error(
        y_true  = Y_valida,
        y_pred  = predicciones_rm,
        squared = False
       )
print(f"El error (rmse) de test es: {rmse_rm}")
## El error (rmse) de test es: 4075.0809995218374

o

print('Root Mean Squared Error RMSE:', np.sqrt(metrics.mean_squared_error(Y_valida, predicciones_rm)))
## Root Mean Squared Error RMSE: 4075.0809995218374

3.6.2 Modelo de árbol de regresión (AR)

Se construye el modelo de árbol de regresión (ar)

modelo_ar = DecisionTreeRegressor(
            #max_depth         = 3,
            random_state      = 1349
          )

Entrenar el modelo

modelo_ar.fit(X_entrena, Y_entrena)
DecisionTreeRegressor(random_state=1349)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

3.6.2.1 Visualización de árbol de regresión

fig, ax = plt.subplots(figsize=(12, 5))

print(f"Profundidad del árbol: {modelo_ar.get_depth()}")
## Profundidad del árbol: 13
print(f"Número de nodos terminales: {modelo_ar.get_n_leaves()}")
## Número de nodos terminales: 152
plot = plot_tree(
            decision_tree = modelo_ar,
            feature_names = datos.drop(columns = "price").columns,
            class_names   = 'price',
            filled        = True,
            impurity      = False,
            fontsize      = 10,
            precision     = 2,
            ax            = ax
       )

plot

Reglas de asociación del árbol

texto_modelo = export_text(
                    decision_tree = modelo_ar,
                    feature_names = list(datos.drop(columns = "price").columns)
               )
print(texto_modelo)
## |--- enginesize <= 182.00
## |   |--- curbweight <= 2544.00
## |   |   |--- curbweight <= 2216.50
## |   |   |   |--- horsepower <= 68.50
## |   |   |   |   |--- curbweight <= 1987.00
## |   |   |   |   |   |--- stroke <= 3.11
## |   |   |   |   |   |   |--- highwaympg <= 47.50
## |   |   |   |   |   |   |   |--- horsepower <= 61.00
## |   |   |   |   |   |   |   |   |--- value: [5399.00]
## |   |   |   |   |   |   |   |--- horsepower >  61.00
## |   |   |   |   |   |   |   |   |--- value: [5348.00]
## |   |   |   |   |   |   |--- highwaympg >  47.50
## |   |   |   |   |   |   |   |--- value: [5151.00]
## |   |   |   |   |   |--- stroke >  3.11
## |   |   |   |   |   |   |--- highwaympg <= 34.50
## |   |   |   |   |   |   |   |--- value: [5195.00]
## |   |   |   |   |   |   |--- highwaympg >  34.50
## |   |   |   |   |   |   |   |--- citympg <= 34.00
## |   |   |   |   |   |   |   |   |--- carlength <= 162.95
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 1888.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [6377.00]
## |   |   |   |   |   |   |   |   |   |--- curbweight >  1888.00
## |   |   |   |   |   |   |   |   |   |   |--- stroke <= 3.19
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [6095.00]
## |   |   |   |   |   |   |   |   |   |   |--- stroke >  3.19
## |   |   |   |   |   |   |   |   |   |   |   |--- truncated branch of depth 2
## |   |   |   |   |   |   |   |   |--- carlength >  162.95
## |   |   |   |   |   |   |   |   |   |--- value: [6695.00]
## |   |   |   |   |   |   |   |--- citympg >  34.00
## |   |   |   |   |   |   |   |   |--- peakrpm <= 5150.00
## |   |   |   |   |   |   |   |   |   |--- value: [6479.00]
## |   |   |   |   |   |   |   |   |--- peakrpm >  5150.00
## |   |   |   |   |   |   |   |   |   |--- enginesize <= 91.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [5572.00]
## |   |   |   |   |   |   |   |   |   |--- enginesize >  91.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [5389.00]
## |   |   |   |   |--- curbweight >  1987.00
## |   |   |   |   |   |--- stroke <= 3.13
## |   |   |   |   |   |   |--- curbweight <= 2027.50
## |   |   |   |   |   |   |   |--- value: [6488.00]
## |   |   |   |   |   |   |--- curbweight >  2027.50
## |   |   |   |   |   |   |   |--- value: [6338.00]
## |   |   |   |   |   |--- stroke >  3.13
## |   |   |   |   |   |   |--- curbweight <= 2104.00
## |   |   |   |   |   |   |   |--- carheight <= 50.70
## |   |   |   |   |   |   |   |   |--- value: [7150.50]
## |   |   |   |   |   |   |   |--- carheight >  50.70
## |   |   |   |   |   |   |   |   |--- horsepower <= 61.50
## |   |   |   |   |   |   |   |   |   |--- value: [7099.00]
## |   |   |   |   |   |   |   |   |--- horsepower >  61.50
## |   |   |   |   |   |   |   |   |   |--- carlength <= 162.30
## |   |   |   |   |   |   |   |   |   |   |--- value: [6669.00]
## |   |   |   |   |   |   |   |   |   |--- carlength >  162.30
## |   |   |   |   |   |   |   |   |   |   |--- value: [6692.00]
## |   |   |   |   |   |   |--- curbweight >  2104.00
## |   |   |   |   |   |   |   |--- value: [7609.00]
## |   |   |   |--- horsepower >  68.50
## |   |   |   |   |--- carwidth <= 63.50
## |   |   |   |   |   |--- value: [5118.00]
## |   |   |   |   |--- carwidth >  63.50
## |   |   |   |   |   |--- curbweight <= 2124.00
## |   |   |   |   |   |   |--- carheight <= 53.60
## |   |   |   |   |   |   |   |--- carwidth <= 63.85
## |   |   |   |   |   |   |   |   |--- carlength <= 157.35
## |   |   |   |   |   |   |   |   |   |--- symboling <= 0.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [8916.50]
## |   |   |   |   |   |   |   |   |   |--- symboling >  0.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [7605.75]
## |   |   |   |   |   |   |   |   |--- carlength >  157.35
## |   |   |   |   |   |   |   |   |   |--- symboling <= 0.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [6575.00]
## |   |   |   |   |   |   |   |   |   |--- symboling >  0.50
## |   |   |   |   |   |   |   |   |   |   |--- wheelbase <= 94.80
## |   |   |   |   |   |   |   |   |   |   |   |--- truncated branch of depth 3
## |   |   |   |   |   |   |   |   |   |   |--- wheelbase >  94.80
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [8249.00]
## |   |   |   |   |   |   |   |--- carwidth >  63.85
## |   |   |   |   |   |   |   |   |--- highwaympg <= 42.50
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 1948.00
## |   |   |   |   |   |   |   |   |   |   |--- carlength <= 147.30
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [6855.00]
## |   |   |   |   |   |   |   |   |   |   |--- carlength >  147.30
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [6529.00]
## |   |   |   |   |   |   |   |   |   |--- curbweight >  1948.00
## |   |   |   |   |   |   |   |   |   |   |--- carheight <= 52.90
## |   |   |   |   |   |   |   |   |   |   |   |--- truncated branch of depth 2
## |   |   |   |   |   |   |   |   |   |   |--- carheight >  52.90
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [6938.00]
## |   |   |   |   |   |   |   |   |--- highwaympg >  42.50
## |   |   |   |   |   |   |   |   |   |--- value: [7738.00]
## |   |   |   |   |   |   |--- carheight >  53.60
## |   |   |   |   |   |   |   |--- curbweight <= 1903.50
## |   |   |   |   |   |   |   |   |--- value: [5499.00]
## |   |   |   |   |   |   |   |--- curbweight >  1903.50
## |   |   |   |   |   |   |   |   |--- curbweight <= 1944.50
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 1928.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [6649.00]
## |   |   |   |   |   |   |   |   |   |--- curbweight >  1928.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [6849.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  1944.50
## |   |   |   |   |   |   |   |   |   |--- stroke <= 2.97
## |   |   |   |   |   |   |   |   |   |   |--- value: [7053.00]
## |   |   |   |   |   |   |   |   |   |--- stroke >  2.97
## |   |   |   |   |   |   |   |   |   |   |--- highwaympg <= 35.50
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [7295.00]
## |   |   |   |   |   |   |   |   |   |   |--- highwaympg >  35.50
## |   |   |   |   |   |   |   |   |   |   |   |--- truncated branch of depth 2
## |   |   |   |   |   |--- curbweight >  2124.00
## |   |   |   |   |   |   |--- horsepower <= 76.00
## |   |   |   |   |   |   |   |--- carheight <= 52.70
## |   |   |   |   |   |   |   |   |--- value: [8238.00]
## |   |   |   |   |   |   |   |--- carheight >  52.70
## |   |   |   |   |   |   |   |   |--- value: [9258.00]
## |   |   |   |   |   |   |--- horsepower >  76.00
## |   |   |   |   |   |   |   |--- citympg <= 30.00
## |   |   |   |   |   |   |   |   |--- curbweight <= 2210.50
## |   |   |   |   |   |   |   |   |   |--- symboling <= 0.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [7775.00]
## |   |   |   |   |   |   |   |   |   |--- symboling >  0.50
## |   |   |   |   |   |   |   |   |   |   |--- carwidth <= 64.65
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [7957.00]
## |   |   |   |   |   |   |   |   |   |   |--- carwidth >  64.65
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [7975.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  2210.50
## |   |   |   |   |   |   |   |   |   |--- value: [8195.00]
## |   |   |   |   |   |   |   |--- citympg >  30.00
## |   |   |   |   |   |   |   |   |--- value: [7126.00]
## |   |   |--- curbweight >  2216.50
## |   |   |   |--- citympg <= 21.00
## |   |   |   |   |--- enginesize <= 75.00
## |   |   |   |   |   |--- value: [11395.00]
## |   |   |   |   |--- enginesize >  75.00
## |   |   |   |   |   |--- boreratio <= 3.26
## |   |   |   |   |   |   |--- value: [15250.00]
## |   |   |   |   |   |--- boreratio >  3.26
## |   |   |   |   |   |   |--- value: [15645.00]
## |   |   |   |--- citympg >  21.00
## |   |   |   |   |--- horsepower <= 89.00
## |   |   |   |   |   |--- carwidth <= 66.00
## |   |   |   |   |   |   |--- horsepower <= 80.00
## |   |   |   |   |   |   |   |--- curbweight <= 2277.50
## |   |   |   |   |   |   |   |   |--- curbweight <= 2250.50
## |   |   |   |   |   |   |   |   |   |--- value: [7603.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  2250.50
## |   |   |   |   |   |   |   |   |   |--- peakrpm <= 4650.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [7788.00]
## |   |   |   |   |   |   |   |   |   |--- peakrpm >  4650.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [7775.00]
## |   |   |   |   |   |   |   |--- curbweight >  2277.50
## |   |   |   |   |   |   |   |   |--- horsepower <= 70.00
## |   |   |   |   |   |   |   |   |   |--- value: [6918.00]
## |   |   |   |   |   |   |   |   |--- horsepower >  70.00
## |   |   |   |   |   |   |   |   |   |--- value: [6785.00]
## |   |   |   |   |   |   |--- horsepower >  80.00
## |   |   |   |   |   |   |   |--- carheight <= 53.15
## |   |   |   |   |   |   |   |   |--- carheight <= 50.50
## |   |   |   |   |   |   |   |   |   |--- value: [8499.00]
## |   |   |   |   |   |   |   |   |--- carheight >  50.50
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 2385.00
## |   |   |   |   |   |   |   |   |   |   |--- carheight <= 52.30
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [6989.00]
## |   |   |   |   |   |   |   |   |   |   |--- carheight >  52.30
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [7463.00]
## |   |   |   |   |   |   |   |   |   |--- curbweight >  2385.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [8189.00]
## |   |   |   |   |   |   |   |--- carheight >  53.15
## |   |   |   |   |   |   |   |   |--- curbweight <= 2255.50
## |   |   |   |   |   |   |   |   |   |--- value: [7895.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  2255.50
## |   |   |   |   |   |   |   |   |   |--- citympg <= 23.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [8013.00]
## |   |   |   |   |   |   |   |   |   |--- citympg >  23.50
## |   |   |   |   |   |   |   |   |   |   |--- curbweight <= 2282.00
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [8495.00]
## |   |   |   |   |   |   |   |   |   |   |--- curbweight >  2282.00
## |   |   |   |   |   |   |   |   |   |   |   |--- truncated branch of depth 3
## |   |   |   |   |   |--- carwidth >  66.00
## |   |   |   |   |   |   |--- curbweight <= 2417.50
## |   |   |   |   |   |   |   |--- carheight <= 54.60
## |   |   |   |   |   |   |   |   |--- value: [8845.00]
## |   |   |   |   |   |   |   |--- carheight >  54.60
## |   |   |   |   |   |   |   |   |--- value: [9370.00]
## |   |   |   |   |   |   |--- curbweight >  2417.50
## |   |   |   |   |   |   |   |--- citympg <= 28.00
## |   |   |   |   |   |   |   |   |--- value: [11245.00]
## |   |   |   |   |   |   |   |--- citympg >  28.00
## |   |   |   |   |   |   |   |   |--- horsepower <= 68.50
## |   |   |   |   |   |   |   |   |   |--- value: [10795.00]
## |   |   |   |   |   |   |   |   |--- horsepower >  68.50
## |   |   |   |   |   |   |   |   |   |--- value: [10698.00]
## |   |   |   |   |--- horsepower >  89.00
## |   |   |   |   |   |--- carheight <= 54.00
## |   |   |   |   |   |   |--- curbweight <= 2538.00
## |   |   |   |   |   |   |   |--- horsepower <= 103.00
## |   |   |   |   |   |   |   |   |--- enginesize <= 127.00
## |   |   |   |   |   |   |   |   |   |--- stroke <= 3.02
## |   |   |   |   |   |   |   |   |   |   |--- value: [9960.00]
## |   |   |   |   |   |   |   |   |   |--- stroke >  3.02
## |   |   |   |   |   |   |   |   |   |   |--- highwaympg <= 30.50
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [9980.00]
## |   |   |   |   |   |   |   |   |   |   |--- highwaympg >  30.50
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [9988.00]
## |   |   |   |   |   |   |   |   |--- enginesize >  127.00
## |   |   |   |   |   |   |   |   |   |--- value: [9895.00]
## |   |   |   |   |   |   |   |--- horsepower >  103.00
## |   |   |   |   |   |   |   |   |--- curbweight <= 2469.50
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 2351.50
## |   |   |   |   |   |   |   |   |   |   |--- curbweight <= 2282.50
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [9298.00]
## |   |   |   |   |   |   |   |   |   |   |--- curbweight >  2282.50
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [9538.00]
## |   |   |   |   |   |   |   |   |   |--- curbweight >  2351.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [9279.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  2469.50
## |   |   |   |   |   |   |   |   |   |--- value: [9639.00]
## |   |   |   |   |   |   |--- curbweight >  2538.00
## |   |   |   |   |   |   |   |--- value: [8449.00]
## |   |   |   |   |   |--- carheight >  54.00
## |   |   |   |   |   |   |--- highwaympg <= 31.00
## |   |   |   |   |   |   |   |--- carlength <= 173.70
## |   |   |   |   |   |   |   |   |--- horsepower <= 100.50
## |   |   |   |   |   |   |   |   |   |--- value: [11595.00]
## |   |   |   |   |   |   |   |   |--- horsepower >  100.50
## |   |   |   |   |   |   |   |   |   |--- value: [11259.00]
## |   |   |   |   |   |   |   |--- carlength >  173.70
## |   |   |   |   |   |   |   |   |--- stroke <= 3.49
## |   |   |   |   |   |   |   |   |   |--- value: [13950.00]
## |   |   |   |   |   |   |   |   |--- stroke >  3.49
## |   |   |   |   |   |   |   |   |   |--- value: [12945.00]
## |   |   |   |   |   |   |--- highwaympg >  31.00
## |   |   |   |   |   |   |   |--- highwaympg <= 33.00
## |   |   |   |   |   |   |   |   |--- symboling <= 0.50
## |   |   |   |   |   |   |   |   |   |--- value: [10898.00]
## |   |   |   |   |   |   |   |   |--- symboling >  0.50
## |   |   |   |   |   |   |   |   |   |--- value: [9995.00]
## |   |   |   |   |   |   |   |--- highwaympg >  33.00
## |   |   |   |   |   |   |   |   |--- curbweight <= 2313.00
## |   |   |   |   |   |   |   |   |   |--- value: [9549.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  2313.00
## |   |   |   |   |   |   |   |   |   |--- peakrpm <= 4700.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [8948.00]
## |   |   |   |   |   |   |   |   |   |--- peakrpm >  4700.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [8949.00]
## |   |--- curbweight >  2544.00
## |   |   |--- carwidth <= 68.60
## |   |   |   |--- horsepower <= 118.50
## |   |   |   |   |--- horsepower <= 92.50
## |   |   |   |   |   |--- carwidth <= 66.70
## |   |   |   |   |   |   |--- compressionratio <= 9.10
## |   |   |   |   |   |   |   |--- carlength <= 175.60
## |   |   |   |   |   |   |   |   |--- value: [8778.00]
## |   |   |   |   |   |   |   |--- carlength >  175.60
## |   |   |   |   |   |   |   |   |--- value: [9295.00]
## |   |   |   |   |   |   |--- compressionratio >  9.10
## |   |   |   |   |   |   |   |--- value: [11048.00]
## |   |   |   |   |   |--- carwidth >  66.70
## |   |   |   |   |   |   |--- horsepower <= 78.00
## |   |   |   |   |   |   |   |--- value: [13845.00]
## |   |   |   |   |   |   |--- horsepower >  78.00
## |   |   |   |   |   |   |   |--- value: [12290.00]
## |   |   |   |   |--- horsepower >  92.50
## |   |   |   |   |   |--- curbweight <= 2701.00
## |   |   |   |   |   |   |--- boreratio <= 3.50
## |   |   |   |   |   |   |   |--- peakrpm <= 5250.00
## |   |   |   |   |   |   |   |   |--- value: [14997.50]
## |   |   |   |   |   |   |   |--- peakrpm >  5250.00
## |   |   |   |   |   |   |   |   |--- value: [13295.00]
## |   |   |   |   |   |   |--- boreratio >  3.50
## |   |   |   |   |   |   |   |--- carheight <= 53.45
## |   |   |   |   |   |   |   |   |--- value: [11199.00]
## |   |   |   |   |   |   |   |--- carheight >  53.45
## |   |   |   |   |   |   |   |   |--- curbweight <= 2676.50
## |   |   |   |   |   |   |   |   |   |--- citympg <= 22.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [11850.00]
## |   |   |   |   |   |   |   |   |   |--- citympg >  22.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [11694.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  2676.50
## |   |   |   |   |   |   |   |   |   |--- value: [12170.00]
## |   |   |   |   |   |--- curbweight >  2701.00
## |   |   |   |   |   |   |--- carlength <= 181.60
## |   |   |   |   |   |   |   |--- stroke <= 3.45
## |   |   |   |   |   |   |   |   |--- value: [17450.00]
## |   |   |   |   |   |   |   |--- stroke >  3.45
## |   |   |   |   |   |   |   |   |--- value: [17669.00]
## |   |   |   |   |   |   |--- carlength >  181.60
## |   |   |   |   |   |   |   |--- curbweight <= 3038.00
## |   |   |   |   |   |   |   |   |--- curbweight <= 2977.50
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 2923.50
## |   |   |   |   |   |   |   |   |   |   |--- symboling <= 0.00
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [12940.00]
## |   |   |   |   |   |   |   |   |   |   |--- symboling >  0.00
## |   |   |   |   |   |   |   |   |   |   |   |--- truncated branch of depth 2
## |   |   |   |   |   |   |   |   |   |--- curbweight >  2923.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [15985.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  2977.50
## |   |   |   |   |   |   |   |   |   |--- enginesize <= 130.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [11900.00]
## |   |   |   |   |   |   |   |   |   |--- enginesize >  130.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [13415.00]
## |   |   |   |   |   |   |   |--- curbweight >  3038.00
## |   |   |   |   |   |   |   |   |--- curbweight <= 3224.50
## |   |   |   |   |   |   |   |   |   |--- peakrpm <= 4575.00
## |   |   |   |   |   |   |   |   |   |   |--- value: [13200.00]
## |   |   |   |   |   |   |   |   |   |--- peakrpm >  4575.00
## |   |   |   |   |   |   |   |   |   |   |--- highwaympg <= 26.00
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [16630.00]
## |   |   |   |   |   |   |   |   |   |   |--- highwaympg >  26.00
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [16515.00]
## |   |   |   |   |   |   |   |   |--- curbweight >  3224.50
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 3357.50
## |   |   |   |   |   |   |   |   |   |   |--- compressionratio <= 14.70
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [16695.00]
## |   |   |   |   |   |   |   |   |   |   |--- compressionratio >  14.70
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [17425.00]
## |   |   |   |   |   |   |   |   |   |--- curbweight >  3357.50
## |   |   |   |   |   |   |   |   |   |   |--- curbweight <= 3457.50
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [13860.00]
## |   |   |   |   |   |   |   |   |   |   |--- curbweight >  3457.50
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [17075.00]
## |   |   |   |--- horsepower >  118.50
## |   |   |   |   |--- horsepower <= 131.50
## |   |   |   |   |   |--- wheelbase <= 102.35
## |   |   |   |   |   |   |--- curbweight <= 2737.50
## |   |   |   |   |   |   |   |--- value: [20970.00]
## |   |   |   |   |   |   |--- curbweight >  2737.50
## |   |   |   |   |   |   |   |--- value: [21105.00]
## |   |   |   |   |   |--- wheelbase >  102.35
## |   |   |   |   |   |   |--- value: [24565.00]
## |   |   |   |   |--- horsepower >  131.50
## |   |   |   |   |   |--- horsepower <= 158.00
## |   |   |   |   |   |   |--- curbweight <= 3112.50
## |   |   |   |   |   |   |   |--- boreratio <= 3.59
## |   |   |   |   |   |   |   |   |--- carlength <= 177.45
## |   |   |   |   |   |   |   |   |   |--- curbweight <= 2923.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [14869.00]
## |   |   |   |   |   |   |   |   |   |--- curbweight >  2923.50
## |   |   |   |   |   |   |   |   |   |   |--- value: [14489.00]
## |   |   |   |   |   |   |   |   |--- carlength >  177.45
## |   |   |   |   |   |   |   |   |   |--- value: [13499.00]
## |   |   |   |   |   |   |   |--- boreratio >  3.59
## |   |   |   |   |   |   |   |   |--- value: [12964.00]
## |   |   |   |   |   |   |--- curbweight >  3112.50
## |   |   |   |   |   |   |   |--- carheight <= 55.05
## |   |   |   |   |   |   |   |   |--- enginesize <= 166.00
## |   |   |   |   |   |   |   |   |   |--- value: [15750.00]
## |   |   |   |   |   |   |   |   |--- enginesize >  166.00
## |   |   |   |   |   |   |   |   |   |--- value: [15690.00]
## |   |   |   |   |   |   |   |--- carheight >  55.05
## |   |   |   |   |   |   |   |   |--- value: [18150.00]
## |   |   |   |   |   |--- horsepower >  158.00
## |   |   |   |   |   |   |--- compressionratio <= 9.15
## |   |   |   |   |   |   |   |--- carlength <= 174.45
## |   |   |   |   |   |   |   |   |--- value: [19699.00]
## |   |   |   |   |   |   |   |--- carlength >  174.45
## |   |   |   |   |   |   |   |   |--- curbweight <= 3148.00
## |   |   |   |   |   |   |   |   |   |--- peakrpm <= 5350.00
## |   |   |   |   |   |   |   |   |   |   |--- curbweight <= 3092.00
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [18420.00]
## |   |   |   |   |   |   |   |   |   |   |--- curbweight >  3092.00
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [18399.00]
## |   |   |   |   |   |   |   |   |   |--- peakrpm >  5350.00
## |   |   |   |   |   |   |   |   |   |   |--- stroke <= 3.24
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [18150.00]
## |   |   |   |   |   |   |   |   |   |   |--- stroke >  3.24
## |   |   |   |   |   |   |   |   |   |   |   |--- value: [17859.17]
## |   |   |   |   |   |   |   |   |--- curbweight >  3148.00
## |   |   |   |   |   |   |   |   |   |--- value: [18950.00]
## |   |   |   |   |   |   |--- compressionratio >  9.15
## |   |   |   |   |   |   |   |--- citympg <= 19.50
## |   |   |   |   |   |   |   |   |--- value: [15998.00]
## |   |   |   |   |   |   |   |--- citympg >  19.50
## |   |   |   |   |   |   |   |   |--- value: [16558.00]
## |   |   |--- carwidth >  68.60
## |   |   |   |--- curbweight <= 3055.50
## |   |   |   |   |--- enginesize <= 157.00
## |   |   |   |   |   |--- peakrpm <= 5350.00
## |   |   |   |   |   |   |--- value: [19045.00]
## |   |   |   |   |   |--- peakrpm >  5350.00
## |   |   |   |   |   |   |--- value: [16845.00]
## |   |   |   |   |--- enginesize >  157.00
## |   |   |   |   |   |--- value: [21485.00]
## |   |   |   |--- curbweight >  3055.50
## |   |   |   |   |--- compressionratio <= 8.90
## |   |   |   |   |   |--- value: [23875.00]
## |   |   |   |   |--- compressionratio >  8.90
## |   |   |   |   |   |--- horsepower <= 110.00
## |   |   |   |   |   |   |--- value: [22470.00]
## |   |   |   |   |   |--- horsepower >  110.00
## |   |   |   |   |   |   |--- value: [22625.00]
## |--- enginesize >  182.00
## |   |--- highwaympg <= 16.50
## |   |   |--- value: [45400.00]
## |   |--- highwaympg >  16.50
## |   |   |--- compressionratio <= 16.50
## |   |   |   |--- carwidth <= 72.00
## |   |   |   |   |--- curbweight <= 4008.00
## |   |   |   |   |   |--- curbweight <= 2778.00
## |   |   |   |   |   |   |--- value: [33278.00]
## |   |   |   |   |   |--- curbweight >  2778.00
## |   |   |   |   |   |   |--- boreratio <= 3.50
## |   |   |   |   |   |   |   |--- carwidth <= 71.10
## |   |   |   |   |   |   |   |   |--- value: [35056.00]
## |   |   |   |   |   |   |   |--- carwidth >  71.10
## |   |   |   |   |   |   |   |   |--- value: [34184.00]
## |   |   |   |   |   |   |--- boreratio >  3.50
## |   |   |   |   |   |   |   |--- enginesize <= 267.50
## |   |   |   |   |   |   |   |   |--- boreratio <= 3.68
## |   |   |   |   |   |   |   |   |   |--- value: [36880.00]
## |   |   |   |   |   |   |   |   |--- boreratio >  3.68
## |   |   |   |   |   |   |   |   |   |--- value: [37028.00]
## |   |   |   |   |   |   |   |--- enginesize >  267.50
## |   |   |   |   |   |   |   |   |--- value: [36000.00]
## |   |   |   |   |--- curbweight >  4008.00
## |   |   |   |   |   |--- value: [32250.00]
## |   |   |   |--- carwidth >  72.00
## |   |   |   |   |--- value: [31400.50]
## |   |   |--- compressionratio >  16.50
## |   |   |   |--- carlength <= 196.75
## |   |   |   |   |--- curbweight <= 3632.50
## |   |   |   |   |   |--- curbweight <= 3505.00
## |   |   |   |   |   |   |--- value: [28176.00]
## |   |   |   |   |   |--- curbweight >  3505.00
## |   |   |   |   |   |   |--- value: [25552.00]
## |   |   |   |   |--- curbweight >  3632.50
## |   |   |   |   |   |--- value: [28248.00]
## |   |   |   |--- carlength >  196.75
## |   |   |   |   |--- value: [31600.00]

3.6.2.2 Importancia de los predictores

importancia_predictores = pd.DataFrame(
                            {'predictor': datos.drop(columns = "price").columns, 
                            'importancia': modelo_ar.feature_importances_}
                            )
                            
print("Importancia de los predictores en el modelo")
## Importancia de los predictores en el modelo
importancia_predictores.sort_values('importancia', ascending=False)
##            predictor  importancia
## 6         enginesize     0.654449
## 5         curbweight     0.243935
## 10        horsepower     0.034996
## 3           carwidth     0.021673
## 13        highwaympg     0.017511
## 9   compressionratio     0.011175
## 12           citympg     0.006409
## 2          carlength     0.003051
## 7          boreratio     0.001889
## 4          carheight     0.001851
## 11           peakrpm     0.001283
## 1          wheelbase     0.000855
## 0          symboling     0.000666
## 8             stroke     0.000258

Estos sería los predictores más importantes para el modelo de árbol de regresión enginesize, curbweight, peakrpm, carheight y wheelbase

3.6.2.3 Predicciones del modelo (ar)

predicciones_ar = modelo_ar.predict(X = X_valida)
predicciones_ar
## array([ 6918.,  9960., 19699., 18150.,  8845.,  8238., 14869., 16845.,
##        11199., 36880., 36880., 45400.,  6918., 16695.,  6095., 16630.,
##         9279., 13499., 18420., 13950.,  6695.,  8845., 11395., 11048.,
##        18150.,  7198.,  7788., 13950., 17669., 14869.,  9988.,  7775.,
##         5572., 14869., 12964., 16845., 24565., 32250.,  9988.,  7957.,
##         7957.])

3.6.2.4 Tabla comparativa

comparaciones = pd.DataFrame(X_valida)
comparaciones = comparaciones.assign(Precio_Real = Y_valida)
comparaciones = comparaciones.assign(Precio_Prediccion = predicciones_ar.flatten().tolist())
print(comparaciones)
##      symboling  wheelbase  ...  Precio_Real  Precio_Prediccion
## 154          0       95.7  ...       7898.0             6918.0
## 147          0       97.0  ...      10198.0             9960.0
## 104          3       91.3  ...      17199.0            19699.0
## 102          0      100.4  ...      14399.0            18150.0
## 61           1       98.8  ...      10595.0             8845.0
## 163          1       94.5  ...       8058.0             8238.0
## 124          3       95.9  ...      12764.0            14869.0
## 7            1      105.8  ...      18920.0            16845.0
## 169          2       98.4  ...       9989.0            11199.0
## 16           0      103.5  ...      41315.0            36880.0
## 15           0      103.5  ...      30760.0            36880.0
## 73           0      120.9  ...      40960.0            45400.0
## 187          2       97.3  ...       9495.0             6918.0
## 109          0      114.2  ...      12440.0            16695.0
## 52           1       93.1  ...       6795.0             6095.0
## 111          0      107.9  ...      15580.0            16630.0
## 80           3       96.3  ...       9959.0             9279.0
## 103          0      100.4  ...      13499.0            13499.0
## 75           1      102.7  ...      16503.0            18420.0
## 10           2      101.2  ...      16430.0            13950.0
## 54           1       93.1  ...       7395.0             6695.0
## 40           0       96.5  ...      10295.0             8845.0
## 57           3       95.3  ...      13645.0            11395.0
## 66           0      104.9  ...      18344.0            11048.0
## 137          2       99.1  ...      18620.0            18150.0
## 161          0       95.7  ...       8358.0             7198.0
## 158          0       95.7  ...       7898.0             7788.0
## 11           0      101.2  ...      16925.0            13950.0
## 171          2       98.4  ...      11549.0            17669.0
## 2            1       94.5  ...      16500.0            14869.0
## 177         -1      102.4  ...      11248.0             9988.0
## 184          2       97.3  ...       7995.0             7775.0
## 21           1       93.7  ...       5572.0             5572.0
## 82           3       95.9  ...      12629.0            14869.0
## 125          3       94.5  ...      22018.0            12964.0
## 6            1      105.8  ...      17710.0            16845.0
## 65           0      104.9  ...      18280.0            24565.0
## 48           0      113.0  ...      35550.0            32250.0
## 42           1       96.5  ...      10345.0             9988.0
## 27           1       93.7  ...       8558.0             7957.0
## 79           1       93.0  ...       7689.0             7957.0
## 
## [41 rows x 16 columns]

3.6.2.5 RMSE modelo de ar

rmse_ar = mean_squared_error(
        y_true  = Y_valida,
        y_pred  = predicciones_ar,
        squared = False
       )
print(f"El error (rmse) de test es: {rmse_ar}")
## El error (rmse) de test es: 3122.9086855905

o

print('Root Mean Squared Error RMSE:', np.sqrt(metrics.mean_squared_error(Y_valida, predicciones_ar)))
## Root Mean Squared Error RMSE: 3122.9086855905

3.6.2.6 Modelo de bosques aleatorios (RF)

Se construye el modelo de árbol de regresión (ar). Semilla 1349 y 20 árboles de entrenamiento

modelo_rf = RandomForestRegressor(n_estimators = 20, random_state = 1349)

modelo_rf.fit(X_entrena, Y_entrena)
RandomForestRegressor(n_estimators=20, random_state=1349)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

3.6.2.7 Variables de importancia

# pendiente ... ...

3.6.2.8 Predicciones del modelo (rf)

predicciones_rf = modelo_rf.predict(X_valida)
predicciones_rf
## array([ 7647.6       , 10356.75      , 18849.95      , 16501.4       ,
##         8928.81666667,  8420.05      , 14567.55835   , 21848.5       ,
##        10358.4       , 33807.5       , 33807.5       , 40826.1       ,
##         8407.35      , 17151.        ,  6008.7       , 15583.6       ,
##        10601.25      , 16168.55      , 17880.8167    , 10670.4       ,
##         6672.7       ,  8652.        , 11317.15      , 12503.2       ,
##        17130.50835   ,  7990.1       ,  7881.        , 10098.75      ,
##        13202.6       , 15045.77501667, 10447.85      ,  8324.3       ,
##         5983.89166667, 14567.55835   , 15538.00835   , 21848.5       ,
##        16832.725     , 32259.2       ,  9695.95      ,  8033.89166667,
##         8051.59166667])

3.6.2.9 Tabla comparativa


comparaciones = pd.DataFrame(X_valida)
comparaciones = comparaciones.assign(Precio_Real = Y_valida)
comparaciones = comparaciones.assign(Precio_Prediccion = predicciones_rf.flatten().tolist())
print(comparaciones)
##      symboling  wheelbase  ...  Precio_Real  Precio_Prediccion
## 154          0       95.7  ...       7898.0        7647.600000
## 147          0       97.0  ...      10198.0       10356.750000
## 104          3       91.3  ...      17199.0       18849.950000
## 102          0      100.4  ...      14399.0       16501.400000
## 61           1       98.8  ...      10595.0        8928.816667
## 163          1       94.5  ...       8058.0        8420.050000
## 124          3       95.9  ...      12764.0       14567.558350
## 7            1      105.8  ...      18920.0       21848.500000
## 169          2       98.4  ...       9989.0       10358.400000
## 16           0      103.5  ...      41315.0       33807.500000
## 15           0      103.5  ...      30760.0       33807.500000
## 73           0      120.9  ...      40960.0       40826.100000
## 187          2       97.3  ...       9495.0        8407.350000
## 109          0      114.2  ...      12440.0       17151.000000
## 52           1       93.1  ...       6795.0        6008.700000
## 111          0      107.9  ...      15580.0       15583.600000
## 80           3       96.3  ...       9959.0       10601.250000
## 103          0      100.4  ...      13499.0       16168.550000
## 75           1      102.7  ...      16503.0       17880.816700
## 10           2      101.2  ...      16430.0       10670.400000
## 54           1       93.1  ...       7395.0        6672.700000
## 40           0       96.5  ...      10295.0        8652.000000
## 57           3       95.3  ...      13645.0       11317.150000
## 66           0      104.9  ...      18344.0       12503.200000
## 137          2       99.1  ...      18620.0       17130.508350
## 161          0       95.7  ...       8358.0        7990.100000
## 158          0       95.7  ...       7898.0        7881.000000
## 11           0      101.2  ...      16925.0       10098.750000
## 171          2       98.4  ...      11549.0       13202.600000
## 2            1       94.5  ...      16500.0       15045.775017
## 177         -1      102.4  ...      11248.0       10447.850000
## 184          2       97.3  ...       7995.0        8324.300000
## 21           1       93.7  ...       5572.0        5983.891667
## 82           3       95.9  ...      12629.0       14567.558350
## 125          3       94.5  ...      22018.0       15538.008350
## 6            1      105.8  ...      17710.0       21848.500000
## 65           0      104.9  ...      18280.0       16832.725000
## 48           0      113.0  ...      35550.0       32259.200000
## 42           1       96.5  ...      10345.0        9695.950000
## 27           1       93.7  ...       8558.0        8033.891667
## 79           1       93.0  ...       7689.0        8051.591667
## 
## [41 rows x 16 columns]

3.6.2.10 RMSE modelo de ar

rmse_rf = mean_squared_error(
        y_true  = Y_valida,
        y_pred  = predicciones_rf,
        squared = False
       )
print(f"El error (rmse) de test es: {rmse_rf}")
## El error (rmse) de test es: 2830.497793616688

o

print('Root Mean Squared Error RMSE:', np.sqrt(metrics.mean_squared_error(Y_valida, predicciones_rf)))
## Root Mean Squared Error RMSE: 2830.497793616688

3.7 Evaluación de modelos

Se comparan las predicciones

comparaciones = pd.DataFrame(X_valida)
comparaciones = comparaciones.assign(Precio_Real = Y_valida)
comparaciones = comparaciones.assign(Precio_Prediccion_rm = predicciones_rm.flatten().tolist(), Precio_Prediccion_ar = predicciones_ar.flatten().tolist(), Precio_Prediccion_rf = predicciones_rf.flatten().tolist())
print(comparaciones)
##      symboling  wheelbase  ...  Precio_Prediccion_ar  Precio_Prediccion_rf
## 154          0       95.7  ...                6918.0           7647.600000
## 147          0       97.0  ...                9960.0          10356.750000
## 104          3       91.3  ...               19699.0          18849.950000
## 102          0      100.4  ...               18150.0          16501.400000
## 61           1       98.8  ...                8845.0           8928.816667
## 163          1       94.5  ...                8238.0           8420.050000
## 124          3       95.9  ...               14869.0          14567.558350
## 7            1      105.8  ...               16845.0          21848.500000
## 169          2       98.4  ...               11199.0          10358.400000
## 16           0      103.5  ...               36880.0          33807.500000
## 15           0      103.5  ...               36880.0          33807.500000
## 73           0      120.9  ...               45400.0          40826.100000
## 187          2       97.3  ...                6918.0           8407.350000
## 109          0      114.2  ...               16695.0          17151.000000
## 52           1       93.1  ...                6095.0           6008.700000
## 111          0      107.9  ...               16630.0          15583.600000
## 80           3       96.3  ...                9279.0          10601.250000
## 103          0      100.4  ...               13499.0          16168.550000
## 75           1      102.7  ...               18420.0          17880.816700
## 10           2      101.2  ...               13950.0          10670.400000
## 54           1       93.1  ...                6695.0           6672.700000
## 40           0       96.5  ...                8845.0           8652.000000
## 57           3       95.3  ...               11395.0          11317.150000
## 66           0      104.9  ...               11048.0          12503.200000
## 137          2       99.1  ...               18150.0          17130.508350
## 161          0       95.7  ...                7198.0           7990.100000
## 158          0       95.7  ...                7788.0           7881.000000
## 11           0      101.2  ...               13950.0          10098.750000
## 171          2       98.4  ...               17669.0          13202.600000
## 2            1       94.5  ...               14869.0          15045.775017
## 177         -1      102.4  ...                9988.0          10447.850000
## 184          2       97.3  ...                7775.0           8324.300000
## 21           1       93.7  ...                5572.0           5983.891667
## 82           3       95.9  ...               14869.0          14567.558350
## 125          3       94.5  ...               12964.0          15538.008350
## 6            1      105.8  ...               16845.0          21848.500000
## 65           0      104.9  ...               24565.0          16832.725000
## 48           0      113.0  ...               32250.0          32259.200000
## 42           1       96.5  ...                9988.0           9695.950000
## 27           1       93.7  ...                7957.0           8033.891667
## 79           1       93.0  ...                7957.0           8051.591667
## 
## [41 rows x 18 columns]

Se compara el RMSE.

Se crea un arreglo numpy

rmse = np.array([[rmse_rm, rmse_ar, rmse_rf]])
rmse
## array([[4075.08099952, 3122.90868559, 2830.49779362]])

Se construye data.frame a partir del rreglo nmpy


rmse = pd.DataFrame(rmse)
rmse.columns = ['rmse_rm', 'rmse_ar', 'rmse_rf']
rmse
##     rmse_rm      rmse_ar      rmse_rf
## 0  4075.081  3122.908686  2830.497794

4 Interpretación

En el presente ejercicio se realizo una cargade datos numéricos de precios de automóviles con respecto a algunas variables numéricas mediante un enlace de Github en formato CSV. Se cargaron datos numéricos de precios de automóviles basados en algunas variables numéricas.

El modelo de regresión linea múltiple destaca el estadístico Adjusted R-squared con un valor de 0.8686632, lo que se define como que las variables independientes explican aproximadamente el 86.86% de la variable dependiente precio.

El mejor modelo conforme al estadístico raiz del error cuadrático medio (rmse) fue bosques aleatorios; se tuvo como resultado un de 4075.0809.

Se construyeron datos de entrenamiento y validación y con el porcentaje de 80% y 20% respectivamente.

El RMSE del modelo de regresión lineal es de 4075.0809.

El RMSE del modelo de árbol de regresión es de 3122.9086.

El RMSE del modelo de bosques aleatorios es de 2830.497794.

Los datos obtenidos mostrados anteriormente fueron realizados utilizando la semilla 1349