<jupyter_start><jupyter_text>Dataset: https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/problem12.html Imports<jupyter_code>import pandas as pd # Importa o Pandas

arquivo = 'https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv' # Nome e local do arquivo

df = pd.read_csv(arquivo) # Cria um dataframe do pandas
df.shape # Verifica o número de linhas e colunas<jupyter_output><empty_output><jupyter_text>Verificar os primeiros itens do dataframe<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Principais estatística do dataframe<jupyter_code>df = df.drop('Name', axis=1)
df.describe()<jupyter_output><empty_output><jupyter_text>Verificar se há valores em branco (NaN)<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 887 entries, 0 to 886
Data columns (total 7 columns):
 #   Column                   Non-Null Count  Dtype  
---  ------                   --------------  -----  
 0   Survived                 887 non-null    int64  
 1   Pclass                   887 non-null    int64  
 2   Sex                      887 non-null    object 
 3   Age                      887 non-null    float64
 4   Siblings/Spouses Aboard  887 non-null    int64  
 5   Parents/Children Aboard  887 non-null    int64  
 6   Fare                     887 non-null    float64
dtypes: float64(2), int64(4), object(1)
memory usage: 48.6+ KB<jupyter_text>Gerando visualizações<jupyter_code>import seaborn as sns # Importa a biblioteca Seaborn (para plot)
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (15,7)
sns.set(style='ticks') # Define o "estilo" que será usado nos gráficos

for pos in range(3, 7):
  plt.subplot(1, 4, pos-2)
  sns.violinplot(data=df.iloc[:, pos])
  plt.xlabel(df.columns[pos], rotation=60)
  plt.yticks(rotation='vertical')

plt.show()
plt.rcParams["figure.figsize"] = (10,6)
sns.heatmap(df.corr())
sns.pairplot(df, hue='Survived')
plt.rcParams["figure.figsize"] = (15,15)

plt.subplot(3, 2, 1)
sns.violinplot(df['Sex'], df['Survived'])
plt.subplot(3, 2, 2)
sns.distplot(df['Fare'][df['Survived']==1], color='r')
sns.distplot(df['Fare'][df['Survived']==0], color='b')
plt.subplot(3, 2, 3)
sns.violinplot(df['Parents/Children Aboard'], df['Survived'])
plt.subplot(3, 2, 4)
sns.violinplot(df['Siblings/Spouses Aboard'], df['Survived'])
plt.subplot(3, 2, 5)
sns.distplot(df['Age'][df['Survived']==1], color='r')
sns.distplot(df['Age'][df['Survived']==0], color='b')
plt.subplot(3, 2, 6)
sns.violinplot(df['Pclass'], df['Survived'])

plt.show()<jupyter_output><empty_output><jupyter_text>Mapendo valores de "sex"<jupyter_code>df['Sex'] = df['Sex'].map({'male': 0, 'female': 1})
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>Modelo<jupyter_code>y = df['Survived']
X = df.drop(['Survived'], axis=1)
from sklearn.model_selection import train_test_split # Importa parte do sklearn para dividir os dados de entrada entre treino e teste
X_treino, X_teste, y_treino, y_teste = train_test_split(X, y, random_state=0) # Divide os dados de entrada entre treino e teste
from lightgbm import LGBMClassifier
modelo = LGBMClassifier()
modelo.fit(X_treino, y_treino)
modelo.score(X_teste, y_teste)<jupyter_output><empty_output><jupyter_text>Otimizando hiperparâmetrosCom base no tutorial do [<NAME>](http://mariofilho.com).<jupyter_code>!pip install scikit-optimize
from skopt import gp_minimize 
def treinar_modelo(params):
    learning_rate = params[0]
    num_leaves = params[1]
    min_child_samples = params[2]
    subsample = params[3]
    colsample_bytree = params[4]
    subsample_freq = params[5]
    
    print(params, '\n')
    
    mdl = LGBMClassifier(learning_rate=learning_rate, num_leaves=num_leaves, min_child_samples=min_child_samples,
                         subsample=subsample, colsample_bytree=colsample_bytree, random_state=0, subsample_freq=subsample_freq, 
                         n_estimators=100)
    
    mdl.fit(X_treino, y_treino)
    
    return -mdl.score(X_teste, y_teste)

space = [(1e-3, 1), #learning rate
         (2, 128), # num_leaves
         (1, 100), # min_child_samples
         (0.05, 1.0), # subsample
         (0.1, 1.0), # colsample bytree
         (1, 5)] # colsample bytree

resultados_gp = gp_minimize(treinar_modelo, space, random_state=1, verbose=1, n_calls=50, n_random_starts=10)
resultados_gp.x<jupyter_output><empty_output><jupyter_text>Adaptando modelo Cross validation<jupyter_code>from sklearn.model_selection import cross_val_score
mdl = LGBMClassifier(learning_rate=0.2829, num_leaves=128, min_child_samples=58,
                     subsample=0.8722, colsample_bytree=0.5265, random_state=0, subsample_freq=1,
                     n_estimators=100)
    
scores = cross_val_score(mdl, X, y, cv=5, scoring='accuracy')

print(scores.mean())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13745349317899958, 'fraction_numerical': 0.03885903265812319, 'mean_word_length': 4.175401069518717, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 2, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '486054', 'license': 'MIT', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1842, 'n_words': 343, 'path': 'python/03_LGBM.ipynb', 'repo_name': 'jhklarcher/data-science', 'size': '4843'}