<jupyter_start><jupyter_text>- Tras los analisis realizados en el proceso de feature engineering, se llego a la conclusion de que las siguientes columnas son las mas importantes:     - count_floors_per_eq     - area_percentage     - foundation_type (r,w,u,i)     - roof_type (x)     - other_floor_type(s,q,j)     - ground_floor_type(f,v)     - superstructure_mud_mortar_stone     - superstructure_cement_mortar_brick     - superstructure_rc_non_engineered     - superstructure_rc_engineered<jupyter_code>features_importantes = ["count_floors_pre_eq",
                        "area_percentage",
                        "has_superstructure_mud_mortar_stone",
                        "has_superstructure_mud_mortar_brick",
                        "has_superstructure_rc_non_engineered",
                        "has_superstructure_rc_engineered"
                        ]

data_train = data[features_importantes]

foundation_dummies = pd.get_dummies(data["foundation_type"])
data_train["foundation_r"] = foundation_dummies["r"]
data_train["foundation_w"] = foundation_dummies["w"]
data_train["foundation_u"] = foundation_dummies["u"]
data_train["foundation_i"] = foundation_dummies["i"]

roof_dummies = pd.get_dummies(data["roof_type"])
data_train["roof_x"] = roof_dummies["x"]


pb_dummies = pd.get_dummies(data["ground_floor_type"])
data_train["ground_floor_f"] = pb_dummies["f"]
data_train["ground_floor_v"] = pb_dummies["v"]

of_dummies = pd.get_dummies(data["other_floor_type"])
data_train["other_floor_s"] = of_dummies["s"]
data_train["other_floor_q"] = of_dummies["q"]
data_train["other_floor_j"] = of_dummies["j"]

data_train
(data_labels.damage_grade
             .value_counts()
             .sort_index()
             .plot.bar(title="Number of Buildings with Each Damage Grade",color=["lightblue","orange","crimson"]))<jupyter_output><empty_output><jupyter_text>Modelo Inicial Machine Learning  Random Forest Hiperparametros a considerar:        n_estimators: cantidad de árboles a construir (100)    max_depth: máxima profundidad de cada árbol    min_samples_split: la cantidad mínima de datos requeridos para splitear un nodo interno (2)    min_samples_leaf: cantidad mínima de datos requeridos para ser una hoja (1)    max_features: la cantidad de features a considerar cuando se busca el mejor split (n)<jupyter_code>pipe = make_pipeline(StandardScaler(), 
                     RandomForestClassifier(random_state=2018))
pipe
param_grid = {'randomforestclassifier__n_estimators': [50, 200],
              'randomforestclassifier__min_samples_leaf': [1, 5],
              'randomforestclassifier__max_depth': [1, 5],
              'randomforestclassifier__min_samples_split': [1, 5],
              'randomforestclassifier__max_features': [1, 16],
             }
gs = GridSearchCV(pipe, param_grid, cv=5)
gs.fit(data_train, data_labels.values.ravel())
gs.best_params_
in_sample_preds = gs.predict(data_train)
f1_score(data_labels, in_sample_preds, average='micro')<jupyter_output><empty_output><jupyter_text>Prediccion<jupyter_code>#Set de Test
test_values = pd.read_csv('data/test_values.csv',dtype = {
    "building_id" : "int32",
    "geo_level_1_id" : "int8", 
    "geo_level_2_id" : "int16",
    "geo_level_3_id" : "int16",
    "count_floors_pre_eq" : "int8",
    "age" : "int16",
    "area_percentage" :"int8",
    "height_percentage" :"int8",
    "land_surface_condition" :"category",
    "foundation_type" :"category",
    "roof_type" :"category",
    "ground_floor_type" :"category",
    "other_floor_type" :"category",
    "position" :"category",
    "plan_configuration" :"category",
    "has_superstructure_adobe_mud" : "bool",
    "has_superstructure_mud_mortar_stone" : "bool",
    "has_superstructure_stone_flag" : "bool",
    "has_superstructure_cement_mortar_stone" : "bool",
    "has_superstructure_mud_mortar_brick" : "bool",
    "has_superstructure_cement_mortar_brick" : "bool",
    "has_superstructure_timber" : "bool",
    "has_superstructure_bamboo" : "bool",
    "has_superstructure_rc_non_engineered" : "bool",
    "has_superstructure_rc_engineered" : "bool",
    "has_superstructure_other" : "bool",
    "legal_ownership_status" :"category",
    "count_families" : "int8",
    "has_secondary_use" : "bool",
    "has_secondary_use_agriculture" : "bool",
    "has_secondary_use_hotel" : "bool",
    "has_secondary_use_rental" : "bool",
    "has_secondary_use_institution" : "bool",
    "has_secondary_use_school" : "bool",
    "has_secondary_use_industry" : "bool",
    "has_secondary_use_health_post" : "bool",
    "has_secondary_use_gov_office" : "bool",
    "has_secondary_use_use_police" : "bool",
    "has_secondary_use_other" : "bool"},
     index_col='building_id')
data_test = test_values[features_importantes]

test_foundation_dummies = pd.get_dummies(test_values["foundation_type"])
data_test["foundation_r"] = test_foundation_dummies["r"]
data_test["foundation_w"] = test_foundation_dummies["w"]
data_test["foundation_u"] = test_foundation_dummies["u"]
data_test["foundation_i"] = test_foundation_dummies["i"]

test_roof_dummies = pd.get_dummies(test_values["roof_type"])
data_test["roof_x"] = test_roof_dummies["x"]


test_pb_dummies = pd.get_dummies(test_values["ground_floor_type"])
data_test["ground_floor_f"] = test_pb_dummies["f"]
data_test["ground_floor_v"] = test_pb_dummies["v"]

test_of_dummies = pd.get_dummies(test_values["other_floor_type"])
data_test["other_floor_s"] = test_of_dummies["s"]
data_test["other_floor_q"] = test_of_dummies["q"]
data_test["other_floor_j"] = test_of_dummies["j"]

data_test
predictions = gs.predict(data_test)
predictions<jupyter_output><empty_output><jupyter_text>Formato de Entrega<jupyter_code>submission_format = pd.read_csv('data/submission_format.csv', index_col='building_id')
my_submission = pd.DataFrame(data=predictions,
                             columns=submission_format.columns,
                             index=submission_format.index)
my_submission
my_submission.to_csv('first_random_forest_submission.csv')<jupyter_output><empty_output><jupyter_text>Obtuvo 5.795  Repito Proceso pero selecciono todos los features<jupyter_code>data_train = data
data_train = pd.get_dummies(data)

data_train
pipe = make_pipeline(StandardScaler(), 
                     RandomForestClassifier(random_state=2018))
pipe
param_grid = {'randomforestclassifier__n_estimators': [50, 1000],
             'randomforestclassifier__max_depth': [1, 5],
              'randomforestclassifier__min_samples_split': [1, 5],}
gs = GridSearchCV(pipe, param_grid, cv=5)
gs.fit(data_train, data_labels.values.ravel())
gs
gs.best_params_
in_sample_preds = gs.predict(data_train)
f1_score(data_labels, in_sample_preds, average='micro')<jupyter_output><empty_output><jupyter_text>- Para la metrifa micro F1, la prediccion es mejor cuando el numero es mas cercano a 1<jupyter_code>data_test = test_values
data_test = pd.get_dummies(test_values)
data_test
predictions = gs.predict(data_test)
predictions
submission_format = pd.read_csv('data/submission_format.csv', index_col='building_id')
my_submission = pd.DataFrame(data=predictions,
                             columns=submission_format.columns,
                             index=submission_format.index)
