<jupyter_start><jupyter_text>Entender o Problema -- Objetivo do Problema:    -- 1.0 Previsão do primeiro destino que um novo usuário irá escolher.        -- Por que?        -- Qual tipo de modelo de negócio do Airbnb?        --(MarketPlace - Conecta Pessoas que Oferecem Acomodaões que estão procurando Acomodação)            -- Tamanho do Portfólio            -- Diversidade/Densidade de Portfólio            -- Preço Médio        -- Demanda (Procura de Acomodações)            -- Número de Usuários            -- LTV (Lifetime Value)            -- CAC (Client Acquisition Cost - Propaganda Instagram/Youtube)                    --  Revenue ( O que o AirBnb quer )            Gross Revenue = Fee*Número Cliente - CAC (Margens de Contribuição)                        -- Proposta da Solução    -- Modelo de Previsão do primeiro destino de um novo usário    -- 1.0 Predições e salva em Tabela do Banco de Dados.    -- 2.0 API:                --- Input: Usuário e sua Características        --- Output: Usuário e suas características com a **predição do destino**            ***MultiClasse e Desbalanceado  0.0 Imports<jupyter_code>import random
import pandas as pd

from keras      import models          as ml
from keras      import layers          as l
from sklearn    import model_selection as ms
from sklearn    import preprocessing   as pp
from sklearn    import metrics         as m
from scikitplot import metrics         as mt<jupyter_output><empty_output><jupyter_text>0.1 Helper Funtions  0.2 Loanding Data<jupyter_code>df_raw = pd.read_csv ('training_users.csv', low_memory = True)
df_raw.shape
#df_sessions = pd.read_csv ('sessions.csv', low_memory = True)
#df_sessions.shape<jupyter_output><empty_output><jupyter_text>1.0 Data Description<jupyter_code>df1 = df_raw.copy()
print('Number of Rows: {}.'.format(df1.shape[0]))
print('Number of Columns: {}.'.format(df1.shape[1]))<jupyter_output>Number of Rows: 213451.
Number of Columns: 16.<jupyter_text>1.1 Date Type<jupyter_code>df1.dtypes<jupyter_output><empty_output><jupyter_text>1.2 NA Check<jupyter_code>df1.isna().sum()
#remove missing value completly

df1 = df1.dropna()<jupyter_output><empty_output><jupyter_text>1.3 Change Data Type<jupyter_code># date_account_created
df1['date_account_created'] = pd.to_datetime (df1['date_account_created'])

# timestamp_first_active
df1['timestamp_first_active'] = pd.to_datetime (df1['timestamp_first_active'], format = '%Y%m%d%H%M%S')

# date_first_booking
df1['date_first_booking'] = pd.to_datetime (df1['date_first_booking'])

# age
df1['age'] = df1['age'].astype(int)<jupyter_output><empty_output><jupyter_text>1.4 Check Balanced Data<jupyter_code>df1['country_destination'].value_counts(normalize = True) #percent<jupyter_output><empty_output><jupyter_text>2.0 Data Filtering<jupyter_code>df2 = df1. copy()<jupyter_output><empty_output><jupyter_text>2.1 Columns Filtering Rows  3.0 Data Preparation<jupyter_code>df3 = df2. copy()
# dummy variable
df3_dummy = pd.get_dummies (df3.drop (['id', 'country_destination'], axis = 1 ))

#join id and country destination
df3 = pd.concat ([df3[['id', 'country_destination']], df3_dummy], axis = 1)<jupyter_output><empty_output><jupyter_text>4.0 Feature Selection<jupyter_code>#Exclusão de Features que não são relevantes para a construção do Modelo

cols_drop = ['date_account_created', 'timestamp_first_active', 'date_first_booking']

df4 = df3.drop (cols_drop, axis = 1)
X = df4.drop ('country_destination', axis = 1)
y = df4['country_destination'].copy()
#Split DataSet into Training and Test

