# ---
# 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="qQjfwiaBNwVq" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 442} outputId="2dc2b9a0-eaa2-40aa-ed94-608ea76d8322" executionInfo={"status": "ok", "timestamp": 1583688930721, "user_tz": -60, "elapsed": 11490, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
# !pip install --upgrade tables
# !pip install eli5

# + id="tmZGPwE5M7PA" colab_type="code" colab={}
import pandas as pd
import numpy as np
from sklearn.dummy import DummyRegressor
from sklearn.tree import DecisionTreeRegressor

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

import eli5
from eli5.sklearn import PermutationImportance

# + id="jnBh8R2cNcaD" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="88d49554-93ab-4c7b-b0fb-a79db87834d7" executionInfo={"status": "ok", "timestamp": 1583689159863, "user_tz": -60, "elapsed": 836, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
# cd "/content/drive/My Drive/Colab Notebooks/dw_matrix/matrix_two/dw_matrix_car"

# + [markdown] id="IiTMIZBoOTLu" colab_type="text"
# ## Wczytywanie danych
#

# + id="ZoZ9s1SIOW3a" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="16b2bcac-1585-4238-d412-c72b9f543283" executionInfo={"status": "ok", "timestamp": 1583689188624, "user_tz": -60, "elapsed": 4397, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
df = pd.read_hdf('data/car.h5')
df.shape

# + id="scl5HBEpOzMy" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 187} outputId="7e664574-909e-4729-8fdc-912e9712b5e6" executionInfo={"status": "ok", "timestamp": 1583689197411, "user_tz": -60, "elapsed": 1116, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
df.columns

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


# + [markdown] id="Z4tUmvMwO7EH" colab_type="text"
# ## Dummy Model

# + id="k_N1Cv8WO9X8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="30744f7f-b887-4590-879e-0b1531712236" executionInfo={"status": "ok", "timestamp": 1583689255920, "user_tz": -60, "elapsed": 816, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
df.select_dtypes(np.number).columns

# + id="hGX_nrZcPEgS" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="22e2af37-aa62-4d7c-9b9c-9c45f665e983" executionInfo={"status": "ok", "timestamp": 1583692444821, "user_tz": -60, "elapsed": 806, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
feats = ['car_id']
X = df[ feats ].values
y = df['price_value'].values

model = DummyRegressor()
model.fit(X, y)
y_pred = model.predict(X)

mae(y, y_pred)

# + id="x5_QjaD5PdeC" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="59cbc8b3-e6e1-4417-e2e6-c450398300fe" executionInfo={"status": "ok", "timestamp": 1583692797593, "user_tz": -60, "elapsed": 1185, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
[x for x in df.columns if 'price' in x]

# + id="tFvSiXfxclEA" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="10e31acd-e79e-4224-a63a-358fc733d5c0" executionInfo={"status": "ok", "timestamp": 1583692818106, "user_tz": -60, "elapsed": 1336, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
df['price_currency'].value_counts()

# + id="bKynYA_fcqDN" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="dd67ffff-b98b-4c79-c5eb-6325eef4b283" executionInfo={"status": "ok", "timestamp": 1583693493690, "user_tz": -60, "elapsed": 1812, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
df['price_currency'].value_counts(normalize=True) * 100  # Żeby zobaczyć jaki procent stanowią wartości wyrażone w EUR

# + id="OlfCfRcefO31" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="357cd9a5-d6dc-4782-bfe0-2eb8787514e7" executionInfo={"status": "ok", "timestamp": 1583693675316, "user_tz": -60, "elapsed": 861, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
df = df[ df['price_currency'] != 'EUR' ]
df.shape

# + [markdown] id="ewLxajTRgar5" colab_type="text"
# Features

# + id="r5izDDAgf2uK" 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


  # print(feat)

# + id="s-g8eBMlggsb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="3d5c3e7c-a5d1-4cad-8564-4383903ee153" executionInfo={"status": "ok", "timestamp": 1583697095681, "user_tz": -60, "elapsed": 845, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
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="HSH_CDcwrv0B" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="ca42445b-51bf-4e7a-d288-a92169682ce7" executionInfo={"status": "ok", "timestamp": 1583700924439, "user_tz": -60, "elapsed": 5803, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
X = df[cat_feats].values
y = df['price_value'].values

model = DecisionTreeRegressor(max_depth=5)
scores = cross_val_score(model, X, y, cv=3, scoring='neg_mean_absolute_error')
np.mean(scores)

# + id="e8X1-EDotdiy" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 391} outputId="a23f51b9-d628-41d0-e4db-2046f3297508" executionInfo={"status": "ok", "timestamp": 1583701190106, "user_tz": -60, "elapsed": 50641, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13310166437012434645"}}
m = DecisionTreeRegressor(max_depth=5)
m.fit(X, y)


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

# + id="jQ1b5ADQ8D-Z" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered