# ---
# 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="DrF0A58nys1f" colab_type="code" outputId="44d93cc9-25f7-46ee-c2b3-cf88e3041230" executionInfo={"status": "ok", "timestamp": 1583696601935, "user_tz": -60, "elapsed": 9853, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}} colab={"base_uri": "https://localhost:8080/", "height": 462}
# !pip install --upgrade tables
# !pip install eli5

# + id="4Ng5B1F7J2zr" colab_type="code" colab={}
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

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="0-sO7qwEKI7q" colab_type="code" outputId="5c8c9366-6796-4af1-83fa-f49c757e1961" executionInfo={"status": "ok", "timestamp": 1583697196675, "user_tz": -60, "elapsed": 1419, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}} colab={"base_uri": "https://localhost:8080/", "height": 34}
# cd /content/drive/My Drive/Colab Notebooks/matrix/matrix_two/dw_matrix_car

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

# + id="n4w4p7YsK_mV" colab_type="code" colab={}
df = pd.read_hdf('data/car.h5')


# + id="OINfdCcZLDq1" colab_type="code" outputId="198d4ca1-f672-4267-f798-8d4e0f564ec5" executionInfo={"status": "ok", "timestamp": 1583698772307, "user_tz": -60, "elapsed": 593, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}} colab={"base_uri": "https://localhost:8080/", "height": 187}
df.columns

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


# + [markdown] id="WRPUGDraLnAk" colab_type="text"
# ## Dummy model

# + id="g_CFOBeCLovU" colab_type="code" outputId="37cbfaa0-5386-41af-cda0-f624cf903f75" executionInfo={"status": "ok", "timestamp": 1583697208464, "user_tz": -60, "elapsed": 832, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}} colab={"base_uri": "https://localhost:8080/", "height": 34}
df.select_dtypes(np.number).columns

# + id="UtmUISABLuAN" colab_type="code" outputId="84df3966-4489-47de-8aac-e4e080bc9087" executionInfo={"status": "ok", "timestamp": 1583697209875, "user_tz": -60, "elapsed": 551, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}} colab={"base_uri": "https://localhost:8080/", "height": 34}
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="jAgLwzJ5MQit" colab_type="code" outputId="c123b65a-d1da-4316-964e-e953f2e7609e" executionInfo={"status": "ok", "timestamp": 1583672222880, "user_tz": -60, "elapsed": 573, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}} colab={"base_uri": "https://localhost:8080/", "height": 34}
[x for x in df.columns if 'price' in x]

# + id="99aYTWn2OGA3" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="3994b829-98a3-4078-eb08-8fbb05ead416" executionInfo={"status": "ok", "timestamp": 1583699708673, "user_tz": -60, "elapsed": 573, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}}
df['price_currency'].value_counts()

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


# + id="udazE7-StcY3" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="5b460161-c4d1-41e3-978a-c9d29b2f7d3f" executionInfo={"status": "ok", "timestamp": 1583699710124, "user_tz": -60, "elapsed": 736, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}}
df = df[df['price_currency'] !='EUR'] ##usuwnaie wszystkich z EUR
df.shape

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


# + [markdown] id="nHV9PDrkuTR-" colab_type="text"
# ##Features przygotowanie cech

# + id="0_d_ZSvtuWfS" 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="UWNEwbK1vWF2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="dd8a811a-5985-4fd3-8b8f-81816d661af0" executionInfo={"status": "ok", "timestamp": 1583699722460, "user_tz": -60, "elapsed": 1842, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}}
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="Wm-qT6DPwtIk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="195ebc51-7e92-45ff-f64f-e6f3a85abd36" executionInfo={"status": "ok", "timestamp": 1583699738184, "user_tz": -60, "elapsed": 3838, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}}
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="5Ca7dL5oyL-f" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 391} outputId="8b70cbb7-5a0b-4c87-e598-806f9b6982bf" executionInfo={"status": "ok", "timestamp": 1583700403348, "user_tz": -60, "elapsed": 41492, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gg703WJANubHrFbKThPl3igWRF1DAbGMV5VvMA7aA=s64", "userId": "06607875177611101838"}}
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="HzhSfT9W3tKl" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered