# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="1kR8GaH0z7YV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 555} outputId="ed29685c-ecc5-432f-c68a-3393b3f2fc1d" executionInfo={"status": "ok", "timestamp": 1583413761829, "user_tz": -60, "elapsed": 14131, "user": {"displayName": "<NAME>\u0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
# !pip install --upgrade tables
# !pip install eli5
# !pip install xgboost

# + id="LqBbntd40Dfj" colab_type="code" colab={}
import pandas as pd
import numpy as np

from sklearn.dummy import DummyRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

import xgboost as xgb

from sklearn.metrics import mean_absolute_error as mae
from sklearn.model_selection import cross_val_score, KFold

import eli5
from eli5.sklearn import PermutationImportance

# + id="ixqHmpk70_QZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="f5d15b78-3774-4f66-9265-9a4710d7f4d2" executionInfo={"status": "ok", "timestamp": 1583414778905, "user_tz": -60, "elapsed": 3061, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "07435370255154987946"}}
df = pd.read_hdf('data/car.h5')
df.shape

# + [markdown] id="DeaI7Pj52eZz" colab_type="text"
# ##Feature Engineering

# + id="aFxVj4fr2hrN" colab_type="code" colab={}
SUFFIX_CAT = '__cat'

for feat in df.columns:
  if isinstance(df[feat][0], list):continue

  factorized_values = df[feat].factorize()[0]
  if SUFFIX_CAT in feat:
    df[feat] = factorized_values
  else:
    df[feat + SUFFIX_CAT] = factorized_values    

# + id="5ewQj23e2-jx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="1ecd9fb5-d623-41d9-930d-2a25045308a6" executionInfo={"status": "ok", "timestamp": 1583414788078, "user_tz": -60, "elapsed": 988, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "07435370255154987946"}}
cat_feats = [x for x in df.columns if SUFFIX_CAT in x]
cat_feats = [x for x in cat_feats if 'price' not in x]
len(cat_feats)


# + id="Bn8RvPjF3bLg" colab_type="code" colab={}
def run_model(model, feats):
  X = df[feats].values
  y = df['price_value'].values

  scores = cross_val_score(model, X, y, cv=3, scoring="neg_mean_absolute_error")
  return np.mean(scores), np.std(scores)


# + [markdown] id="Zuu6SzBZ4mYO" colab_type="text"
# ## DecisionTree

# + id="S3PLuFBd3fYj" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="3a8a2771-6558-4efe-acfa-39a0da19aefe" executionInfo={"status": "ok", "timestamp": 1583414915896, "user_tz": -60, "elapsed": 5735, "user": {"displayName": "<NAME>\u0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
run_model(DecisionTreeRegressor(max_depth=5), cat_feats)

# + [markdown] id="20odFAfd4vnm" colab_type="text"
# ## Random Forest

# + id="yaTW1y-X4vAM" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="a39056b9-1148-49f6-e912-b0c9d0dd51d8" executionInfo={"status": "ok", "timestamp": 1583415140145, "user_tz": -60, "elapsed": 103627, "user": {"displayName": "<NAME>\u0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
model = RandomForestRegressor(max_depth=5, n_estimators=50, random_state=0)
run_model( model, cat_feats)

# + [markdown] id="ar1myUT85G1S" colab_type="text"
# XGBoost

# + id="lG4KR7Dk4hxb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 111} outputId="8c393ba2-fe82-403f-ad2b-888cb9fb8aec" executionInfo={"status": "ok", "timestamp": 1583415847313, "user_tz": -60, "elapsed": 59357, "user": {"displayName": "<NAME>0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
xgb_params = {
    'max_depth': 5,
    'n_estimators': 50,
    'learning_rate': 0.1,
    'seed': 0
}

run_model(xgb.XGBRegressor(**xgb_params), cat_feats)

# + id="Xg6GbfD85sYk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 438} outputId="d05f9053-9de3-4611-9fac-3ec19548c47b" executionInfo={"status": "ok", "timestamp": 1583416346209, "user_tz": -60, "elapsed": 361125, "user": {"displayName": "<NAME>0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
m = xgb.XGBRegressor(max_depth=5, n_estimators=50, learning_rate=0.1, seed=0)
m.fit(X, y)

imp = PermutationImportance(m, random_state=0).fit(X,y)
eli5.show_weights(imp, feature_names = cat_feats)

# + id="V1wrz-078oNZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 111} outputId="f2a2b126-5a35-4202-df60-e7b487149fd3" executionInfo={"status": "ok", "timestamp": 1583416977374, "user_tz": -60, "elapsed": 13909, "user": {"displayName": "<NAME>0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
feats = ['param_napęd__cat','param_rok-produkcji__cat','param_stan__cat','param_skrzynia-biegów__cat','param_faktura-vat__cat','param_moc__cat','param_marka-pojazdu__cat','feature_kamera-cofania__cat','param_typ__cat','param_pojemność-skokowa__cat','seller_name__cat','feature_wspomaganie-kierownicy__cat','param_model-pojazdu__cat','param_wersja__cat','param_kod-silnika__cat','feature_system-start-stop__cat','feature_asystent-pasa-ruchu__cat','feature_czujniki-parkowania-przednie__cat','feature_łopatki-zmiany-biegów__cat','feature_regulowane-zawieszenie__cat']

