<jupyter_start><jupyter_text>Wczytanie danych<jupyter_code>df = pd.read_hdf('data/car.h5')
df.shape
# Skupiam się jedynie na samochodach używanych
df = df[df['param_stan'] == 'Używane']
df.shape<jupyter_output><empty_output><jupyter_text>Feature Engineering Druga iteracja<jupyter_code>def extract_number(text):
  if text:
    return int(re.findall('[0-9]+', str(text).replace(' ', '').replace(',', ''))[0])
  else:
    return -1
df['param_rok-produkcji'] = df['param_rok-produkcji'].map(lambda x: extract_number(x))
df['param_moc'] = df['param_moc'].map(lambda x: extract_number(x))
# moc > 1000 jest dzielona przez 100
df['param_moc'] = df['param_moc'].map(lambda x: int(x/100) if x > 1000 else x)
df['param_pojemność-skokowa'] = df['param_pojemność-skokowa'].map(lambda x: extract_number(x))
# pojemność-skokowa > 10000 jest dzielona przez 100
df['param_pojemność-skokowa'] = df['param_pojemność-skokowa'].map(lambda x: int(x/100) if x > 10000 else x)
# zamiana euro na pln
df['price_value_pln'] = df.apply(lambda x: x.price_value if x.price_currency == 'EUR' else x.price_value, axis = 1)
SUFFIX_CAT = '__cat'
for feat in df.columns:
  if isinstance(df[feat].iloc[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)
feats = [x for x in cat_feats if x not in ['param_rok-produkcji__cat', 'param_moc__cat', 'param_pojemność-skokowa__cat']]
feats += ['param_rok-produkcji', 'param_moc', 'param_pojemność-skokowa']
len(feats)<jupyter_output><empty_output><jupyter_text>XGBoost<jupyter_code>def run_model(data, model, feats):
  X = data[feats].values
  y = data['price_value_pln'].values

  scores = cross_val_score(model, X, y, cv=3, scoring='neg_mean_absolute_error')
  return np.mean(scores), np.std(scores)
feats_top = [
'param_rok-produkcji',
'param_moc',
'param_pojemność-skokowa',
'param_marka-pojazdu__cat',
'param_napęd__cat',
'param_rodzaj-paliwa__cat',
'param_typ__cat',
'param_uszkodzony__cat',
'param_kod-silnika__cat',
'seller_name__cat',
'param_wersja__cat',
'param_faktura-vat__cat',
'param_model-pojazdu__cat',
'feature_czujniki-parkowania-przednie__cat',
'param_skrzynia-biegów__cat',
'param_bezwypadkowy__cat',
'param_kraj-pochodzenia__cat',
'feature_kamera-cofania__cat',
'feature_asystent-pasa-ruchu__cat',
'feature_system-start-stop__cat',
'feature_regulowane-zawieszenie__cat',
'param_liczba-miejsc__cat',
'feature_światła-led__cat',
'seller_address__cat',
'feature_nawigacja-gps__cat',
]

xgb_params = {
    'max_depth': 5,
    'n_estimators': 50,
    'learning_rate': 0.1,
    'seed': 0,
    'objective': 'reg:squarederror',
    'tree_method': 'gpu_hist',
}

model = xgb.XGBRegressor(**xgb_params)
run_model(df, model, feats_top)<jupyter_output><empty_output><jupyter_text>Hyperopt<jupyter_code>def obj_func(params):
  print("Training with params: ")
  print(params)

  mean_mae,score_std = run_model(df, xgb.XGBRegressor(**params), feats_top)

  print('MAE: {}'.format(np.abs(mean_mae)))
  print('---------------- \n')

  return {'loss': np.abs(mean_mae), 'status': STATUS_OK}

# 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),
    'n_estimators': hp.choice('n_estimators', np.arange(60,140,1, dtype=int)),
    'objective': 'reg:squarederror',
    'tree_method': 'gpu_hist',
    'seed': 0,
}

# run
best = fmin(obj_func, xgb_reg_params, algo=tpe.suggest, max_evals=30, return_argmin=False)

print('---------------- \n')

best<jupyter_output>Training with params: 
{'colsample_bytree': 0.6000000000000001, 'learning_rate': 0.25, 'max_depth': 12, 'n_estimators': 114, 'objective': 'reg:squarederror', 'seed': 0, 'subsample': 0.6000000000000001, 'tree_method': 'gpu_hist'}
MAE: 7992.9620936197825
---------------- 

Training with params: 
{'colsample_bytree': 0.6000000000000001, 'learning_rate': 0.3, 'max_depth': 6, 'n_estimators': 71, 'objective': 'reg:squarederror', 'seed': 0, 'subsample': 0.8500000000000001, 'tree_method': 'gpu_hist'}
MAE: 7561.473382126637
---------------- 

Training with params: 
{'colsample_bytree': 0.9, 'learning_rate': 0.1, 'max_depth': 7, 'n_estimators': 104, 'objective': 'reg:squarederror', 'seed': 0, 'subsample': 0.8, 'tree_method': 'gpu_hist'}
MAE: 6882.390526942246
---------------- 

Training with params: 
{'colsample_bytree': 0.55, 'learning_rate': 0.05, 'max_depth': 5, 'n_estimators': 61, 'objective': 'reg:squarederror', 'seed': 0, 'subsample': 0.8, 'tree_method': 'gpu_hist'}
MAE: 9376.056787191328
[...]<jupyter_text>Final<jupyter_code>feats_top = [
'param_rok-produkcji',
'param_moc',
'param_pojemność-skokowa',
'param_marka-pojazdu__cat',
'param_rodzaj-paliwa__cat',
'param_napęd__cat',
'param_uszkodzony__cat',
'param_typ__cat',
'param_kod-silnika__cat',
'seller_name__cat',
'param_wersja__cat',
'param_faktura-vat__cat',
'feature_czujniki-parkowania-przednie__cat',
'param_model-pojazdu__cat',
'param_skrzynia-biegów__cat',
'param_bezwypadkowy__cat',
'feature_kamera-cofania__cat',
'param_kraj-pochodzenia__cat',
'feature_asystent-pasa-ruchu__cat',
'feature_łopatki-zmiany-biegów__cat',
'seller_address__cat',
'feature_system-start-stop__cat',
'param_liczba-miejsc__cat',
'feature_regulowane-zawieszenie__cat',
'feature_nawigacja-gps__cat',
]

xgb_params = {
 'colsample_bytree': 1.0,
 'learning_rate': 0.05,
 'max_depth': 13,
 'n_estimators': 124,
 'objective': 'reg:squarederror',
 'seed': 0,
 'subsample': 0.6000000000000001,
 'tree_method': 'gpu_hist'}

model = xgb.XGBRegressor(**xgb_params)
run_model(df, model, feats_top)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17483577564426478, 'fraction_numerical': 0.046824995789119084, 'mean_word_length': 6.450439146800502, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '532209', 'license': 'MIT', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2474, 'n_words': 397, 'path': 'day5_hyperopt.ipynb', 'repo_name': 'bsekiewicz/dw_matrix_car', 'size': '5937'}