X_train, X_test, y_train, y_test = ms.train_test_split(X, y, test_size = 0.2, random_state = 32)<jupyter_output><empty_output><jupyter_text>5.0 Machine Learning Model - Neural Network MLP (Validação)<jupyter_code>x_train = X_train.drop ('id', axis = 1)
x_test = X_test.drop ('id', axis = 1)<jupyter_output><empty_output><jupyter_text>5.1 Baseline Model<jupyter_code>country_destination_list = df1['country_destination'].drop_duplicates().sort_values().tolist()
k_num = y_test.shape[0]
country_destination_weights = df1['country_destination'].value_counts(normalize = True).sort_index().tolist()

#Modelo Entrega Valor Aleatório
yhat_random = random.choices (population = country_destination_list, 
                              k = k_num,
                              weights = country_destination_weights )
len (yhat_random)
# Accuracy 
acc_random = m.accuracy_score (y_test_nn, yhat_nn)
print( 'Accuracy: {}'.format (acc_random))

#Balanced Accuracy

balanced_acc_random = m.balanced_accuracy_score (y_test_nn, yhat_random)
print ('Balanced Accuracy:{}'.format (balanced_acc_random))

#Kappa Metrics
kappa_random = m.cohen_kappa_score(y_test_nn, yhat_random)
print ('Kappa Score: {}'.format (kappa_random))

#classification report
print (m.classification_report (y_test_nn, yhat_random))

#Confusion Matrix
mt.plot_confusion_matrix (y_test_nn, yhat_random, normalize = False, figsize = (12,12))<jupyter_output>Accuracy: 0.7027502750275028
Balanced Accuracy:0.08816332171186365
Kappa Score: -0.0059268279059374596
              precision    recall  f1-score   support

          AU       0.00      0.00      0.00       101
          CA       0.00      0.00      0.00       214
          DE       0.01      0.01      0.01       170
          ES       0.02      0.01      0.02       345
          FR       0.04      0.05      0.05       710
          GB       0.02      0.03      0.03       344
          IT       0.02      0.02      0.02       423
          NL       0.03      0.03      0.03       128
          PT       0.00      0.00      0.00        36
          US       0.70      0.71      0.71      9624
       other       0.11      0.11      0.11      1540

    accuracy                           0.52     13635
   macro avg       0.09      0.09      0.09     13635
weighted avg       0.51      0.52      0.51     13635<jupyter_text>5.2 Neural Network - MLP<jupyter_code>ohe = pp.OneHotEncoder ()
y_train_nn = ohe.fit_transform (y_train.values.reshape (-1,1)).toarray()
#model definition
model = ml.Sequential()
model.add (l.Dense(128, input_dim = x_train.shape[1], activation = 'relu'))
model.add (l.Dense(11, activation = 'softmax'))

#model compile
model.compile ( loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
#train model
model.fit (x_train, y_train_nn, epochs = 100)
#predicition
pred_nn = model.predict (x_test)

#invert prediction
yhat_nn = ohe.inverse_transform (pred_nn)

#prediction prepare
y_test_nn = y_test.to_numpy()
yhat_nn = yhat_nn.reshape (1, -1)[0]<jupyter_output><empty_output><jupyter_text>5.2 Neural Network - CV<jupyter_code>#generate k-fold

num_folds = 5
kfold = StratifiedKFold (n_splits = num_folds, shuffle = True, random_state = 32)

for train_ix, val_ix in kfols.split (x_train, y_train):
    
    #get fold
    x_train.iloc[train_ix]
    
    
    
    #model definition
    model = ml.Sequential()
    model.add (l.Dense(256, input_dim = x_train.shape[1], activation = 'relu'))
    model.add (l.Dense(11, activation = 'softmax'))

    #model compile
    model.compile ( loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    
    
    #train model
    model.fit (x_train, y_train_nn, epochs = 100, batch_size = 32, verbose = 0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10813030385984122, 'fraction_numerical': 0.04763208321927183, 'mean_word_length': 3.199425287356322, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '643350', 'license': 'MIT', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2556, 'n_words': 583, 'path': 'Ciclo 02_Imbalanced_Metrics.ipynb', 'repo_name': 'mimsai/airbnb_predicition', 'size': '7306'}