<jupyter_start><jupyter_text>Feature Engineering<jupyter_code>SUFFIX_CAT = '__cat'
for feat in df.columns:
  if isinstance(df[feat][0], list): continue

  factorizes_values = df[feat].factorize()[0]
  if SUFFIX_CAT in feat:
    df[feat] = factorizes_values
  else:
    # przypisanie id do każdego koloru
    df[feat + SUFFIX_CAT] = df[feat].factorize()[0]
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

  # neg_mean_absolute_error = wyniki, które dostajemy, będą też miały wartość ujemną
  scores = cross_val_score(model, X, y, cv = 3, scoring = 'neg_mean_absolute_error')
  return np.mean(scores), np.std(scores)<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>run_model(DecisionTreeRegressor(max_depth=5), cat_feats)<jupyter_output><empty_output><jupyter_text>Random Forest<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 = 5, 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_skrzynia-biegów__cat","param_faktura-vat__cat","param_rok-produkcji__cat","param_stan__cat","feature_kamera-cofania__cat","param_moc__cat","param_pojemność-skokowa__cat","feature_czujniki-parkowania-przednie__cat","feature_regulowane-zawieszenie__cat","feature_wspomaganie-kierownicy__cat","feature_asystent-pasa-ruchu__cat","feature_światła-led__cat","feature_łopatki-zmiany-biegów__cat","seller_name__cat","feature_bluetooth__cat","param_marka-pojazdu__cat","feature_system-start-stop__cat","feature_światła-do-jazdy-dziennej__cat","param_kraj-pochodzenia__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_skrzynia-biegów__cat","param_faktura-vat__cat","param_rok-produkcji","param_stan__cat","feature_kamera-cofania__cat","param_moc__cat","param_pojemność-skokowa__cat","feature_czujniki-parkowania-przednie__cat","feature_regulowane-zawieszenie__cat","feature_wspomaganie-kierownicy__cat","feature_asystent-pasa-ruchu__cat","feature_światła-led__cat","feature_łopatki-zmiany-biegów__cat","seller_name__cat","feature_bluetooth__cat","param_marka-pojazdu__cat","feature_system-start-stop__cat","feature_światła-do-jazdy-dziennej__cat","param_kraj-pochodzenia__cat"]

run_model(xgb.XGBRegressor(**xgb_params), feats)
df['param_moc'].unique()
df['param_moc'] = df['param_moc'].map(lambda x: -1 if str(x) == 'None' else int(x.split(' ')[0]))
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_skrzynia-biegów__cat","param_faktura-vat__cat","param_rok-produkcji","param_stan__cat","feature_kamera-cofania__cat","param_moc","param_pojemność-skokowa","feature_czujniki-parkowania-przednie__cat","feature_regulowane-zawieszenie__cat","feature_wspomaganie-kierownicy__cat","feature_asystent-pasa-ruchu__cat","feature_światła-led__cat","feature_łopatki-zmiany-biegów__cat","seller_name__cat","feature_bluetooth__cat","param_marka-pojazdu__cat","feature_system-start-stop__cat","feature_światła-do-jazdy-dziennej__cat","param_kraj-pochodzenia__cat"]

run_model(xgb.XGBRegressor(**xgb_params), feats)
df['param_pojemność-skokowa'] = df['param_pojemność-skokowa__cat'].map(lambda x: -1 if str(x) == 'None' else int(str(x).split('cm')[0].replace(' ', '')))
feats = ["param_napęd__cat","param_skrzynia-biegów__cat","param_faktura-vat__cat","param_rok-produkcji","param_stan__cat","feature_kamera-cofania__cat","param_moc","param_pojemność-skokowa","feature_czujniki-parkowania-przednie__cat","feature_regulowane-zawieszenie__cat","feature_wspomaganie-kierownicy__cat","feature_asystent-pasa-ruchu__cat","feature_światła-led__cat","feature_łopatki-zmiany-biegów__cat","seller_name__cat","feature_bluetooth__cat","param_marka-pojazdu__cat","feature_system-start-stop__cat","feature_światła-do-jazdy-dziennej__cat","param_kraj-pochodzenia__cat"]

run_model(xgb.XGBRegressor(**xgb_params), feats)<jupyter_output>[16:49:44] WARNING: /workspace/src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.
[16:49:48] WARNING: /workspace/src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.
[16:49:52] WARNING: /workspace/src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.
{'fraction_non_alphanumeric': 0.14652384801940177, 'fraction_numerical': 0.011115602263540825, 'mean_word_length': 10.403225806451612, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '566930', 'license': 'MIT', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1996, 'n_words': 200, 'path': 'day4_XGBoost_model.ipynb', 'repo_name': 'KasiaOtko/dw_matrix_car', 'size': '4948'}