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

# + id="heW1hyLKwl4t" 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="iVPEgBS_xq4v" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="40f73e05-0b06-41ec-d8cc-6fe7f1f1a0f9" executionInfo={"status": "ok", "timestamp": 1583362849026, "user_tz": -60, "elapsed": 603, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
# cd "/content/drive/My Drive/Colab Notebooks/matrix_two/dw_matrix_cars"

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

# + id="9Sxrfn9ExniI" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="3ff8d5ee-a4da-4da2-f079-dbe88b47f7d4" executionInfo={"status": "ok", "timestamp": 1583362885474, "user_tz": -60, "elapsed": 4371, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
df = pd.read_hdf('data/car.h5')
df.shape

# + id="FaSSZi3OyEmZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 185} outputId="6ac8098d-cbdf-4dd9-a3d5-58300cffeda0" executionInfo={"status": "ok", "timestamp": 1583362891524, "user_tz": -60, "elapsed": 609, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
df.columns

# + [markdown] id="D7I2D-VCy6lp" colab_type="text"
# ## Dummy Model

# + id="oR-HIDoByHDY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="9fc76dfc-fe32-4c49-889c-bad8b0b87c7d" executionInfo={"status": "ok", "timestamp": 1583363143108, "user_tz": -60, "elapsed": 933, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
df.select_dtypes(np.number).columns # szukamy cech tylpu int lub float

# + id="gZMtzpCUzKCU" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="19abc50a-f15b-498c-b134-b028222f20cb" executionInfo={"status": "ok", "timestamp": 1583363290198, "user_tz": -60, "elapsed": 639, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
feats = [ 'car_id' ]
X = df[ feats ].values
y = df[ 'price_value' ].values

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

mae(y,y_pred) # mierzy jakość: średnia z różnicy między wartością oryginalną a przewidywaną

# + id="78y64zBaz8S2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="33c181a9-9367-489c-cde6-fc29f792fbe4" executionInfo={"status": "ok", "timestamp": 1583363393930, "user_tz": -60, "elapsed": 579, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
[x for x in df.columns if 'price' in x]

# + id="MFLeoXJ_0BLp" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 67} outputId="0a9bcfe7-742a-41e8-cd61-2652fb27b51e" executionInfo={"status": "ok", "timestamp": 1583363528467, "user_tz": -60, "elapsed": 689, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
df['price_currency'].value_counts() # powinno się znormalizować do jednej waluty np. przemnożyć przez kurs

# + id="ewaZk2eg0hqn" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="8c2b171a-ea6b-49cb-8f9c-d2f0ad59c75d" executionInfo={"status": "ok", "timestamp": 1583363577930, "user_tz": -60, "elapsed": 606, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
df = df [ df['price_currency'] != 'EUR']  # ale tu usuwamy euro bo jest ich bardzo mała ilość
df.shape

# + [markdown] id="5yAx_sjL07FJ" colab_type="text"
# ## Features

# + id="vswtxZdb02K8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="20e2979e-80cc-4a97-b4db-42d648a4eb2f" executionInfo={"status": "ok", "timestamp": 1583363680938, "user_tz": -60, "elapsed": 2263, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
for feat in df.columns:
  print(feat)

# + id="_jq21eUi1IeV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="fa0e1163-fa03-49ea-aa12-753be168866b" executionInfo={"status": "ok", "timestamp": 1583363770008, "user_tz": -60, "elapsed": 2018, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
df['param_color'].factorize()[0] # zamieniamy stringi na liczby (id)

# + id="X02vew2P3KOr" 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 + SUFIX_CAT ] = factorized_values

# + id="yKmmRs7u4Yfz" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="ad64d2b5-1eb7-46a8-f99f-3c2dc09a42d0" executionInfo={"status": "ok", "timestamp": 1583365127022, "user_tz": -60, "elapsed": 767, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
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="cQ-CGtqR5k8t" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e3c959fb-a13c-4870-9e0a-d16b99b4d879" executionInfo={"status": "ok", "timestamp": 1583365135147, "user_tz": -60, "elapsed": 4962, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
X = df[cat_feats].values
y = df['price_value'].values

model = DecisionTreeRegressor(max_depth=5) # trzeba uważać na za dużą głąbokość drzewa
scores = cross_val_score(model, X, y, cv=3, scoring='neg_mean_absolute_error' )
np.mean(scores)

# + id="MnxD4xeZ6rKi" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 386} outputId="47ac1be7-6c1e-4047-a401-703ce4395db6" executionInfo={"status": "ok", "timestamp": 1583365529250, "user_tz": -60, "elapsed": 43090, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "00941150960329998400"}}
# Sprawdzamy, które cechy są ważne

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 )

StarcoderdataJupyterScriptsDedupFiltered