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).
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
# 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
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]
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
| 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~
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]
Datos de entrenamiento al 80% de los datos y 20% los datos de validación. Semilla 2022
X_entrena, X_valida, Y_entrena, Y_valida = train_test_split(datos.drop(columns = "price"), datos['price'],train_size = 0.80, random_state = 1301)
X_entrena
## symboling wheelbase carlength ... peakrpm citympg highwaympg
## 14 1 103.5 189.0 ... 4250 20 25
## 62 0 98.8 177.8 ... 4800 26 32
## 97 1 94.5 170.2 ... 5200 31 37
## 133 2 99.1 186.6 ... 5250 21 28
## 178 3 102.9 183.5 ... 5200 20 24
## .. ... ... ... ... ... ... ...
## 177 -1 102.4 175.6 ... 4200 27 32
## 81 3 96.3 173.0 ... 5000 25 32
## 180 -1 104.5 187.8 ... 5200 20 24
## 110 0 114.2 198.9 ... 4150 25 25
## 171 2 98.4 176.2 ... 4800 24 30
##
## [164 rows x 14 columns]
X_valida
## symboling wheelbase carlength ... peakrpm citympg highwaympg
## 46 2 96.0 172.6 ... 5000 24 29
## 113 0 114.2 198.9 ... 5000 19 24
## 167 2 98.4 176.2 ... 4800 24 30
## 165 1 94.5 168.7 ... 6600 26 29
## 108 0 107.9 186.7 ... 4150 28 33
## 152 1 95.7 158.7 ... 4800 31 38
## 172 2 98.4 176.2 ... 4800 24 30
## 85 1 96.3 172.4 ... 5000 25 32
## 57 3 95.3 169.0 ... 6000 17 23
## 69 0 106.7 187.5 ... 4350 22 25
## 99 0 97.2 173.4 ... 5200 27 34
## 48 0 113.0 199.6 ... 4750 15 19
## 157 0 95.7 166.3 ... 4800 30 37
## 189 3 94.5 159.3 ... 5500 24 29
## 176 -1 102.4 175.6 ... 4200 27 32
## 22 1 93.7 157.3 ... 5500 31 38
## 75 1 102.7 178.4 ... 5000 19 24
## 93 1 94.5 170.2 ... 5200 31 37
## 201 -1 109.1 188.8 ... 5300 19 25
## 43 0 94.3 170.7 ... 4800 24 29
## 155 0 95.7 169.7 ... 4800 27 32
## 67 -1 110.0 190.9 ... 4350 22 25
## 194 -2 104.3 188.8 ... 5400 23 28
## 2 1 94.5 171.2 ... 5000 19 26
## 135 2 99.1 186.6 ... 5250 21 28
## 202 -1 109.1 188.8 ... 5500 18 23
## 186 2 97.3 171.7 ... 5250 27 34
## 3 2 99.8 176.6 ... 5500 24 30
## 187 2 97.3 171.7 ... 4500 37 42
## 137 2 99.1 186.6 ... 5500 19 26
## 122 1 93.7 167.3 ... 5500 31 38
## 156 0 95.7 166.3 ... 4800 30 37
## 33 1 93.7 150.0 ... 6000 30 34
## 124 3 95.9 173.2 ... 5000 19 24
## 53 1 93.1 166.8 ... 5000 31 38
## 129 1 98.4 175.7 ... 5750 17 28
## 168 2 98.4 176.2 ... 4800 24 30
## 16 0 103.5 193.8 ... 5400 16 22
## 65 0 104.9 175.0 ... 5000 19 27
## 204 -1 109.1 188.8 ... 5400 19 25
## 144 0 97.0 172.0 ... 4800 24 25
##
## [41 rows x 14 columns]
Se construye el modelo de regresión lineal múltiple (rm)
modelo_rm = LinearRegression()
modelo_rm.fit(X_entrena,Y_entrena)
## LinearRegression()
Solo se muestran los coeficientes de: \(\beta_1, \beta_2, ...\beta_n\)
modelo_rm.coef_
## array([ 3.39743570e+02, 2.28625211e+02, -1.32670486e+02, 7.78254108e+02,
## 2.70816972e+02, -6.35459624e-01, 1.08611929e+02, -2.27496528e+02,
## -3.17549673e+03, 3.26116786e+02, 5.87324973e+01, 1.87410546e+00,
## -2.54124899e+02, 1.72090793e+02])
print(modelo_rm.score(X_entrena, Y_entrena))
## 0.8587723706648346
predicciones_rm = modelo_rm.predict(X_valida)
print(predicciones_rm[:-1])
## [ 9727.67264512 17142.77862913 13784.67032518 11337.86773894
## 18352.28698194 6689.0971978 13779.06236051 9457.42154791
## 8461.51777503 24117.26785698 10395.6222793 29166.28856022
## 6615.25530535 11290.6100733 9514.31994577 6620.13440886
## 20699.901503 5581.95749434 20922.65268531 4937.03705545
## 5423.86289897 24501.50579309 15763.31325932 17119.17265407
## 13814.69368122 24224.54137126 10470.31925979 12385.13965397
## 10177.04606929 17229.52218157 5962.1551961 6687.21156921
## 8757.81623047 15347.35188085 5665.24303005 38696.3383786
## 13787.21216368 26077.74370547 17043.22885389 18738.82621971]
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
## 46 2 96.0 ... 11048.0 9727.672645
## 113 0 114.2 ... 16695.0 17142.778629
## 167 2 98.4 ... 8449.0 13784.670325
## 165 1 94.5 ... 9298.0 11337.867739
## 108 0 107.9 ... 13200.0 18352.286982
## 152 1 95.7 ... 6488.0 6689.097198
## 172 2 98.4 ... 17669.0 13779.062361
## 85 1 96.3 ... 6989.0 9457.421548
## 57 3 95.3 ... 13645.0 8461.517775
## 69 0 106.7 ... 28176.0 24117.267857
## 99 0 97.2 ... 8949.0 10395.622279
## 48 0 113.0 ... 35550.0 29166.288560
## 157 0 95.7 ... 7198.0 6615.255305
## 189 3 94.5 ... 11595.0 11290.610073
## 176 -1 102.4 ... 10898.0 9514.319946
## 22 1 93.7 ... 6377.0 6620.134409
## 75 1 102.7 ... 16503.0 20699.901503
## 93 1 94.5 ... 7349.0 5581.957494
## 201 -1 109.1 ... 19045.0 20922.652685
## 43 0 94.3 ... 6785.0 4937.037055
## 155 0 95.7 ... 8778.0 5423.862899
## 67 -1 110.0 ... 25552.0 24501.505793
## 194 -2 104.3 ... 12940.0 15763.313259
## 2 1 94.5 ... 16500.0 17119.172654
## 135 2 99.1 ... 15510.0 13814.693681
## 202 -1 109.1 ... 21485.0 24224.541371
## 186 2 97.3 ... 8495.0 10470.319260
## 3 2 99.8 ... 13950.0 12385.139654
## 187 2 97.3 ... 9495.0 10177.046069
## 137 2 99.1 ... 18620.0 17229.522182
## 122 1 93.7 ... 7609.0 5962.155196
## 156 0 95.7 ... 6938.0 6687.211569
## 33 1 93.7 ... 6529.0 8757.816230
## 124 3 95.9 ... 12764.0 15347.351881
## 53 1 93.1 ... 6695.0 5665.243030
## 129 1 98.4 ... 31400.5 38696.338379
## 168 2 98.4 ... 9639.0 13787.212164
## 16 0 103.5 ... 41315.0 26077.743705
## 65 0 104.9 ... 18280.0 17043.228854
## 204 -1 109.1 ... 22625.0 18738.826220
## 144 0 97.0 ... 9233.0 9556.528372
##
## [41 rows x 16 columns]
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: 3739.4258639164377
o
print('Root Mean Squared Error RMSE:', np.sqrt(metrics.mean_squared_error(Y_valida, predicciones_rm)))
## Root Mean Squared Error RMSE: 3739.4258639164377
Se construye el modelo de árbol de regresión (ar)
modelo_ar = DecisionTreeRegressor(
#max_depth = 3,
random_state = 1301
)
Entrenar el modelo
modelo_ar.fit(X_entrena, Y_entrena)
## DecisionTreeRegressor(random_state=1301)
fig, ax = plt.subplots(figsize=(12, 5))
print(f"Profundidad del árbol: {modelo_ar.get_depth()}")
## Profundidad del árbol: 14
print(f"Número de nodos terminales: {modelo_ar.get_n_leaves()}")
## Número de nodos terminales: 150
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 <= 2697.50
## | | |--- curbweight <= 2291.50
## | | | |--- curbweight <= 2072.00
## | | | | |--- horsepower <= 68.50
## | | | | | |--- curbweight <= 1931.00
## | | | | | | |--- peakrpm <= 5050.00
## | | | | | | | |--- highwaympg <= 34.50
## | | | | | | | | |--- value: [5195.00]
## | | | | | | | |--- highwaympg > 34.50
## | | | | | | | | |--- curbweight <= 1902.50
## | | | | | | | | | |--- peakrpm <= 4900.00
## | | | | | | | | | | |--- value: [6479.00]
## | | | | | | | | | |--- peakrpm > 4900.00
## | | | | | | | | | | |--- value: [6095.00]
## | | | | | | | | |--- curbweight > 1902.50
## | | | | | | | | | |--- value: [6795.00]
## | | | | | | |--- peakrpm > 5050.00
## | | | | | | | |--- stroke <= 3.05
## | | | | | | | | |--- value: [5151.00]
## | | | | | | | |--- stroke > 3.05
## | | | | | | | | |--- carwidth <= 63.90
## | | | | | | | | | |--- value: [5572.00]
## | | | | | | | | |--- carwidth > 63.90
## | | | | | | | | | |--- boreratio <= 2.94
## | | | | | | | | | | |--- value: [5399.00]
## | | | | | | | | | |--- boreratio > 2.94
## | | | | | | | | | | |--- value: [5389.00]
## | | | | | |--- curbweight > 1931.00
## | | | | | | |--- wheelbase <= 95.10
## | | | | | | | |--- boreratio <= 2.98
## | | | | | | | | |--- curbweight <= 1978.00
## | | | | | | | | | |--- curbweight <= 1955.50
## | | | | | | | | | | |--- value: [6189.00]
## | | | | | | | | | |--- curbweight > 1955.50
## | | | | | | | | | | |--- value: [6229.00]
## | | | | | | | | |--- curbweight > 1978.00
## | | | | | | | | | |--- carheight <= 50.70
## | | | | | | | | | | |--- value: [7150.50]
## | | | | | | | | | |--- carheight > 50.70
## | | | | | | | | | | |--- carlength <= 162.30
## | | | | | | | | | | | |--- value: [6669.00]
## | | | | | | | | | | |--- carlength > 162.30
## | | | | | | | | | | | |--- value: [6692.00]
## | | | | | | | |--- boreratio > 2.98
## | | | | | | | | |--- wheelbase <= 93.80
## | | | | | | | | | |--- value: [7395.00]
## | | | | | | | | |--- wheelbase > 93.80
## | | | | | | | | | |--- value: [7099.00]
## | | | | | | |--- wheelbase > 95.10
## | | | | | | | |--- highwaympg <= 38.50
## | | | | | | | | |--- value: [6338.00]
## | | | | | | | |--- highwaympg > 38.50
## | | | | | | | | |--- value: [5348.00]
## | | | | |--- horsepower > 68.50
## | | | | | |--- carwidth <= 63.50
## | | | | | | |--- value: [5118.00]
## | | | | | |--- carwidth > 63.50
## | | | | | | |--- carheight <= 54.00
## | | | | | | | |--- stroke <= 3.35
## | | | | | | | | |--- carlength <= 157.35
## | | | | | | | | | |--- symboling <= 0.50
## | | | | | | | | | | |--- value: [8916.50]
## | | | | | | | | | |--- symboling > 0.50
## | | | | | | | | | | |--- value: [7605.75]
## | | | | | | | | |--- carlength > 157.35
## | | | | | | | | | |--- stroke <= 3.20
## | | | | | | | | | | |--- value: [6575.00]
## | | | | | | | | | |--- stroke > 3.20
## | | | | | | | | | | |--- wheelbase <= 94.80
## | | | | | | | | | | | |--- truncated branch of depth 2
## | | | | | | | | | | |--- wheelbase > 94.80
## | | | | | | | | | | | |--- value: [8249.00]
## | | | | | | | |--- stroke > 3.35
## | | | | | | | | |--- carwidth <= 63.95
## | | | | | | | | | |--- value: [6855.00]
## | | | | | | | | |--- carwidth > 63.95
## | | | | | | | | | |--- value: [7129.00]
## | | | | | | |--- carheight > 54.00
## | | | | | | | |--- 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
## | | | | | | | | | |--- compressionratio <= 9.30
## | | | | | | | | | | |--- value: [7295.00]
## | | | | | | | | | |--- compressionratio > 9.30
## | | | | | | | | | | |--- curbweight <= 1961.00
## | | | | | | | | | | | |--- value: [7299.00]
## | | | | | | | | | | |--- curbweight > 1961.00
## | | | | | | | | | | | |--- value: [7499.00]
## | | | |--- curbweight > 2072.00
## | | | | |--- highwaympg <= 29.50
## | | | | | |--- value: [9980.00]
## | | | | |--- highwaympg > 29.50
## | | | | | |--- stroke <= 2.84
## | | | | | | |--- curbweight <= 2167.50
## | | | | | | | |--- horsepower <= 77.50
## | | | | | | | | |--- value: [7053.00]
## | | | | | | | |--- horsepower > 77.50
## | | | | | | | | |--- value: [7126.00]
## | | | | | | |--- curbweight > 2167.50
## | | | | | | | |--- curbweight <= 2215.00
## | | | | | | | | |--- value: [7775.00]
## | | | | | | | |--- curbweight > 2215.00
## | | | | | | | | |--- carwidth <= 64.60
## | | | | | | | | | |--- value: [7603.00]
## | | | | | | | | |--- carwidth > 64.60
## | | | | | | | | | |--- value: [7463.00]
## | | | | | |--- stroke > 2.84
## | | | | | | |--- citympg <= 30.00
## | | | | | | | |--- symboling <= 0.50
## | | | | | | | | |--- highwaympg <= 32.50
## | | | | | | | | | |--- value: [7898.00]
## | | | | | | | | |--- highwaympg > 32.50
## | | | | | | | | | |--- curbweight <= 2262.50
## | | | | | | | | | | |--- carlength <= 166.90
## | | | | | | | | | | | |--- truncated branch of depth 2
## | | | | | | | | | | |--- carlength > 166.90
## | | | | | | | | | | | |--- value: [7895.00]
## | | | | | | | | | |--- curbweight > 2262.50
## | | | | | | | | | | |--- value: [9095.00]
## | | | | | | | |--- symboling > 0.50
## | | | | | | | | |--- carheight <= 50.70
## | | | | | | | | | |--- value: [8558.00]
## | | | | | | | | |--- carheight > 50.70
## | | | | | | | | | |--- wheelbase <= 93.35
## | | | | | | | | | | |--- value: [7689.00]
## | | | | | | | | | |--- wheelbase > 93.35
## | | | | | | | | | | |--- wheelbase <= 94.10
## | | | | | | | | | | | |--- value: [7957.00]
## | | | | | | | | | | |--- wheelbase > 94.10
## | | | | | | | | | | | |--- truncated branch of depth 4
## | | | | | | |--- citympg > 30.00
## | | | | | | | |--- citympg <= 32.50
## | | | | | | | | |--- value: [6918.00]
## | | | | | | | |--- citympg > 32.50
## | | | | | | | | |--- curbweight <= 2262.50
## | | | | | | | | | |--- symboling <= 1.00
## | | | | | | | | | | |--- value: [7738.00]
## | | | | | | | | | |--- symboling > 1.00
## | | | | | | | | | | |--- value: [7775.00]
## | | | | | | | | |--- curbweight > 2262.50
## | | | | | | | | | |--- carheight <= 52.90
## | | | | | | | | | | |--- value: [7788.00]
## | | | | | | | | | |--- carheight > 52.90
## | | | | | | | | | | |--- wheelbase <= 96.50
## | | | | | | | | | | | |--- value: [7898.00]
## | | | | | | | | | | |--- wheelbase > 96.50
## | | | | | | | | | | | |--- value: [7995.00]
## | | |--- curbweight > 2291.50
## | | | |--- highwaympg <= 29.50
## | | | | |--- wheelbase <= 99.45
## | | | | | |--- highwaympg <= 28.50
## | | | | | | |--- horsepower <= 110.50
## | | | | | | | |--- boreratio <= 3.24
## | | | | | | | | |--- value: [12945.00]
## | | | | | | | |--- boreratio > 3.24
## | | | | | | | | |--- boreratio <= 3.43
## | | | | | | | | | |--- value: [11395.00]
## | | | | | | | | |--- boreratio > 3.43
## | | | | | | | | | |--- compressionratio <= 9.31
## | | | | | | | | | | |--- value: [12170.00]
## | | | | | | | | | |--- compressionratio > 9.31
## | | | | | | | | | | |--- value: [11850.00]
## | | | | | | |--- horsepower > 110.50
## | | | | | | | |--- citympg <= 22.00
## | | | | | | | | |--- stroke <= 2.97
## | | | | | | | | | |--- value: [14997.50]
## | | | | | | | | |--- stroke > 2.97
## | | | | | | | | | |--- value: [15645.00]
## | | | | | | | |--- citympg > 22.00
## | | | | | | | | |--- value: [11694.00]
## | | | | | |--- highwaympg > 28.50
## | | | | | | |--- wheelbase <= 96.95
## | | | | | | | |--- boreratio <= 3.43
## | | | | | | | | |--- value: [9538.00]
## | | | | | | | |--- boreratio > 3.43
## | | | | | | | | |--- value: [8013.00]
## | | | | | | |--- wheelbase > 96.95
## | | | | | | | |--- value: [11259.00]
## | | | | |--- wheelbase > 99.45
## | | | | | |--- carwidth <= 66.60
## | | | | | | |--- highwaympg <= 27.00
## | | | | | | | |--- value: [15250.00]
## | | | | | | |--- highwaympg > 27.00
## | | | | | | | |--- symboling <= 1.00
## | | | | | | | | |--- value: [16925.00]
## | | | | | | | |--- symboling > 1.00
## | | | | | | | | |--- value: [16430.00]
## | | | | | |--- carwidth > 66.60
## | | | | | | |--- value: [13295.00]
## | | | |--- highwaympg > 29.50
## | | | | |--- carwidth <= 66.75
## | | | | | |--- compressionratio <= 8.55
## | | | | | | |--- peakrpm <= 5100.00
## | | | | | | | |--- symboling <= 0.00
## | | | | | | | | |--- value: [8921.00]
## | | | | | | | |--- symboling > 0.00
## | | | | | | | | |--- carlength <= 172.70
## | | | | | | | | | |--- value: [8189.00]
## | | | | | | | | |--- carlength > 172.70
## | | | | | | | | | |--- value: [8499.00]
## | | | | | | |--- peakrpm > 5100.00
## | | | | | | | |--- carheight <= 50.50
## | | | | | | | | |--- value: [9959.00]
## | | | | | | | |--- carheight > 50.50
## | | | | | | | | |--- peakrpm <= 5350.00
## | | | | | | | | | |--- value: [9549.00]
## | | | | | | | | |--- peakrpm > 5350.00
## | | | | | | | | | |--- value: [9279.00]
## | | | | | |--- compressionratio > 8.55
## | | | | | | |--- curbweight <= 2419.50
## | | | | | | | |--- carlength <= 173.70
## | | | | | | | | |--- highwaympg <= 31.50
## | | | | | | | | | |--- value: [10345.00]
## | | | | | | | | |--- highwaympg > 31.50
## | | | | | | | | | |--- wheelbase <= 97.25
## | | | | | | | | | | |--- value: [9960.00]
## | | | | | | | | | |--- wheelbase > 97.25
## | | | | | | | | | | |--- value: [9995.00]
## | | | | | | | |--- carlength > 173.70
## | | | | | | | | |--- curbweight <= 2349.00
## | | | | | | | | | |--- horsepower <= 89.00
## | | | | | | | | | | |--- value: [8845.00]
## | | | | | | | | | |--- horsepower > 89.00
## | | | | | | | | | | |--- value: [8948.00]
## | | | | | | | | |--- curbweight > 2349.00
## | | | | | | | | | |--- stroke <= 3.47
## | | | | | | | | | | |--- symboling <= 0.50
## | | | | | | | | | | | |--- value: [9370.00]
## | | | | | | | | | | |--- symboling > 0.50
## | | | | | | | | | | | |--- value: [9720.00]
## | | | | | | | | | |--- stroke > 3.47
## | | | | | | | | | | |--- curbweight <= 2393.00
## | | | | | | | | | | | |--- value: [10295.00]
## | | | | | | | | | | |--- curbweight > 2393.00
## | | | | | | | | | | | |--- value: [9988.00]
## | | | | | | |--- curbweight > 2419.50
## | | | | | | | |--- peakrpm <= 4950.00
## | | | | | | | | |--- compressionratio <= 9.00
## | | | | | | | | | |--- boreratio <= 3.35
## | | | | | | | | | | |--- value: [11248.00]
## | | | | | | | | | |--- boreratio > 3.35
## | | | | | | | | | | |--- value: [11245.00]
## | | | | | | | | |--- compressionratio > 9.00
## | | | | | | | | | |--- curbweight <= 2615.00
## | | | | | | | | | | |--- symboling <= 1.00
## | | | | | | | | | | | |--- truncated branch of depth 2
## | | | | | | | | | | |--- symboling > 1.00
## | | | | | | | | | | | |--- value: [9989.00]
## | | | | | | | | | |--- curbweight > 2615.00
## | | | | | | | | | | |--- value: [11199.00]
## | | | | | | | |--- peakrpm > 4950.00
## | | | | | | | | |--- carheight <= 54.10
## | | | | | | | | | |--- compressionratio <= 8.85
## | | | | | | | | | | |--- value: [9895.00]
## | | | | | | | | | |--- compressionratio > 8.85
## | | | | | | | | | | |--- value: [10198.00]
## | | | | | | | | |--- carheight > 54.10
## | | | | | | | | | |--- value: [9295.00]
## | | | | |--- carwidth > 66.75
## | | | | | |--- boreratio <= 3.10
## | | | | | | |--- value: [13845.00]
## | | | | | |--- boreratio > 3.10
## | | | | | | |--- value: [12290.00]
## | |--- curbweight > 2697.50
## | | |--- boreratio <= 3.37
## | | | |--- peakrpm <= 5000.00
## | | | | |--- carheight <= 55.60
## | | | | | |--- carlength <= 182.80
## | | | | | | |--- curbweight <= 2737.50
## | | | | | | | |--- value: [20970.00]
## | | | | | | |--- curbweight > 2737.50
## | | | | | | | |--- value: [21105.00]
## | | | | | |--- carlength > 182.80
## | | | | | | |--- value: [22470.00]
## | | | | |--- carheight > 55.60
## | | | | | |--- value: [24565.00]
## | | | |--- peakrpm > 5000.00
## | | | | |--- highwaympg <= 21.00
## | | | | | |--- value: [23875.00]
## | | | | |--- highwaympg > 21.00
## | | | | | |--- compressionratio <= 8.85
## | | | | | | |--- curbweight <= 2899.00
## | | | | | | | |--- carheight <= 55.00
## | | | | | | | | |--- value: [17450.00]
## | | | | | | | |--- carheight > 55.00
## | | | | | | | | |--- value: [17710.00]
## | | | | | | |--- curbweight > 2899.00
## | | | | | | | |--- horsepower <= 135.00
## | | | | | | | | |--- value: [18920.00]
## | | | | | | | |--- horsepower > 135.00
## | | | | | | | | |--- value: [17859.17]
## | | | | | |--- compressionratio > 8.85
## | | | | | | |--- carlength <= 185.05
## | | | | | | | |--- citympg <= 19.50
## | | | | | | | | |--- value: [15998.00]
## | | | | | | | |--- citympg > 19.50
## | | | | | | | | |--- value: [16558.00]
## | | | | | | |--- carlength > 185.05
## | | | | | | | |--- horsepower <= 133.00
## | | | | | | | | |--- value: [15040.00]
## | | | | | | | |--- horsepower > 133.00
## | | | | | | | | |--- curbweight <= 3141.00
## | | | | | | | | | |--- value: [15690.00]
## | | | | | | | | |--- curbweight > 3141.00
## | | | | | | | | | |--- value: [15750.00]
## | | |--- boreratio > 3.37
## | | | |--- horsepower <= 156.00
## | | | | |--- peakrpm <= 5450.00
## | | | | | |--- compressionratio <= 9.40
## | | | | | | |--- curbweight <= 2877.00
## | | | | | | | |--- symboling <= 2.50
## | | | | | | | | |--- value: [11549.00]
## | | | | | | | |--- symboling > 2.50
## | | | | | | | | |--- boreratio <= 3.59
## | | | | | | | | | |--- value: [12629.00]
## | | | | | | | | |--- boreratio > 3.59
## | | | | | | | | | |--- value: [12964.00]
## | | | | | | |--- curbweight > 2877.00
## | | | | | | | |--- carlength <= 192.80
## | | | | | | | | |--- curbweight <= 3067.50
## | | | | | | | | | |--- wheelbase <= 104.15
## | | | | | | | | | | |--- compressionratio <= 8.00
## | | | | | | | | | | | |--- truncated branch of depth 2
## | | | | | | | | | | |--- compressionratio > 8.00
## | | | | | | | | | | | |--- value: [13499.00]
## | | | | | | | | | |--- wheelbase > 104.15
## | | | | | | | | | | |--- value: [11900.00]
## | | | | | | | | |--- curbweight > 3067.50
## | | | | | | | | | |--- wheelbase <= 104.15
## | | | | | | | | | | |--- carheight <= 55.60
## | | | | | | | | | | | |--- value: [13499.00]
## | | | | | | | | | | |--- carheight > 55.60
## | | | | | | | | | | | |--- value: [14399.00]
## | | | | | | | | | |--- wheelbase > 104.15
## | | | | | | | | | | |--- horsepower <= 96.00
## | | | | | | | | | | | |--- value: [15580.00]
## | | | | | | | | | | |--- horsepower > 96.00
## | | | | | | | | | | | |--- value: [16630.00]
## | | | | | | | |--- carlength > 192.80
## | | | | | | | | |--- value: [12440.00]
## | | | | | |--- compressionratio > 9.40
## | | | | | | |--- citympg <= 26.50
## | | | | | | | |--- curbweight <= 3457.50
## | | | | | | | | |--- carheight <= 56.85
## | | | | | | | | | |--- wheelbase <= 106.70
## | | | | | | | | | | |--- value: [15985.00]
## | | | | | | | | | |--- wheelbase > 106.70
## | | | | | | | | | | |--- value: [16845.00]
## | | | | | | | | |--- carheight > 56.85
## | | | | | | | | | |--- citympg <= 23.50
## | | | | | | | | | | |--- value: [13415.00]
## | | | | | | | | | |--- citympg > 23.50
## | | | | | | | | | | |--- symboling <= -0.50
## | | | | | | | | | | | |--- value: [16515.00]
## | | | | | | | | | | |--- symboling > -0.50
## | | | | | | | | | | | |--- value: [13860.00]
## | | | | | | | |--- curbweight > 3457.50
## | | | | | | | | |--- value: [17075.00]
## | | | | | | |--- citympg > 26.50
## | | | | | | | |--- enginesize <= 143.00
## | | | | | | | | |--- value: [18344.00]
## | | | | | | | |--- enginesize > 143.00
## | | | | | | | | |--- value: [17425.00]
## | | | | |--- peakrpm > 5450.00
## | | | | | |--- highwaympg <= 25.50
## | | | | | | |--- value: [18150.00]
## | | | | | |--- highwaympg > 25.50
## | | | | | | |--- value: [22018.00]
## | | | |--- horsepower > 156.00
## | | | | |--- highwaympg <= 24.00
## | | | | | |--- stroke <= 3.21
## | | | | | | |--- curbweight <= 3101.00
## | | | | | | | |--- value: [18420.00]
## | | | | | | |--- curbweight > 3101.00
## | | | | | | | |--- value: [18950.00]
## | | | | | |--- stroke > 3.21
## | | | | | | |--- value: [19699.00]
## | | | | |--- highwaympg > 24.00
## | | | | | |--- wheelbase <= 95.20
## | | | | | | |--- value: [17199.00]
## | | | | | |--- wheelbase > 95.20
## | | | | | | |--- enginesize <= 151.00
## | | | | | | | |--- value: [18150.00]
## | | | | | | |--- enginesize > 151.00
## | | | | | | | |--- value: [18399.00]
## |--- enginesize > 182.00
## | |--- boreratio <= 3.77
## | | |--- stroke <= 3.52
## | | | |--- citympg <= 15.50
## | | | | |--- compressionratio <= 9.75
## | | | | | |--- value: [36880.00]
## | | | | |--- compressionratio > 9.75
## | | | | | |--- value: [36000.00]
## | | | |--- citympg > 15.50
## | | | | |--- compressionratio <= 8.15
## | | | | | |--- value: [30760.00]
## | | | | |--- compressionratio > 8.15
## | | | | | |--- curbweight <= 2778.00
## | | | | | | |--- value: [33278.00]
## | | | | | |--- curbweight > 2778.00
## | | | | | | |--- wheelbase <= 93.05
## | | | | | | | |--- value: [37028.00]
## | | | | | | |--- wheelbase > 93.05
## | | | | | | | |--- symboling <= 1.00
## | | | | | | | | |--- value: [34184.00]
## | | | | | | | |--- symboling > 1.00
## | | | | | | | | |--- value: [35056.00]
## | | |--- stroke > 3.52
## | | | |--- curbweight <= 3760.00
## | | | | |--- value: [28248.00]
## | | | |--- curbweight > 3760.00
## | | | | |--- curbweight <= 3918.00
## | | | | | |--- value: [31600.00]
## | | | | |--- curbweight > 3918.00
## | | | | | |--- value: [32250.00]
## | |--- boreratio > 3.77
## | | |--- wheelbase <= 116.45
## | | | |--- value: [45400.00]
## | | |--- wheelbase > 116.45
## | | | |--- value: [40960.00]
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.671950
## 5 curbweight 0.241722
## 7 boreratio 0.023963
## 13 highwaympg 0.017926
## 11 peakrpm 0.011950
## 10 horsepower 0.007024
## 9 compressionratio 0.006426
## 1 wheelbase 0.006164
## 8 stroke 0.003782
## 12 citympg 0.003145
## 3 carwidth 0.003076
## 4 carheight 0.001438
## 0 symboling 0.000749
## 2 carlength 0.000684
Estos sería los predictores más importantes para el modelo de árbol de regresión enginesize, curbweight, boreratio, highwaympg y peakrpm.
predicciones_ar = modelo_ar.predict(X = X_valida)
predicciones_ar
## array([11549., 12440., 9989., 9980., 17425., 6338., 13499., 8189.,
## 11395., 28248., 9549., 32250., 8358., 9980., 9988., 5572.,
## 18420., 7799., 18150., 9538., 24565., 28248., 15985., 21105.,
## 11549., 18150., 8195., 8948., 9995., 18150., 6918., 8358.,
## 7129., 12629., 7395., 45400., 9989., 30760., 15250., 16845.,
## 12170.])
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
## 46 2 96.0 ... 11048.0 11549.0
## 113 0 114.2 ... 16695.0 12440.0
## 167 2 98.4 ... 8449.0 9989.0
## 165 1 94.5 ... 9298.0 9980.0
## 108 0 107.9 ... 13200.0 17425.0
## 152 1 95.7 ... 6488.0 6338.0
## 172 2 98.4 ... 17669.0 13499.0
## 85 1 96.3 ... 6989.0 8189.0
## 57 3 95.3 ... 13645.0 11395.0
## 69 0 106.7 ... 28176.0 28248.0
## 99 0 97.2 ... 8949.0 9549.0
## 48 0 113.0 ... 35550.0 32250.0
## 157 0 95.7 ... 7198.0 8358.0
## 189 3 94.5 ... 11595.0 9980.0
## 176 -1 102.4 ... 10898.0 9988.0
## 22 1 93.7 ... 6377.0 5572.0
## 75 1 102.7 ... 16503.0 18420.0
## 93 1 94.5 ... 7349.0 7799.0
## 201 -1 109.1 ... 19045.0 18150.0
## 43 0 94.3 ... 6785.0 9538.0
## 155 0 95.7 ... 8778.0 24565.0
## 67 -1 110.0 ... 25552.0 28248.0
## 194 -2 104.3 ... 12940.0 15985.0
## 2 1 94.5 ... 16500.0 21105.0
## 135 2 99.1 ... 15510.0 11549.0
## 202 -1 109.1 ... 21485.0 18150.0
## 186 2 97.3 ... 8495.0 8195.0
## 3 2 99.8 ... 13950.0 8948.0
## 187 2 97.3 ... 9495.0 9995.0
## 137 2 99.1 ... 18620.0 18150.0
## 122 1 93.7 ... 7609.0 6918.0
## 156 0 95.7 ... 6938.0 8358.0
## 33 1 93.7 ... 6529.0 7129.0
## 124 3 95.9 ... 12764.0 12629.0
## 53 1 93.1 ... 6695.0 7395.0
## 129 1 98.4 ... 31400.5 45400.0
## 168 2 98.4 ... 9639.0 9989.0
## 16 0 103.5 ... 41315.0 30760.0
## 65 0 104.9 ... 18280.0 15250.0
## 204 -1 109.1 ... 22625.0 16845.0
## 144 0 97.0 ... 9233.0 12170.0
##
## [41 rows x 16 columns]
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: 4398.897359760434
o
print('Root Mean Squared Error RMSE:', np.sqrt(metrics.mean_squared_error(Y_valida, predicciones_ar)))
## Root Mean Squared Error RMSE: 4398.897359760434
Se construye el modelo de árbol de regresión (ar). Semilla 2022 y 20 árboles de entrenamiento
modelo_rf = RandomForestRegressor(n_estimators = 20, random_state = 1301)
modelo_rf.fit(X_entrena, Y_entrena)
## RandomForestRegressor(n_estimators=20, random_state=1301)
# pendiente ... ...
predicciones_rf = modelo_rf.predict(X_valida)
predicciones_rf
## array([12881.8 , 14473.5 , 10745.53333333, 9217.75 ,
## 17462.575 , 6731.55 , 13481.3 , 9118.15 ,
## 11398.75 , 26299.425 , 9445.9 , 33864.9 ,
## 8103.05 , 9740.7 , 9618.81666667, 5903.65 ,
## 18594.85 , 7776.6 , 17908.05 , 11583.375 ,
## 13492.15 , 26383.425 , 15655.1 , 16173.60835 ,
## 14292.55 , 17929.55835 , 8214.15 , 10389.05 ,
## 10123.1 , 17125.2 , 7703.05 , 7809.05 ,
## 7346.575 , 14097.55 , 6935.45 , 35003.85 ,
## 10745.53333333, 31951.55 , 13967.06666667, 17160.5 ,
## 11369.9 ])
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
## 46 2 96.0 ... 11048.0 12881.800000
## 113 0 114.2 ... 16695.0 14473.500000
## 167 2 98.4 ... 8449.0 10745.533333
## 165 1 94.5 ... 9298.0 9217.750000
## 108 0 107.9 ... 13200.0 17462.575000
## 152 1 95.7 ... 6488.0 6731.550000
## 172 2 98.4 ... 17669.0 13481.300000
## 85 1 96.3 ... 6989.0 9118.150000
## 57 3 95.3 ... 13645.0 11398.750000
## 69 0 106.7 ... 28176.0 26299.425000
## 99 0 97.2 ... 8949.0 9445.900000
## 48 0 113.0 ... 35550.0 33864.900000
## 157 0 95.7 ... 7198.0 8103.050000
## 189 3 94.5 ... 11595.0 9740.700000
## 176 -1 102.4 ... 10898.0 9618.816667
## 22 1 93.7 ... 6377.0 5903.650000
## 75 1 102.7 ... 16503.0 18594.850000
## 93 1 94.5 ... 7349.0 7776.600000
## 201 -1 109.1 ... 19045.0 17908.050000
## 43 0 94.3 ... 6785.0 11583.375000
## 155 0 95.7 ... 8778.0 13492.150000
## 67 -1 110.0 ... 25552.0 26383.425000
## 194 -2 104.3 ... 12940.0 15655.100000
## 2 1 94.5 ... 16500.0 16173.608350
## 135 2 99.1 ... 15510.0 14292.550000
## 202 -1 109.1 ... 21485.0 17929.558350
## 186 2 97.3 ... 8495.0 8214.150000
## 3 2 99.8 ... 13950.0 10389.050000
## 187 2 97.3 ... 9495.0 10123.100000
## 137 2 99.1 ... 18620.0 17125.200000
## 122 1 93.7 ... 7609.0 7703.050000
## 156 0 95.7 ... 6938.0 7809.050000
## 33 1 93.7 ... 6529.0 7346.575000
## 124 3 95.9 ... 12764.0 14097.550000
## 53 1 93.1 ... 6695.0 6935.450000
## 129 1 98.4 ... 31400.5 35003.850000
## 168 2 98.4 ... 9639.0 10745.533333
## 16 0 103.5 ... 41315.0 31951.550000
## 65 0 104.9 ... 18280.0 13967.066667
## 204 -1 109.1 ... 22625.0 17160.500000
## 144 0 97.0 ... 9233.0 11369.900000
##
## [41 rows x 16 columns]
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: 2787.6573634278498
o
print('Root Mean Squared Error RMSE:', np.sqrt(metrics.mean_squared_error(Y_valida, predicciones_rf)))
## Root Mean Squared Error RMSE: 2787.6573634278498
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
## 46 2 96.0 ... 11549.0 12881.800000
## 113 0 114.2 ... 12440.0 14473.500000
## 167 2 98.4 ... 9989.0 10745.533333
## 165 1 94.5 ... 9980.0 9217.750000
## 108 0 107.9 ... 17425.0 17462.575000
## 152 1 95.7 ... 6338.0 6731.550000
## 172 2 98.4 ... 13499.0 13481.300000
## 85 1 96.3 ... 8189.0 9118.150000
## 57 3 95.3 ... 11395.0 11398.750000
## 69 0 106.7 ... 28248.0 26299.425000
## 99 0 97.2 ... 9549.0 9445.900000
## 48 0 113.0 ... 32250.0 33864.900000
## 157 0 95.7 ... 8358.0 8103.050000
## 189 3 94.5 ... 9980.0 9740.700000
## 176 -1 102.4 ... 9988.0 9618.816667
## 22 1 93.7 ... 5572.0 5903.650000
## 75 1 102.7 ... 18420.0 18594.850000
## 93 1 94.5 ... 7799.0 7776.600000
## 201 -1 109.1 ... 18150.0 17908.050000
## 43 0 94.3 ... 9538.0 11583.375000
## 155 0 95.7 ... 24565.0 13492.150000
## 67 -1 110.0 ... 28248.0 26383.425000
## 194 -2 104.3 ... 15985.0 15655.100000
## 2 1 94.5 ... 21105.0 16173.608350
## 135 2 99.1 ... 11549.0 14292.550000
## 202 -1 109.1 ... 18150.0 17929.558350
## 186 2 97.3 ... 8195.0 8214.150000
## 3 2 99.8 ... 8948.0 10389.050000
## 187 2 97.3 ... 9995.0 10123.100000
## 137 2 99.1 ... 18150.0 17125.200000
## 122 1 93.7 ... 6918.0 7703.050000
## 156 0 95.7 ... 8358.0 7809.050000
## 33 1 93.7 ... 7129.0 7346.575000
## 124 3 95.9 ... 12629.0 14097.550000
## 53 1 93.1 ... 7395.0 6935.450000
## 129 1 98.4 ... 45400.0 35003.850000
## 168 2 98.4 ... 9989.0 10745.533333
## 16 0 103.5 ... 30760.0 31951.550000
## 65 0 104.9 ... 15250.0 13967.066667
## 204 -1 109.1 ... 16845.0 17160.500000
## 144 0 97.0 ... 12170.0 11369.900000
##
## [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([[3739.42586392, 4398.89735976, 2787.65736343]])
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 3739.425864 4398.89736 2787.657363
Se cargaron datos numéricos de precios de automóviles basados en algunas variables numéricas.
El RMSE devolvió los siguientes resultados: Modelo de regresión lineal múltiple (RM) RMSE: 3739.4258639164377.
Modelo de árbol de regresión (AR) RMSE: 4398.897359760434.
Modelo de bosques aleatorios (RF) RMSE: 2787.6573634278498.
En el modelo de árbol de regresión los predictores más importantes serían enginesize, curbweight, peakrpm, carheight y wheelbase.
En términos de mejor modelo de regresion, ¿cuál modelo es mejor comparando el estadístico RMSE? Al analizar los resultados, el mejor modelo conforme al estadístico raiz del error cuadrático medio (rmse) fue el de Modelo de bosques aleatorios (RF) con un valor de 2787.6573634278498. Resultando de los datos de entrenamiento y validación, y con el porcentaje de datos de entrenamiento y validación de 80% y 20%.