<jupyter_start><jupyter_text>PREPARO CSV<jupyter_code>labels=pd.read_csv("../../csv/train_labels.csv")
labels.head()
values=pd.read_csv("../../csv/train_values.csv")
values.head()
values.isnull().values.any()
values = values.dropna()
labels.isnull().values.any()
values.info()
to_be_categorized = ["land_surface_condition", "foundation_type", "roof_type",\
                     "position", "ground_floor_type", "other_floor_type",\
                     "plan_configuration", "legal_ownership_status", "geo_level_1_id"]
for row in to_be_categorized:
    values[row] = values[row].astype("category")
values.info()
datatypes = dict(values.dtypes)
for row in values.columns:
    if datatypes[row] != "int64" and datatypes[row] != "int32" and \
       datatypes[row] != "int16" and datatypes[row] != "int8":
        continue
    if values[row].nlargest(1).item() > 32767 and values[row].nlargest(1).item() < 2**31:
        values[row] = values[row].astype(np.int32)
    elif values[row].nlargest(1).item() > 127:
        values[row] = values[row].astype(np.int16)
    else:
        values[row] = values[row].astype(np.int8)
values.info()
labels.info()
labels["building_id"] = labels["building_id"].astype(np.int32)
labels["damage_grade"] = labels["damage_grade"].astype(np.int8)
labels.info()
important_values = values\
                .merge(labels, on="building_id")
important_values.drop(columns=["building_id"], inplace = True)
important_values.info()
X_train, X_test, y_train, y_test = train_test_split(important_values.drop(columns = 'damage_grade'),
                                        important_values['damage_grade'], test_size = 0.15, random_state = 123)


def feature_engieniere(df):
  df["volume_percentage"] = df["height_percentage"] *  df["area_percentage"]
  df["less_than_25_years"] = df["age"] >= 25
  df["more_than_20_metres"] = df["height_percentage"] <= 20 
  for i in [1,2,3]:
    df[f"geo_{i}_height_avg"] = df.groupby(f"geo_level_{i}_id")["height_percentage"].transform(np.average)
    df[f"geo_{i}_area_avg"] = df.groupby(f"geo_level_{i}_id")["area_percentage"].transform(np.average)
    df[f"geo_{i}_age_avg"] =  df.groupby(f"geo_level_{i}_id")["age"].transform(np.average)
    df[f"geo_{i}_families_avg"] = df.groupby(f"geo_level_{i}_id")["count_families"].transform(np.average)
    df[f"geo_{i}_volume_avg"] = df.groupby(f"geo_level_{i}_id")["volume_percentage"].transform(np.average)
    df[f"geo_{i}_floors_avg"] = df.groupby(f"geo_level_{i}_id")["count_floors_pre_eq"].transform(np.average)
  return df

#X_train = feature_engieniere(X_train)
#X_test = feature_engieniere(X_test)

category_cols = X_train.select_dtypes(include=['category', "bool"]).columns.to_list()
numeric_cols = X_train.select_dtypes(include=['int16', 'int8', "float64"]).columns.to_list()

preprocessor = ColumnTransformer([('onehot', OneHotEncoder(handle_unknown='ignore'), category_cols)],
                                  remainder='passthrough')

X_train_prep = preprocessor.fit_transform(X_train)
X_test_prep  = preprocessor.transform(X_test)

encoded_cat = preprocessor.named_transformers_['onehot'].get_feature_names(category_cols)
labels = np.concatenate([numeric_cols, encoded_cat])

X_train_prep = pd.DataFrame(X_train_prep, columns=labels)
X_test_prep  = pd.DataFrame(X_test_prep, columns=labels)
X_train_prep.info()
from sklearn.utils import class_weight

classes_weights = class_weight.compute_sample_weight(
    class_weight='balanced',
    y=y_train
)

classes_weights
param_grid = {'n_estimators'  : [350,300],
              'max_depth'     : [9,8], 
              'subsample'     : [0.6, 0.8], 
              'learning_rate' : [0.15, 0.45] 
             }