run_model(xgb.XGBRegressor(**xgb_params), feats)



# + id="9Fc6jLED_KXx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 111} outputId="c9676aca-465e-4528-a255-34bd8af9629e" executionInfo={"status": "ok", "timestamp": 1583417485998, "user_tz": -60, "elapsed": 14607, "user": {"displayName": "<NAME>\u0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
df['param_rok_produkcji'] = df['param_rok-produkcji'].map(lambda x: -1 if str(x) == 'None' else int(x))

feats = ['param_napęd__cat','param_rok-produkcji','param_stan__cat','param_skrzynia-biegów__cat','param_faktura-vat__cat','param_moc__cat','param_marka-pojazdu__cat','feature_kamera-cofania__cat','param_typ__cat','param_pojemność-skokowa__cat','seller_name__cat','feature_wspomaganie-kierownicy__cat','param_model-pojazdu__cat','param_wersja__cat','param_kod-silnika__cat','feature_system-start-stop__cat','feature_asystent-pasa-ruchu__cat','feature_czujniki-parkowania-przednie__cat','feature_łopatki-zmiany-biegów__cat','feature_regulowane-zawieszenie__cat']

run_model(xgb.XGBRegressor(**xgb_params), feats)

# + id="dhYiY-_7_pR3" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 111} outputId="4d71d30e-04d4-4660-8724-0e0de9a6545e" executionInfo={"status": "ok", "timestamp": 1583417587345, "user_tz": -60, "elapsed": 14394, "user": {"displayName": "<NAME>ali\u0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
df['param_moc'] = df['param_moc'].map(lambda x: -1 if str(x) == 'None' else int(x.split(' ')[0]) )


# + id="hXi5cIxCAsTN" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 111} outputId="fcd9eb97-d89c-4f7c-840f-c2c0b8e2a6b2" executionInfo={"status": "ok", "timestamp": 1583417758419, "user_tz": -60, "elapsed": 14244, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "07435370255154987946"}}
df['param_moc'] = df['param_moc'].map(lambda x: -1 if str(x) == 'None' else (int(str(x).split(' ')[0]) ) )

df['param_rok_produkcji'] = df['param_rok-produkcji'].map(lambda x: -1 if str(x) == 'None' else int(x))

feats = ['param_napęd__cat','param_rok-produkcji','param_stan__cat','param_skrzynia-biegów__cat','param_faktura-vat__cat','param_moc','param_marka-pojazdu__cat','feature_kamera-cofania__cat','param_typ__cat','param_pojemność-skokowa__cat','seller_name__cat','feature_wspomaganie-kierownicy__cat','param_model-pojazdu__cat','param_wersja__cat','param_kod-silnika__cat','feature_system-start-stop__cat','feature_asystent-pasa-ruchu__cat','feature_czujniki-parkowania-przednie__cat','feature_łopatki-zmiany-biegów__cat','feature_regulowane-zawieszenie__cat']

run_model(xgb.XGBRegressor(**xgb_params), feats)

# + id="Ot4yxci1C4eX" colab_type="code" colab={}
df['param_pojemność-skokowa'] = df['param_pojemność-skokowa'].map(lambda x: -1 if str(x) == 'None' else int(str(x).split('cm')[0].replace(' ','') ) )

# + id="m7-mi5MjDvoa" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 111} outputId="7cbacfd0-0963-4c38-9a6b-50e1fea8d91a" executionInfo={"status": "ok", "timestamp": 1583418246548, "user_tz": -60, "elapsed": 14013, "user": {"displayName": "<NAME>\u0144ski", "photoUrl": "", "userId": "07435370255154987946"}}
feats = ['param_napęd__cat','param_rok-produkcji','param_stan__cat','param_skrzynia-biegów__cat','param_faktura-vat__cat','param_moc','param_marka-pojazdu__cat','feature_kamera-cofania__cat','param_typ__cat','param_pojemność-skokowa','seller_name__cat','feature_wspomaganie-kierownicy__cat','param_model-pojazdu__cat','param_wersja__cat','param_kod-silnika__cat','feature_system-start-stop__cat','feature_asystent-pasa-ruchu__cat','feature_czujniki-parkowania-przednie__cat','feature_łopatki-zmiany-biegów__cat','feature_regulowane-zawieszenie__cat']

run_model(xgb.XGBRegressor(**xgb_params), feats)

# + id="1ApaREOpFM4m" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 54} outputId="2a971a9d-590b-4213-b82a-9219725f5b3f" executionInfo={"status": "ok", "timestamp": 1583418416824, "user_tz": -60, "elapsed": 3103, "user": {"displayName": "<NAME>\u0144ski", "photoUrl": "", "userId": "07435370255154987946"}}


# + id="PKFzW9M8F4yR" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered