<jupyter_start><jupyter_text>for dataset in Xs:    age_mean = dataset['Age'].mean()    age_std = dataset['Age'].std()    age_null_count = dataset['Age'].isnull().sum()    age_null_random_list = np.random.randint(age_mean - age_std, age_mean + age_std, size=age_null_count)    dataset.loc[ : ,'Age'].loc[dataset['Age'].isnull()] = age_null_random_list    dataset['Age'][np.isnan(dataset['Age'])] = age_null_random_list    <= outro jeito de fazer a ultima linha    dataset.loc[ : ,'Age'] = dataset.Age.map(int)<jupyter_code>X.describe()
X_predict.describe()
for dataset in Xs:
    dataset.loc[ : , 'Cabin'] = dataset['Cabin'].map(lambda x: 0 if type(x)==str else 1)
        
X.tail(10)
X['Title'] = train['Name'].str.extract('([A-Za-z]+)\.')
X_predict['Title'] = test['Name'].str.extract('([A-Za-z]+)\.')
#str.extract sep ara so o arg passado
# arg deve ser uma expressão regular python (pesquisar +)
# '([ Letras a serem consideradas])'
# + indica pegar a paravra completa
#\. indica o que vem antes do ponto (.) 
X.tail(10)
for dataset in Xs:
    print(dataset.Title.value_counts().sum()) #sem NaN
    print(dataset.Title.unique())
for dataset in Xs:
    dataset.loc[ : ,'Title'] = dataset['Title'].replace(['Mlle', 'Miss'], 1)
    dataset.loc[ : ,'Title'] = dataset['Title'].replace(['Ms', 'Miss'], 2)
    dataset.loc[ : ,'Title'] = dataset['Title'].replace(['Mme', 'Mrs'], 3)
    dataset.loc[ : ,'Title'] = dataset['Title'].replace(['Master', 'Mr'], 4)
    dataset.loc[ : ,'Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',
                                                           'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 5) 
    # 5  => titulos mais raros de aparecerem
    
X.tail(10)
pd.qcut(X.Age, 5).unique()
for dataset in Xs:
    dataset.loc[dataset['Age'] <=19, 'Age'] = 0
    dataset.loc[(dataset['Age'] >19) & (dataset['Age'] <=25), 'Age'] = 1
    dataset.loc[(dataset['Age'] >25) & (dataset['Age'] <=31), 'Age'] = 2
    dataset.loc[(dataset['Age'] >31) & (dataset['Age'] <=41), 'Age'] = 3
    dataset.loc[(dataset['Age'] >41) & (dataset['Age'] <=80), 'Age'] = 4

X
for dataset in Xs:
    dataset['FamilySize']= dataset.SibSp + dataset.Parch +1
    
X
for dataset in Xs:
    dataset['IsAlone'] = 0
    dataset.loc[dataset['FamilySize']==1, 'IsAlone'] = 1
X
for dataset in Xs:
    dataset.loc[dataset.Age.isnull(), 'Age'] = -1<jupyter_output>C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:965: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self.obj[item] = s<jupyter_text>scaler = preprocessing.MinMaxScaler()for dataset in Xs:    dataset.loc[ : , :] = scaler.fit_transform(dataset)    X.tail(10)<jupyter_code>X.describe()
model = LogisticRegression()
model.fit(X,y)
coeff_df = pd.DataFrame(X.columns)
coeff_df.columns = ['Feature']
coeff_df["Correlation"] = pd.Series(model.coef_[0])
#apos treinadar o modelo, .coef_ retorna uma array(1, n) com as correlações entre as variaveis e o alvo

#correlação 
coeff_df.sort_values(by='Correlation', ascending=False)
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.4, random_state=42)
models = [LogisticRegression(random_state=42), DecisionTreeClassifier(random_state=42),
          KNeighborsClassifier(), SVC(random_state=42), RandomForestClassifier(random_state=42),
         LinearSVC(random_state=42), Perceptron(random_state=42), SGDClassifier(random_state=42)]

for model in models:
    model.fit(X_train, y_train)
    y_pred = model.predict(X_valid)
    print(accuracy_score(y_valid,y_pred),type(model))
