<jupyter_start><jupyter_text><jupyter_code>!pip install --upgrade tables
!pip install eli5
!pip install xgboost
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
!pwd
cd '/content/drive/My Drive/Colab Notebooks/matrix/matrix_two/dw_matrix_car'
df = pd.read_hdf('data/car.h5')
df.shape<jupyter_output><empty_output><jupyter_text>Feature engineering<jupyter_code>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
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)
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)
run_model(DecisionTreeRegressor(max_depth=5), cat_feats)<jupyter_output><empty_output><jupyter_text>RandomForest<jupyter_code>model = RandomForestRegressor(max_depth=5, n_estimators=50, random_state=0)
run_model(model, cat_feats)<jupyter_output><empty_output><jupyter_text>XGBoost<jupyter_code>xgb_params = {
    'max_depth': 5,
    'n_estimators': 50,
    'learning_rate': 0.1,
    'seed': 0
}


run_model(xgb.XGBRegressor(**xgb_params), cat_feats )
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)
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 )
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 )
df['param_moc'].map(lambda x: -1 if str(x) == 'None' else int(str(x).replace(' ', '').replace('KM','')) )
df['param_rok-produkcji'] = df['param_rok-produkcji'].map(lambda x: -1 if str(x) == 'None' else int(x) )
df['param_moc'] = df['param_moc'].map(lambda x: -1 if str(x) == 'None' else int(str(x).replace(' ', '').replace('KM','')) )
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 )
df['param_pojemność-skokowa'] = df['param_pojemność-skokowa'].map(lambda x: -1 if str(x) == 'None' else int(str(x).replace(' ', '').replace('cm3','')) )
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 )
ls<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15015447991761072, 'fraction_numerical': 0.006591143151390319, 'mean_word_length': 10.137614678899082, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '542627', 'license': 'MIT', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1959, 'n_words': 213, 'path': 'day4.ipynb', 'repo_name': 'aranjaka/dw_matrix_car', 'size': '4855'}