<jupyter_start><jupyter_text>Features Engineering<jupyter_code>SUFFIX_CAT = '__cat'
for feat in df.columns:
  if isinstance(df[feat][0], list): continue # skip list columns
  factorized_values = df[feat].factorize()[0]
  if SUFFIX_CAT in feat: # guard against multiply restarts of this notebook
    df[feat] = factorized_values
  else:
    df[feat + SUFFIX_CAT] = factorized_values

cat_feats0 = [x for x in df.columns if SUFFIX_CAT in x]
cat_feats = [x for x in cat_feats0 if 'price' not in x]
len(cat_feats)

df['param_rok-produkcji_NUM'] = df['param_rok-produkcji'].map(lambda r: -1 if r is None else int(r))
df['param_moc_NUM'] = df['param_moc'].map(lambda x: -1 if x is None else int(x.split(' ')[0]) )
df['param_pojemność-skokowa_NUM'] = df['param_pojemność-skokowa'].map(lambda x: -1 if x is None else int(x.split('cm')[0].replace(' ','')) )
df.boxplot(column='price_value',by='param_rok-produkcji_NUM', figsize=(100,10)) 
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') # runs fit and cross-validation
  return np.mean(scores), np.std(scores)
feats4 = ['param_stan__cat',
'param_rok-produkcji_NUM',
'param_napęd__cat',
'param_faktura-vat__cat',
'param_moc_NUM',
'param_skrzynia-biegów__cat',
'param_marka-pojazdu__cat',
'feature_kamera-cofania__cat',
'param_typ__cat',
'param_pojemność-skokowa_NUM',
'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']

xgb_params ={'max_depth':5, 'n_estimators':50, 'learning_rate':0.1, 'seed':0}
run_model(xgb.XGBRegressor(**xgb_params, objective="reg:squarederror"), feats4)<jupyter_output><empty_output><jupyter_text>Hyperopt<jupyter_code>def objective_func(params):
  print("Training with params: ")
  print(params)
  mean_mae, score_std = run_model(xgb.XGBRegressor(**params),feats4)
  return {'loss': np.abs(mean_mae), 'status': STATUS_OK}
# parameter space
xgb_reg_params = {
    'learning_rate':    hp.choice('learning_rate', np.arange(0.05, 0.31, 0.05)),
    'max_depth':        hp.choice('max_depth', np.arange(5, 16, 1, dtype=int)),
    'subsample':        hp.quniform('subsample', 0.5, 1, 0.05),
    'colsample_bytree': hp.quniform('colsample_bytree', 0.5, 1, 0.05),
    'objective':        'reg:squarederror',
    'n_estimators':     100,
    'seed': 0,
}
best = fmin(objective_func, xgb_reg_params, algo = tpe.suggest, max_evals=25)
best<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15433012583271652, 'fraction_numerical': 0.020355292376017766, 'mean_word_length': 6.305405405405406, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '365734', 'license': 'MIT', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1088, 'n_words': 189, 'path': 'day5.ipynb', 'repo_name': 'rafald/dw_matrix_car', 'size': '2702'}