resultados_acc=[]
kf = RepeatedKFold(n_splits=2, n_repeats=8 , random_state=42)
for train_lines, test_lines in kf.split(X):
    x_train = X.iloc[train_lines]
    x_test = X.iloc[test_lines]
    y_train = y.iloc[train_lines]
    y_test = y.iloc[test_lines]
    modelo = LogisticRegression(random_state=0, multi_class='multinomial')
    modelo.fit(x_train, y_train)
    predict_test = modelo.predict(x_test)
    acc = np.mean(predict_test == y_test)
    resultados_acc.append(acc)
    print('Acc: ',acc)
print('\nAcc média: ',np.mean(resultados_acc))
hist(resultados_acc)
resultados_acc=[]
kf = RepeatedKFold(n_splits=2, n_repeats=8 , random_state=42)
for train_lines, test_lines in kf.split(X):
    x_train = X.iloc[train_lines]
    x_test = X.iloc[test_lines]
    y_train = y.iloc[train_lines]
    y_test = y.iloc[test_lines]
    modelo = LogisticRegression(random_state=0, n_jobs=-1, solver='newton-cg')
    modelo.fit(x_train, y_train)
    predict_test = modelo.predict(x_test)
    acc = np.mean(predict_test == y_test)
    resultados_acc.append(acc)
    print('Acc: ',acc)
print('\nAcc média: ',np.mean(resultados_acc))
hist(resultados_acc)
resultados_acc=[]
kf = RepeatedKFold(n_splits=2, n_repeats=8 , random_state=42)
for train_lines, test_lines in kf.split(X):
    x_train = X.iloc[train_lines]
    x_test = X.iloc[test_lines]
    y_train = y.iloc[train_lines]
    y_test = y.iloc[test_lines]
    modelo = LogisticRegression(random_state=0, n_jobs=-1, solver='sag')
    modelo.fit(x_train, y_train)
    predict_test = modelo.predict(x_test)
    acc = np.mean(predict_test == y_test)
    resultados_acc.append(acc)
    print('Acc: ',acc)
print('\nAcc média: ',np.mean(resultados_acc))
hist(resultados_acc)
resultados_acc=[]
kf = RepeatedKFold(n_splits=2, n_repeats=8 , random_state=42)
for train_lines, test_lines in kf.split(X):
    x_train = X.iloc[train_lines]
    x_test = X.iloc[test_lines]
    y_train = y.iloc[train_lines]
    y_test = y.iloc[test_lines]
    modelo = LogisticRegression(random_state=0, n_jobs=-1, penalty='l1', solver='liblinear')
    modelo.fit(x_train, y_train)
    predict_test = modelo.predict(x_test)
    acc = np.mean(predict_test == y_test)
    resultados_acc.append(acc)
    print('Acc: ',acc)
print('\nAcc média: ',np.mean(resultados_acc))
hist(resultados_acc)
resultados_acc=[]
kf = RepeatedKFold(n_splits=2, n_repeats=8 , random_state=42)
for train_lines, test_lines in kf.split(X):
    x_train = X.iloc[train_lines]
    x_test = X.iloc[test_lines]
    y_train = y.iloc[train_lines]
    y_test = y.iloc[test_lines]
    modelo = LogisticRegression(random_state=0, n_jobs=-1, penalty='elasticnet', solver='saga', l1_ratio=0.001)
    modelo.fit(x_train, y_train)
    predict_test = modelo.predict(x_test)
    acc = np.mean(predict_test == y_test)
    resultados_acc.append(acc)
    print('Acc: ',acc)
print('\nAcc média: ',np.mean(resultados_acc))
hist(resultados_acc)
resultados_acc=[]
modelo_final = LogisticRegression(random_state=0, n_jobs=-1, penalty='elasticnet', solver='saga', l1_ratio=0.001)
modelo_final.fit(X,y)
y_predict = modelo_final.predict(X_predict)
resultados = pd.Series(y_predict, index=test['PassengerId'], name='Survived')
resultados.to_csv('Resultados_LRenet.csv', header=True )
pd.read_csv('Resultados_LR2.csv')<jupyter_output>C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\_sag.py:330: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  "the coef_ did not converge", ConvergenceWarning)
{'fraction_non_alphanumeric': 0.1594898518924849, 'fraction_numerical': 0.015907844212835986, 'mean_word_length': 4.8957154405820535, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '198517', 'license': 'MIT', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2775, 'n_words': 506, 'path': 'Titanic(Keggle)/model_titanic2.ipynb', 'repo_name': 'Sizzzer/data-science', 'size': '7292'}