grid = GridSearchCV(
        estimator  = GradientBoostingClassifier(random_state=123),
        param_grid = param_grid,
        scoring    = 'accuracy',
        n_jobs     = -1,
        cv         = RepeatedKFold(n_splits=3, n_repeats=1, random_state=123), 
        refit      = True,
        verbose    = 1,
        return_train_score = True
       )

grid.fit(X = X_train_prep, y = y_train)

resultados = pd.DataFrame(grid.cv_results_)
resultados.filter(regex = '(param*|mean_t|std_t)') \
    .drop(columns = 'params') \
    .sort_values('mean_test_score', ascending = False) \
    .head(4)

modelo_final = grid.best_estimator_ 
# Mejores hiperparámetros por validación cruzada
# ==============================================================================
print("----------------------------------------")
print("Mejores hiperparámetros encontrados (cv)")
print("----------------------------------------")
print(grid.best_params_, ":", grid.best_score_, grid.scoring)
modelo_final = GradientBoostingClassifier(n_estimators = 305,
                                  max_depth = 9,
                                  min_samples_split = 2,
                                  min_samples_leaf = 3,
                                  subsample=0.6,
                                  verbose=True,
                                  learning_rate=0.15)
modelo_final.fit(X = X_train_prep, y = y_train, sample_weight = classes_weights)

predicciones = modelo_final.predict(X = X_test_prep)
mat_confusion = confusion_matrix(
                    y_true    = y_test,
                    y_pred    = predicciones
                )

accuracy = accuracy_score(
            y_true    = y_test,
            y_pred    = predicciones,
            normalize = True
           )

print("Matriz de confusión")
print("-------------------")
print(mat_confusion)
print("")
print(f"El accuracy de test es: {100 * accuracy} %")
y_preds = modelo_final.predict(X_test_prep)
f1_score(y_test, y_preds, average='micro')
test_values = pd.read_csv('test_values.csv', index_col = "building_id")
test_values
for row in to_be_categorized:
    test_values[row] = test_values[row].astype("category")

datatypes = dict(test_values.dtypes)
for row in test_values.columns:
    if datatypes[row] != "int64" and datatypes[row] != "int32" and \
       datatypes[row] != "int16" and datatypes[row] != "int8":
        continue
    if test_values[row].nlargest(1).item() > 32767 and test_values[row].nlargest(1).item() < 2**31:
        test_values[row] = test_values[row].astype(np.int32)
    elif test_values[row].nlargest(1).item() > 127:
        test_values[row] = test_values[row].astype(np.int16)
    else:
        test_values[row] = test_values[row].astype(np.int8)

cat_cols = test_values.select_dtypes(include=['category', "bool"]).columns.to_list()
num_cols = test_values.select_dtypes(include=['int16', 'int8', "float64"]).columns.to_list()

preprocessor_2 = ColumnTransformer([('onehot', OneHotEncoder(handle_unknown='ignore'), cat_cols)],
                                  remainder='passthrough')

test_values_reduce = preprocessor_2.fit_transform(test_values)

en_cat = preprocessor_2.named_transformers_['onehot'].get_feature_names(cat_cols)
labels_2 = np.concatenate([num_cols, en_cat])

test_values_reduce = pd.DataFrame(test_values_reduce, columns=labels_2)

test_values_reduce.info()
preds = modelo_final.predict(test_values_reduce)
submission_format = pd.read_csv("submission_format.csv", index_col = "building_id")
my_submission = pd.DataFrame(data=preds,
                             columns=submission_format.columns,
                             index=submission_format.index)
my_submission.to_csv('gradient_boosting_model_1.csv')
!head gradient_boosting_model_1.csv<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1597315436241611, 'fraction_numerical': 0.017986577181208052, 'mean_word_length': 3.5322384428223845, 'pattern_counts': {'":': 3, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '62990', 'license': 'MIT', 'n_tokens_mistral': 2635, 'n_tokens_neox': 2534, 'n_words': 354, 'path': 'src/GradientBoosting/jf-model-1.ipynb', 'repo_name': 'joaquinfontela/Machine-Learning', 'size': '7450'}