<jupyter_start><jupyter_text>Feature Engineering<jupyter_code>SUFFIX_CAT = '__cat'

for feature in df.columns:
  if isinstance(df[feature][0], list): continue

  factorized_values = df[feature].factorize()[0]
  if SUFFIX_CAT in feature:
    df[feature] = factorized_values
  else:
    df[feature + SUFFIX_CAT] = factorized_values
cat_features = [x for x in df.columns if SUFFIX_CAT in x]
cat_features = [x for x in cat_features if 'price' not in x]
len(cat_features)
def run_model(model, features):
  X = df[features].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)<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>model = DecisionTreeRegressor(max_depth=5)
run_model(model, cat_features)<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_features)<jupyter_output><empty_output><jupyter_text>XGBoost<jupyter_code>xgb_params = {
    'max_depth': 5,
    'n_estimators': 50,
    'learning_rate': 0.1,
    'seed': 0
}

model = xgb.XGBRegressor(**xgb_params)
run_model(model, cat_features)
m = xgb.XGBRegressor(**xgb_params)
m.fit(X, y)

imp = PermutationImportance(m, random_state=0).fit(X, y)
eli5.show_weights(imp, feature_names=cat_features)
features = [
'param_napęd__cat',
'param_rok-produkcji__cat',
'param_stan__cat',
'param_skrzynia-biegów__cat',
'param_faktura-vat__cat',
'param_moc__cat',
'param_marka-pojazdu__cat',
'feature_kamera-cofania__cat',
'param_typ__cat',
'param_pojemność-skokowa__cat',
'seller_name__cat',
'feature_wspomaganie-kierownicy__cat',
'param_model-pojazdu__cat',
'param_wersja__cat',
'param_kod-silnika__cat',
'feature_system-start-stop__cat',
'feature_asystent-pasa-ruchu__cat',
'feature_czujniki-parkowania-przednie__cat',
'feature_łopatki-zmiany-biegów__cat',
'feature_regulowane-zawieszenie__cat']

run_model(model, features)
df['param_rok-produkcji'].unique()
df['param_moc'].unique()
df['param_pojemność-skokowa'].unique()
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).split(' ')[0]))
df['param_pojemność-skokowa'] = df['param_pojemność-skokowa'].map(lambda x: -1 if str(x) == 'None' else int(str(x).split('cm')[0].replace(' ', '')))

num_features = ['param_rok-produkcji', 'param_moc', 'param_pojemność-skokowa']
for feature in num_features:
  features[features.index(feature + '__cat')] = feature

run_model(model, features)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup