<jupyter_start><jupyter_text>Leo archivos<jupyter_code>%matplotlib inline
from pathlib import Path
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from imblearn.under_sampling import RandomUnderSampler

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
dict_features = {'building_id': 'int64', 'geo_level_1_id': 'int64',\
      'age': 'int64', 'area_percentage': 'int64', \
      'height_percentage': 'int64', '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': 'int64', \
      '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',
      'families_and_secondary_use': 'bool', 'inhabited_building': 'bool', 'has_superstructure_engineered': 'bool',\
      'range_age': 'category'
      }
id='1RUtolRcQlR3RGULttM4ZoQaK_Ouow4gc'
downloaded = drive.CreateFile({'id': id})
downloaded.GetContentFile('train_labels.csv')
train_labels = pd.read_csv('train_labels.csv', encoding='latin-1', dtype={'building_id': 'int64', 'damage_grade': 'int64'})
id='1kt2VFhgpfRS72wtBOBy1KDat9LanfMZU'
downloaded = drive.CreateFile({'id': id})
downloaded.GetContentFile('test_values.csv')
test_values = pd.read_csv('test_values.csv', dtype = dict_features)
id='1br3fMwXX_J0XmiXvOm_wfKWvHSj45T3y'
downloaded = drive.CreateFile({'id': id})
downloaded.GetContentFile('train_values.csv')
train_values = pd.read_csv('train_values.csv', dtype = dict_features)
train_values.set_index('building_id', inplace=True)
test_values.set_index('building_id', inplace=True)
train_labels.set_index('building_id', inplace=True)
print(train_values.shape)
print(train_labels.shape)<jupyter_output>(260601, 38)
(260601, 1)<jupyter_text>Por si algo falla<jupyter_code>train_values_original = train_values<jupyter_output><empty_output><jupyter_text>Analizo los datos<jupyter_code>train_values.head()
train_labels<jupyter_output><empty_output><jupyter_text>Aplico OneHotEncoding<jupyter_code>id='18b8OQ-P3sMu3VvwBEhNoP-uH-DIPaKsn'
downloaded = drive.CreateFile({'id': id})
downloaded.GetContentFile('one_hot_encoder.py')
from one_hot_encoder import encoder
train_values = encoder(train_values)
train_values.head()<jupyter_output><empty_output><jupyter_text>Primera prueba - Todos los features<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
test_size = 0.3
random_state = 0


x_train, x_test, y_train, y_test = train_test_split(train_values, train_labels, test_size=test_size, random_state=random_state)
y_train
print(x_train.shape)
print(y_train.shape)

print(x_test.shape)
print(y_test.shape)
sc = StandardScaler()

sc.fit(x_train)

x_train_std = sc.transform(x_train)
x_test_std = sc.transform(x_test)
from sklearn.multiclass import OneVsOneClassifier
from sklearn.linear_model import Perceptron
from sklearn.multiclass import OneVsOneClassifier
from sklearn.metrics import f1_score
n_iter = 40
eta0=0.1 #learning rate
ppn = Perceptron(eta0=eta0, random_state=random_state)
clf = OneVsOneClassifier(ppn)
clf.fit(x_train_std, y_train)
y_pred = clf.predict(x_test_std)
f1_score(y_test, y_pred, average='micro')<jupyter_output><empty_output><jupyter_text>Pruebo MultiCapa - 1<jupyter_code>from sklearn.neural_network import MLPClassifier
clf = MLPClassifier(random_state=1, max_iter=300)
clf.fit(x_train_std, y_train)
y_pred = clf.predict(x_test_std)
f1_score(y_test, y_pred, average='micro')<jupyter_output><empty_output><jupyter_text>RandomSearch a MLP<jupyter_code>from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import GridSearchCV
parameter_space = {
    'hidden_layer_sizes': [(50,50,50), (50,100,50), (100,)],
    'activation': ['tanh', 'relu'],
    'solver': ['sgd', 'adam'],
    'alpha': [0.0001, 0.05],
    'learning_rate': ['constant','adaptive'],
}
mlp = MLPClassifier(**params)
mlp.fit(x_train_std, y_train)
y_pred_2 = mlp.predict(x_test_std)
f1_score(y_test, y_pred_2, average='micro')<jupyter_output><empty_output><jupyter_text>Aplico random search<jupyter_code>parameter_space = {
    'hidden_layer_sizes': [(50,50,50), (50,100,50), (100,)],
    'activation': ['tanh', 'relu'],
    'solver': ['sgd', 'adam'],
    'alpha': [0.0001, 0.05],
    'learning_rate': ['constant','adaptive'],
}
mlp = MLPClassifier(max_iter=100)
clf_2 = GridSearchCV(mlp, parameter_space, n_jobs=-1, cv=3)
clf_2.fit(x_train_std, y_train)
print('Best parameters found:\n', clf_2.best_params_)
y_pred_2 = clf_2.predict(x_test_std)
f1_score(y_test, y_pred_2, average='micro')<jupyter_output><empty_output><jupyter_text>GridSearch MLB<jupyter_code>from sklearn import neural_network
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
parameters = {'solver': ['lbfgs'], 'max_iter': [500,1000,1500], 'alpha': 10.0 ** -np.arange(1, 7), 'hidden_layer_sizes':np.arange(5, 12), 'random_state':[0,1,2,3,4,5,6,7,8,9]}
clf_grid = GridSearchCV(neural_network.MLPClassifier(), parameters, n_jobs=-1,verbose = 200, scoring='f1_micro')


clf_grid.fit(x_train,y_train)
print("-----------------Original Features--------------------")
print("Best score: %0.4f" % clf_grid.best_score_)
print("Using the following parameters:")
print(clf_grid.best_params_)
parametros_final = clf_grid.best_score_
clf = neural_network.MLPClassifier(alpha= 0.1, hidden_layer_sizes= 10, max_iter= 500, random_state= 4, solver= 'lbfgs')
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
from sklearn.metrics import f1_score
f1_score(y_test, y_pred, average='micro')<jupyter_output><empty_output><jupyter_text>Prueba Balanceada Voy a probar balanceando los datos<jupyter_code>from sklearn.model_selection import train_test_split
index = []
index1 = train_labels[train_labels['damage_grade'] == 1].index.tolist()
index2 = train_labels[train_labels['damage_grade'] == 2].index.tolist()
index3 = train_labels[train_labels['damage_grade'] == 3].index.tolist()

index.extend(index1[0:25000])
index.extend(index2[0:25000])
index.extend(index3[0:25000])
x_train = train_values.loc[index].copy()
y_train = train_labels.loc[index].copy()
x_train
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train,
                                                    test_size=0.10, random_state=9)<jupyter_output><empty_output><jupyter_text>Pruebo MultiCapa - Balanceada<jupyter_code>from sklearn.neural_network import MLPClassifier
from sklearn.metrics import f1_score
clf = MLPClassifier(random_state=1, max_iter=300)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
f1_score(y_test, y_pred, average='micro')
from sklearn.metrics import f1_score, accuracy_score, confusion_matrix, classification_report

pred = y_pred
score = f1_score(y_test, pred, average='micro')
#score = accuracy_score(y_valid_split, pred)

cm = confusion_matrix(y_test, pred)
report = classification_report(y_test, pred)

print("f1_micro: ", score, "\n\n")
print(cm, "\n\n")
print(report, "\n\n")<jupyter_output>f1_micro:  0.36573333333333335 


[[ 257 2223   33]
 [  57 2340   89]
 [  29 2326  146]] 


              precision    recall  f1-score   support

           1       0.75      0.10      0.18      2513
           2       0.34      0.94      0.50      2486
           3       0.54      0.06      0.11      2501

    accuracy                           0.37      7500
   macro avg       0.54      0.37      0.26      7500
weighted avg       0.55      0.37      0.26      7500
{'fraction_non_alphanumeric': 0.13305992253360674, 'fraction_numerical': 0.03907496012759171, 'mean_word_length': 6.143205858421481, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '220369', 'license': 'MIT', 'n_tokens_mistral': 3444, 'n_tokens_neox': 3213, 'n_words': 542, 'path': 'data/RedesNeuronales.ipynb', 'repo_name': 'gsabatino9/OrganizacionDeDatos', 'size': '8778'}