my_submission.to_csv('second_random_forest_submission.csv')<jupyter_output><empty_output><jupyter_text>Obtuvo 5.765  Pruebo el mismo modelo pero con otros features<jupyter_code>#Set de entrenamiento con el target Damage Grade includio
data = pd.read_csv('data/train_values.csv',dtype = {
    "building_id" : "int32",
    "geo_level_1_id" : "int8", 
    "geo_level_2_id" : "int16",
    "geo_level_3_id" : "int16",
    "count_floors_pre_eq" : "int8",
    "age" : "int16",
    "area_percentage" :"int8",
    "height_percentage" :"int8",
    "land_surface_condition" :"category",
    "foundation_type" :"category",
    "roof_type" :"category",
    "ground_floor_type" :"category",
    "other_floor_type" :"category",
    "position" :"category",
    "plan_configuration" :"category",
    "has_superstructure_adobe_mud" : "bool",
    "has_superstructure_mud_mortar_stone" : "bool",
    "has_superstructure_stone_flag" : "bool",
    "has_superstructure_cement_mortar_stone" : "bool",
    "has_superstructure_mud_mortar_brick" : "bool",
    "has_superstructure_cement_mortar_brick" : "bool",
    "has_superstructure_timber" : "bool",
    "has_superstructure_bamboo" : "bool",
    "has_superstructure_rc_non_engineered" : "bool",
    "has_superstructure_rc_engineered" : "bool",
    "has_superstructure_other" : "bool",
    "legal_ownership_status" :"category",
    "count_families" : "int8",
    "has_secondary_use" : "bool",
    "has_secondary_use_agriculture" : "bool",
    "has_secondary_use_hotel" : "bool",
    "has_secondary_use_rental" : "bool",
    "has_secondary_use_institution" : "bool",
    "has_secondary_use_school" : "bool",
    "has_secondary_use_industry" : "bool",
    "has_secondary_use_health_post" : "bool",
    "has_secondary_use_gov_office" : "bool",
    "has_secondary_use_use_police" : "bool",
    "has_secondary_use_other" : "bool"},
    index_col='building_id')

data_labels =  pd.read_csv('data/train_labels.csv',dtype = {"damage_grade" : "int8" },index_col='building_id')
data_labels
features_importantes = ["count_floors_pre_eq",
                        "area_percentage",
                        "has_superstructure_mud_mortar_stone",
                        "has_superstructure_mud_mortar_brick",
                        "has_superstructure_rc_non_engineered",
                        "has_superstructure_rc_engineered",
                        "foundation_type",
                        "roof_type",
                        "ground_floor_type",
                        "other_floor_type"
                        ]
data_train= data[features_importantes]
data_train = pd.get_dummies(data_train)
data_train
pipe = make_pipeline(StandardScaler(), 
                     RandomForestClassifier(random_state=2018))
pipe
param_grid = {'randomforestclassifier__n_estimators': [50, 200],
              'randomforestclassifier__min_samples_leaf': [1, 5],
              'randomforestclassifier__max_depth': [1, 5],
              'randomforestclassifier__min_samples_split': [1, 5],
              'randomforestclassifier__max_features': [1, 16],
             }
gs = GridSearchCV(pipe, param_grid, cv=5)
gs.fit(data_train, data_labels.values.ravel())
gs.best_params_
in_sample_preds = gs.predict(data_train)
f1_score(data_labels, in_sample_preds, average='micro')
data_test = test_values[features_importantes]
data_test = pd.get_dummies(data_test)
data_test
predictions = gs.predict(data_test)
predictions
submission_format = pd.read_csv('data/submission_format.csv', index_col='building_id')
my_submission = pd.DataFrame(data=predictions,
                             columns=submission_format.columns,
                             index=submission_format.index)
my_submission.to_csv('third_random_forest_submission.csv')<jupyter_output><empty_output><jupyter_text>Obtuvo 5,802  Pickle Rick<jupyter_code>import pickle
#filename es el nombre que le daremos a nuestro archivo
filename = 'pickles/random_forest_pickle.pkl'
#Con el comando dump y la instrucción wb nos aseguramos de crear y escribir sobre el 
#archivo creado, otra precaución es lo que deseamos guardar en este caso fue el modelo
pickle.dump(gs, open(filename,'wb'))
loaded_model = pickle.load(open(filename, 'rb'))
loaded_model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11497862688650441, 'fraction_numerical': 0.00942161737764983, 'mean_word_length': 4.584023380418899, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '273122', 'license': 'MIT', 'n_tokens_mistral': 4100, 'n_tokens_neox': 3963, 'n_words': 593, 'path': 'tp2/Random Forest.ipynb', 'repo_name': 'alanbach89/orga_datos_1c_2021', 'size': '11463'}