# ---
# 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="6NkNeww6uBnC" colab_type="code" colab={}
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import cross_val_score

# + id="bnciBrJhuoCd" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="d6119bed-f02f-4ccc-9837-970db0720716" executionInfo={"status": "ok", "timestamp": 1581634066281, "user_tz": -60, "elapsed": 403, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
# cd "/content/drive/My Drive/Colab Notebooks/dw_matrix"

# + id="yQ_8yU6FvJTT" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="7a766a17-9b41-47f3-c908-0fedd1b4da4d" executionInfo={"status": "ok", "timestamp": 1581634072420, "user_tz": -60, "elapsed": 1532, "user": {"displayName": "Micha\u0142 Klimczak", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
# ls data

# + id="62UuhxAIvKkD" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="32c04ef8-face-4d3e-c337-eb690a98c14a" executionInfo={"status": "ok", "timestamp": 1581634143647, "user_tz": -60, "elapsed": 2291, "user": {"displayName": "Micha\u01<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
df = pd.read_csv('data/shoes_prices.csv')

# + id="z-1rva1FvbwK" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="5d8fae2b-5c53-46dc-d45a-acd1c50de67b" executionInfo={"status": "ok", "timestamp": 1581634168983, "user_tz": -60, "elapsed": 612, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
df.shape

# + id="OtTfvu8OviP1" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 221} outputId="97b195f1-0440-4997-c4e2-718cdd355ef2" executionInfo={"status": "ok", "timestamp": 1581634194451, "user_tz": -60, "elapsed": 536, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
df.columns

# + id="TBbL6YvPvolx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="4b9c753e-f89b-495a-e19b-2024ccd0697f" executionInfo={"status": "ok", "timestamp": 1581634289490, "user_tz": -60, "elapsed": 436, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
mean_prices = np.mean(df['prices_amountmin'])
mean_prices

# + id="LSCQJyc7v7La" colab_type="code" colab={}
y_true = df['prices_amountmin']
y_pred =[mean_prices]* y_true.shape[0]

# + id="LVjWUGWawTOb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="506c84a6-fee6-4a18-f6e8-537a4187d2a1" executionInfo={"status": "ok", "timestamp": 1581634448833, "user_tz": -60, "elapsed": 447, "user": {"displayName": "Micha\u014<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
mean_absolute_error(y_true, y_pred)

# + id="UsjwSZc8wmts" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="83e982a4-de52-4066-ad7e-401b90f15d38" executionInfo={"status": "ok", "timestamp": 1581634588647, "user_tz": -60, "elapsed": 633, "user": {"displayName": "Micha\u01<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
df['prices_amountmin'].hist(bins = 100)

# + id="3Ry0pLHzxIzk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="9e0fcc94-a37e-46da-9c55-cf040d6318ed" executionInfo={"status": "ok", "timestamp": 1581634727651, "user_tz": -60, "elapsed": 657, "user": {"displayName": "Micha\u0142 Klimczak", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
np.log1p(df['prices_amountmin']).hist(bins=100)

# + id="VVmIhAC6xYzh" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="168fb6d1-b654-462a-f5a6-8d0689238bc8" executionInfo={"status": "ok", "timestamp": 1581635108519, "user_tz": -60, "elapsed": 487, "user": {"displayName": "Micha\u014<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
y_true = df['prices_amountmin']
price_log_mean =np.expm1( np.mean(np.log1p(y_true)))
y_pred =[price_log_mean]* y_true.shape[0]
mean_absolute_error(y_true, y_pred)

# + id="0UOiN8agyC0b" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 221} outputId="619287ef-228f-4038-9d68-38b93ebedbcf" executionInfo={"status": "ok", "timestamp": 1581635141607, "user_tz": -60, "elapsed": 514, "user": {"displayName": "Micha\u0142 Klimczak", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
df.columns

# + id="2oRKyl3219SO" colab_type="code" colab={}
df['manu_cat']=df.manufacturer.factorize()[0]

# + id="6B2Ee917zP1R" colab_type="code" colab={}
df['brand_cat']=df.brand.factorize()[0]

# + id="cLjI92xg2iQV" colab_type="code" colab={}
df['feat_cat']=df.features.factorize()[0]

# + id="CkFmq1N4zYF7" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="49de7360-838b-4421-d863-094e6cb63ca7" executionInfo={"status": "ok", "timestamp": 1581635647840, "user_tz": -60, "elapsed": 518, "user": {"displayName": "Micha\u0142 Klimczak", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
feats = ['brand_cat']
X = df[feats].values
y = df['prices_amountmin'].values

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


# + id="3NEvBuLl1LbP" colab_type="code" colab={}
def run_model(feats):
  X = df[feats].values
  y = df['prices_amountmin'].values
  model = DecisionTreeRegressor(max_depth=5)
  scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error')
  return np.mean(scores), np.std(scores)


# + id="Go0eAsPd1nPP" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="8fd248e5-a98c-4a6e-f850-cbb816156f5b" executionInfo={"status": "ok", "timestamp": 1581635781990, "user_tz": -60, "elapsed": 465, "user": {"displayName": "Micha\u0142 Klimczak", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
run_model(['brand_cat'])

# + id="AvSWa_Jl1sMF" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="7e58695f-0161-409a-932a-04c4f0b04bcf" executionInfo={"status": "ok", "timestamp": 1581635896391, "user_tz": -60, "elapsed": 408, "user": {"displayName": "Micha\u0142 Klimczak", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
run_model(['manu_cat'])

# + id="FObfuTHf2y1I" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e0e8aa26-4083-42ea-83e0-931d678de4e2" executionInfo={"status": "ok", "timestamp": 1581636087421, "user_tz": -60, "elapsed": 422, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
run_model(['feat_cat'])

# + id="oF0_Oyn32IIf" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="ddd0f959-55bf-4111-9721-4a827b905a02" executionInfo={"status": "ok", "timestamp": 1581635930587, "user_tz": -60, "elapsed": 529, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
run_model(['brand_cat','manu_cat'])

# + id="R-HZaWwE2QdR" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6936ecb8-c33d-45e9-cc82-ed61edabea88" executionInfo={"status": "ok", "timestamp": 1581636105054, "user_tz": -60, "elapsed": 671, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
run_model(['brand_cat','manu_cat','feat_cat'])

# + id="Hu2HEN4m27Ak" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="2963ea23-9f73-4ab4-91b8-30aec3ecf28b" executionInfo={"status": "ok", "timestamp": 1581636420355, "user_tz": -60, "elapsed": 1521, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
# !git add /drive/My Drive/Colab Notebooks/day4_meta.ipynb

# + id="8R6GqTPH3fWl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6df53528-c38f-4847-813c-35ea8dc77db1" executionInfo={"status": "ok", "timestamp": 1581636274038, "user_tz": -60, "elapsed": 4227, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBpD8i4oklMcS_RtbV2dHMCza0WsbqsgtzWm_Y1Kw=s64", "userId": "14327755442440161367"}}
# ls

# + id="5VqsEE1Z3jZT" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered