# ---
# 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="lkf3NxuH1i_Z" colab_type="code" colab={}
# !pip install --upgrade tables
# !pip install eli5
# !pip install xgboost

# + id="GMxslV2b1wFM" 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="OpM_atrb1zD6" colab_type="code" colab={}
# cd "/content/drive/My Drive/Colab Notebooks/matrix/matrix_two/dw_matrix_car"

# + id="NDuQn3GP31jy" 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="fG8boQ9u33ry" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} executionInfo={"status": "ok", "timestamp": 1595613653158, "user_tz": -120, "elapsed": 740, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="7e5032ce-47af-4a42-be2b-593286a0f5f8"
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="JmTAQo7-9VrP" 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="QwlB3OVsHOyc" colab_type="text"
# ## Decision tree

# + id="lhVgEc3VGSZR" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} executionInfo={"status": "ok", "timestamp": 1595615774582, "user_tz": -120, "elapsed": 4941, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="b23d9b4f-f9bb-425f-cea3-71bb544f9e26"
run_model(DecisionTreeRegressor(max_depth=5), cat_feats)

# + [markdown] id="KGOTSecEHhYT" colab_type="text"
# ## Random forest
#
#

# + id="cRwk9M1HHmW7" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} executionInfo={"status": "ok", "timestamp": 1595616086174, "user_tz": -120, "elapsed": 111099, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="a018dc2d-3130-465b-d09e-17ec7513a3e6"
model = RandomForestRegressor(max_depth=5, n_estimators=50, random_state=0)
run_model(model, cat_feats)

# + [markdown] id="7VDTZuQUIXXV" colab_type="text"
# ## XGBoost

# + id="rcnW-Ec7IsM_" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} executionInfo={"status": "ok", "timestamp": 1595625817849, "user_tz": -120, "elapsed": 64979, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="82e1d49c-144d-4796-eb31-97b8cc06e40c"
xgb_params = {
    'max_depth':5,
    'n_estimators':50,
    'learning_rate':0.1,
    'seed':0
}


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

# + id="ax8VVpfRLCjP" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 408} executionInfo={"status": "ok", "timestamp": 1595617285328, "user_tz": -120, "elapsed": 356033, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="00e76ff0-8ee7-4b4b-b914-9b16beea5eec"
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="VMLLJxCUN5Fx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} executionInfo={"status": "ok", "timestamp": 1595625738070, "user_tz": -120, "elapsed": 14035, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="f720d6a1-7349-41a3-9f8a-1e314f19f42e"
feats = [
'param_napęd__cat',
'param_stan__cat',
'param_rok-produkcji__cat',
'param_faktura-vat__cat',
'param_moc__cat',
'param_skrzynia-biegów__cat',
'param_marka-pojazdu__cat',
'feature_kamera-cofania__cat',
'param_typ__cat',
'param_pojemność-skokowa__cat',
'seller_name__cat',
'param_wersja__cat',
'feature_wspomaganie-kierownicy__cat',
'param_model-pojazdu__cat',
'feature_system-start-stop__cat',
'param_kod-silnika__cat',
'feature_asystent-pasa-ruchu__cat',
'feature_łopatki-zmiany-biegów__cat',
'feature_światła-led__cat',
'feature_czujniki-parkowania-przednie__cat'
]
run_model(xgb.XGBRegressor(**xgb_params), feats)

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


# + id="hhO-GwgQRJ4F" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} executionInfo={"status": "ok", "timestamp": 1595625694430, "user_tz": -120, "elapsed": 14131, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="90c52239-f596-4e48-e822-0910059c3798"
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_stan__cat',
'param_rok-produkcji',
'param_faktura-vat__cat',
'param_moc__cat',
'param_skrzynia-biegów__cat',
'param_marka-pojazdu__cat',
'feature_kamera-cofania__cat',
'param_typ__cat',
'param_pojemność-skokowa__cat',
'seller_name__cat',
'param_wersja__cat',
'feature_wspomaganie-kierownicy__cat',
'param_model-pojazdu__cat',
'feature_system-start-stop__cat',
'param_kod-silnika__cat',
'feature_asystent-pasa-ruchu__cat',
'feature_łopatki-zmiany-biegów__cat',
'feature_światła-led__cat',
'feature_czujniki-parkowania-przednie__cat'
]

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

# + id="Ael1bjRfYdAB" colab_type="code" colab={}
df['param_moc']=df['param_moc'].map(lambda x: -1 if str(x) =='None' else int(x.split(' ')[0]))

# + id="UulcDE1OZrc5" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 221} executionInfo={"status": "ok", "timestamp": 1595620656132, "user_tz": -120, "elapsed": 655, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="6445ddc8-db40-4f29-c0df-e8e08b41245e"
df['param_moc']

# + id="co2gTrSbZwDR" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} executionInfo={"status": "ok", "timestamp": 1595620703735, "user_tz": -120, "elapsed": 13455, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="80321f48-a497-4b60-895f-a36d249fa1c8"
feats = [
'param_napęd__cat',
'param_stan__cat',
'param_rok-produkcji',
'param_faktura-vat__cat',
'param_moc',
'param_skrzynia-biegów__cat',
'param_marka-pojazdu__cat',
'feature_kamera-cofania__cat',
'param_typ__cat',
'param_pojemność-skokowa__cat',
'seller_name__cat',
'param_wersja__cat',
'feature_wspomaganie-kierownicy__cat',
'param_model-pojazdu__cat',
'feature_system-start-stop__cat',
'param_kod-silnika__cat',
'feature_asystent-pasa-ruchu__cat',
'feature_łopatki-zmiany-biegów__cat',
'feature_światła-led__cat',
'feature_czujniki-parkowania-przednie__cat'
]

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

# + id="RsJTlrspavYP" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} executionInfo={"status": "ok", "timestamp": 1595620923941, "user_tz": -120, "elapsed": 552, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="501701ba-bc62-480c-dbd6-3461522b9b48"
df['param_pojemność-skokowa'].unique()

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


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

# + id="xuLI3Lx9ba7C" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} executionInfo={"status": "ok", "timestamp": 1595621142281, "user_tz": -120, "elapsed": 13356, "user": {"displayName": "Pawe\u0142", "photoUrl": "", "userId": "07557086783430784441"}} outputId="409c0b02-a7bd-4e8b-df29-cbe2861041ba"
feats = [
'param_napęd__cat',
'param_stan__cat',
'param_rok-produkcji',
'param_faktura-vat__cat',
'param_moc',
'param_skrzynia-biegów__cat',
'param_marka-pojazdu__cat',
'feature_kamera-cofania__cat',
'param_typ__cat',
'param_pojemność-skokowa',
'seller_name__cat',
'param_wersja__cat',
'feature_wspomaganie-kierownicy__cat',
'param_model-pojazdu__cat',
'feature_system-start-stop__cat',
'param_kod-silnika__cat',
'feature_asystent-pasa-ruchu__cat',
'feature_łopatki-zmiany-biegów__cat',
'feature_światła-led__cat',
'feature_czujniki-parkowania-przednie__cat'
]

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

StarcoderdataJupyterScriptsDedupFiltered