Kaggle competition: IESB - 2019
plt.style.use('default' )<feature_engineering>
train = pd.read_csv('.. /input/train.csv' )
train['AgeCategory'] = pd.qcut(train['Age'], 4)
 test['AgeCategory'] = pd.qcut(test['Age'], 4 )<categorify>
test = pd.read_csv('.. /input/test.csv' )
train['Age_Code'] = LabelEncoder().fit_transform(train['AgeCategory'])
 test['Age_Code'] = LabelEncoder().fit_transform(test['AgeCategory'] )<feature_engineering>
train['nota_mat'] = np.log(train['nota_mat'] )
data_df['Last_Name'] = data_df['Name'].apply(lambda x: str.split(x, ",")[0])
 DEFAULT_SURVIVAL_VALUE = 0.5
data_df['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in data_df[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data_df.loc[data_df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
data_df.loc[data_df['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passengers with family survival information:",
data_df.loc[data_df['Family_Survival']!=0.5].shape[0] )<groupby>
df = train.append(test, sort=False )
for _, grp_df in data_df.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data_df.loc[data_df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
data_df.loc[data_df['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passenger with family/group survival information: "
+str(data_df[data_df['Family_Survival']!=0.5].shape[0]))
train['Family_Survival'] = data_df['Family_Survival'][:891]
test['Family_Survival'] = data_df['Family_Survival'][891:]<categorify>
df.count()
train['Sex'].replace(['male','female'],[0,1],inplace=True)
 test['Sex'].replace(['male','female'],[0,1],inplace=True )<define_variables>
for col in df.columns:
if(df[col].count() < 3124):
print(col)
 print(3124 - df[col].count())
drop_elements = ['PassengerId', 'Name', 'SibSp', 'Parch','Ticket', 'Cabin', 'FareCategory', 'AgeCategory','Age', 'Fare', 'Title', 'Embarked'] 
<drop_column>
df.nunique()
train = train.drop(drop_elements, axis=1)
 test = test.drop(drop_elements, axis=1 )<prepare_x_and_y>
df['participacao_transf_receita'] = df['participacao_transf_receita'].fillna(np.mean(df['participacao_transf_receita']))
df['servidores'] = df['servidores'].fillna(np.mean(df['servidores']))
df['perc_pop_econ_ativa'] = df['perc_pop_econ_ativa'].fillna(np.mean(df['perc_pop_econ_ativa']))
df['gasto_pc_saude'] = df['gasto_pc_saude'].fillna(np.mean(df['gasto_pc_saude']))
df['hab_p_medico'] = df['hab_p_medico'].fillna(np.mean(df['hab_p_medico']))
df['gasto_pc_educacao'] = df['gasto_pc_educacao'].fillna(np.mean(df['gasto_pc_educacao']))
df['exp_anos_estudo'] = df['exp_anos_estudo'].fillna(np.mean(df['exp_anos_estudo']))
df['exp_vida'] = df['exp_vida'].fillna(np.mean(df['exp_vida']))
df['idhm'] = df['idhm'].fillna(np.mean(df['idhm']))
df['indice_governanca'] = df['indice_governanca'].fillna(np.mean(df['indice_governanca']))
y = train['Survived']<prepare_x_and_y>
def convertaStringParaFloat(valor):
if(type(valor)== str):
valor = valor.replace(',','')
 valor = float(valor)
 return valor
df['densidade_dem'] = df['densidade_dem'].map(convertaStringParaFloat)
 df['densidade_dem'] = df['densidade_dem'].fillna(np.mean(df['densidade_dem']))
df.info()
X = train[train.columns[1:]] 
<import_modules>
df['area'] = df['area'].map(convertaStringParaFloat )
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler<define_variables>
df['estado'] = df['estado'].astype('category' )
all_features = ['Pclass', 'Sex', 'FamilySize', 'Fare_Code', 'Age_Code', 'Family_Survival']<feature_engineering>
df['estado'] = df['estado'].cat.codes
all_preprocess = ColumnTransformer(
 transformers = [ 
('allfeatures', all_transformer, all_features),
]
)<split>
df['municipio'] = df['municipio'].astype('category' )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=train['Survived'] )<import_modules>
df['municipio'] = df['municipio'].cat.codes
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import cross_val_score<choose_model_class>
df = pd.concat([df, pd.get_dummies(df['regiao'], prefix='regiao' ).iloc[:, :-1]], axis =1)
 del df['regiao']
df.info()
classifiers = [
LogisticRegression(random_state=42),
RandomForestClassifier(random_state=42),
SVC(random_state=42),
KNeighborsClassifier() ,
SGDClassifier(random_state=42),
]<train_model>
df = pd.concat([df, pd.get_dummies(df['porte'], prefix='porte' ).iloc[:, :-1]], axis =1)
 del df['porte']
first_round_scores = {}
for classifier in classifiers:
pipe = Pipeline(steps=[('preprocessor', all_preprocess), 
('classifier', classifier)])
 pipe.fit(X_train, y_train)
 print(classifier)
 score = pipe.score(X_test, y_test)
 first_round_scores[classifier.__class__.__name__[:10]] = score
print("model score: %.3f" % score )<train_on_grid>
f = lambda x: x.replace('ID_ID_','')
 df['codigo_mun'] = df['codigo_mun'].map(f)
 df['codigo_mun'] = df['codigo_mun'].astype(int)
grid_scores = {}
log_clf = Pipeline(steps=[('preprocessor', all_preprocess), 
('classifier', LogisticRegression(random_state=42)) ])
 log_param_grid = {
'classifier__C': [0.01, 0.1, 1.0, 10],
'classifier__solver' : ['liblinear','lbfgs','sag', 'saga'],
'classifier__max_iter' : [500],
}
log_grid_search = GridSearchCV(log_clf, log_param_grid, cv=10, iid=True)
 log_grid_search.fit(X_train, y_train)
 print('Logistic Regression - grid_search.best_params_ and best_scores_', log_grid_search.best_params_, log_grid_search.best_score_)
 log_model_score = log_grid_search.score(X_test, y_test)
 print("Logistic Regression - model score: ", log_model_score)
 grid_scores['log'] = log_model_score<train_on_grid>
def transformePorcentagem(valorString):
if(valorString == '
return 0
valorSemSinal = valorString.replace('%', '')
 valor = int(valorSemSinal)/ 100
return valor
rf_clf = Pipeline(steps=[('preprocessor', all_preprocess), 
('classifier', RandomForestClassifier(random_state=42)) ])
 rf_param_grid = {
'classifier__n_estimators' : [50, 100],
'classifier__max_features' : [2, 3],
'classifier__criterion' : ['gini', 'entropy']
}
rf_grid_search = GridSearchCV(rf_clf, rf_param_grid, cv=10, iid=True)
 rf_grid_search.fit(X_train, y_train)
 print('Random Forest grid_search.best_params_ and best_scores_', rf_grid_search.best_params_, rf_grid_search.best_score_)
 rf_model_score = rf_grid_search.score(X_test, y_test)
 print("Random Forest - model score: ", rf_model_score)
 grid_scores['rf'] = rf_model_score<train_on_grid>
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].map(transformePorcentagem )
svm_clf = Pipeline(steps=[('preprocessor', all_preprocess), 
('classifier', SVC(random_state=42)) ])
 svm_param_grid = [
{'classifier__kernel': ['linear'], 'classifier__C': [10., 30., 100., 300.]},
{'classifier__kernel': ['rbf'], 'classifier__C': [1.0, 3.0, 10., 30., 100., 300.],
'classifier__gamma': [0.01, 0.03, 0.1, 0.3, 1.0]},
]
svm_grid_search = GridSearchCV(svm_clf, svm_param_grid, cv=10, iid=True)
 svm_grid_search.fit(X_train, y_train)
 print('SVM grid_search.best_params_ and best_scores_', svm_grid_search.best_params_, svm_grid_search.best_score_)
 svm_model_score = svm_grid_search.score(X_test, y_test)
 print("SVM - model score: ", svm_model_score)
 grid_scores['svm'] = svm_model_score<train_on_grid>
def corrijaValoresAcimaDeCem(valor):
if(valor > 100):
print(valor)
 return 0
else:
return valor
knn_clf = Pipeline(steps=[('preprocessor', all_preprocess), 
('classifier', KNeighborsClassifier())])
 knn_param_grid = {
'classifier__n_neighbors': [5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26 ],
'classifier__weights': ['uniform', 'distance' ],
'classifier__leaf_size': list(range(1,50,5)) ,
}
knn_grid_search = GridSearchCV(knn_clf, knn_param_grid, cv=10, iid=True, scoring ='roc_auc')
 knn_grid_search.fit(X_train, y_train)
 print('KNN grid_search.best_params_ and best_scores_', knn_grid_search.best_params_, knn_grid_search.best_score_)
 knn_model_score = knn_grid_search.score(X_test, y_test)
 print("KNN - model score: ", knn_model_score)
 grid_scores['knn'] = knn_model_score<train_on_grid>
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].map(corrijaValoresAcimaDeCem )
sgd_clf = Pipeline(steps=[('preprocessor', all_preprocess), 
('classifier', SGDClassifier(random_state=42)) ])
 sgd_param_grid = {
'classifier__max_iter': [100, 200],
'classifier__alpha': [0.0001, 0.001, 0.01, 0.1],
}
sgd_grid_search = GridSearchCV(sgd_clf, sgd_param_grid, cv=10, iid=True)
 sgd_grid_search.fit(X_train, y_train)
 print('SGD grid_search.best_params_ and best_scores_', sgd_grid_search.best_params_, sgd_grid_search.best_score_)
 sgd_model_score = sgd_grid_search.score(X_test, y_test)
 print("SGD - model score: ", sgd_model_score)
 grid_scores['sgd'] = sgd_model_score<choose_model_class>
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].fillna(np.mean(df['comissionados_por_servidor']))
final_pipe = Pipeline(steps=[('preprocessor', all_preprocess)] )<normalization>
df['ranking_igm'] = df['ranking_igm'].map(retiraSinalGrau )
X_final_processed = final_pipe.fit_transform(X )<categorify>
df['ranking_igm'] = df['ranking_igm'].astype('category' )
test_final_processed = final_pipe.transform(test )<train_on_grid>
df['ranking_igm'] = df['ranking_igm'].cat.codes
knn_hyperparameters = {
'n_neighbors': [6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22],
'algorithm' : ['auto'],
'weights': ['uniform', 'distance'],
'leaf_size': list(range(1,50,5)) ,
}
gd=GridSearchCV(estimator = KNeighborsClassifier() , param_grid = knn_hyperparameters,
cv=10, scoring = "roc_auc")
 gd.fit(X_final_processed, y)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_model>
def retirarOutliers(valor):
if(valor > 2174):
return 2174
else:
return valor
gd.best_estimator_.fit(X_final_processed, y)
 y_pred = gd.best_estimator_.predict(test_final_processed )<train_model>
df['hab_p_medico'] = df['hab_p_medico'].map(retirarOutliers )
knn = KNeighborsClassifier(algorithm='auto', leaf_size=26, metric='minkowski',
metric_params=None, n_jobs=None, n_neighbors=6, p=2,
weights='uniform')
 knn.fit(X_final_processed, y)
 y_pred = knn.predict(test_final_processed )<save_to_csv>
def retirarOutliers(valor):
if(valor < 0.3):
print(valor)
 return 0.3
else:
return valor
submission = pd.DataFrame(pd.read_csv(".. /input/test.csv")['PassengerId'])
 submission['Survived'] = y_pred
submission.to_csv("submission.csv", index = False )<import_modules>
df['perc_pop_econ_ativa'] = df['perc_pop_econ_ativa'].map(retirarOutliers )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score, roc_auc_score
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from collections import Counter<load_from_csv>
def limparAnosEstudos(valor):
if(valor < 1.9):
return 2
elif(valor > 11):
return 10
else:
return valor
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv' )<concatenate>
df['anos_estudo_empreendedor'] = df['anos_estudo_empreendedor'].map(limparAnosEstudos )
df = pd.concat([train,test] ).set_index('PassengerId' )<count_missing_values>
def limparJornada(valor):
if(valor > 50):
return 50
elif(valor < 30):
return 30
else:
return valor
df.isnull().sum()<data_type_conversions>
df['jornada_trabalho'] = df['jornada_trabalho'].map(limparJornada )
df.Fare = df.Fare.fillna(df.Fare.loc[df.Pclass==3].median() )<count_values>
def limparAnosEstudo(valor):
if(valor > 11):
return 11
elif(valor < 7.5):
return 7.5
else:
return valor
Counter(df.Embarked )<data_type_conversions>
df['exp_anos_estudo'] = df['exp_anos_estudo'].map(limparAnosEstudo )
df.Embarked = df.Embarked.fillna('S' )<feature_engineering>
def limparParticipacao(valor):
if(valor < 50):
return 55
else:
return valor
age = list()
for i in df.index:
if df.Age.isnull() [i]:
s = df['Pclass'][i]
age.append(df['Age'].loc[df['Pclass']== s].median())
 else : age.append(df['Age'][i])
 df.Age=age<categorify>
df['participacao_transf_receita'] = df['participacao_transf_receita'].map(limparParticipacao )
def Cabin_cla(x):
if x=='A' : return 4
elif x=='B'or'C': return 3
elif x=='D'or'D': return 2
elif x=='F'or'G': return 1
else : return 0
df['Cabin']=list(map(Cabin_cla,df.Cabin))<count_values>
def limparNotaMat(valor):
if(valor > 6.32):
return 6.32
else:
return valor
title = [i.split(' ')[1].split('.')[0].strip() for i in df.Name]
Counter(title )<categorify>
df['nota_mat'] = df['nota_mat'].map(limparNotaMat )
def Name_cla(x):
if x=='Mr': return 1
elif x=='Master': return 3
elif x=='Miss': return 4
elif x=='Mrs': return 5
else : return 2
df.Name = list(map(Name_cla,title))<drop_column>
np.percentile(df['populacao'], 75)
 np.percentile(df['populacao'], 25 )
df=df.drop(['Parch','SibSp'],axis=1 )<categorify>
irq = np.percentile(df['populacao'], 75)- np.percentile(df['populacao'], 25)
df.Embarked = [{'S':1, 'Q':2, 'C':3}[i] for i in df.Embarked]<prepare_x_and_y>
df['populacao'] = irq
y_train=df.Survived.dropna()
X_train=df[df.Survived.notnull() ].drop('Survived',axis=1)
 X_test=df[df.Survived.isnull() ].drop('Survived',axis=1 )<find_best_model_class>
np.percentile(df['gasto_pc_saude'], 75)
cv_score = list()
auc_score = list()
for i in [5,6,7,8,9]:
rfc=RandomForestClassifier(criterion='gini',random_state=4, max_depth=i)
 rfc.fit(X_train, y_train)
 tree_predicted = rfc.predict(X_train)
 auc_score.append(roc_auc_score(y_train,tree_predicted))
scores = cross_val_score(rfc, X_train, y_train,cv=5)
 cv_score.append(scores.mean())
 df_score=pd.DataFrame({'max_depth':[5,6,7,8,9],'cv_score':cv_score,'auc_score':auc_score} )<find_best_model_class>
np.percentile(df['gasto_pc_saude'], 25 )
rfc=RandomForestClassifier(criterion='gini',random_state=4, max_depth=8)
 rfc.fit(X_train, y_train)
 tree_predicted = rfc.predict(X_train)
 roc_auc_score(y_train,tree_predicted )<predict_on_test>
df['gasto_pc_saude'] = df['gasto_pc_saude'].map(limparGastoSaude )
rfc=RandomForestClassifier(criterion='gini',random_state=4, max_depth=7)
 rfc.fit(X_train, y_train)
 tree_predicted = rfc.predict(X_train)
 y_test = rfc.predict(X_test )<count_values>
def limpaTaxaEmpreendedorismo(valor):
if(valor > 0.4):
return 0.4
elif(valor < 0.12):
return 0.12
else:
return valor
sum(y_test )<save_to_csv>
df['taxa_empreendedorismo'] = df['taxa_empreendedorismo'].map(limpaTaxaEmpreendedorismo )
PassengerId = list(X_test.reset_index() ['PassengerId'])
 result = pd.DataFrame({'PassengerId':PassengerId,'Survived':y_test})
 result['Survived']=[int(i)for i in result.Survived]
result.to_csv('result.csv', index=False )<load_from_csv>
def limpaGastoEducacao(valor):
if(valor > 1100):
return 1100
elif(valor < 250):
return 250
else:
return valor
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<drop_column>
df['gasto_pc_educacao'] = df['gasto_pc_educacao'].map(limpaGastoEducacao )
full_train = train_data.drop(['Name', 'Parch', 'Ticket'], axis = 1)
 full_train.head()<filter>
df['area'] = 1173.985
full_train = full_train[pd.notnull(full_train['Embarked'])]<data_type_conversions>
train_raw = df[~df['nota_mat'].isnull() ]
full_train['Cabin'] = full_train['Cabin'].where(full_train['Cabin'].isna() , 1)
 full_train['Cabin'] = full_train['Cabin'].fillna(0)
 full_train['Cabin'].head()<categorify>
for coluna in train_raw.columns:
if(coluna == 'ranking_igm'):
continue
corr = np.corrcoef(train_raw['nota_mat'],train_raw[coluna])[0,1]
if(corr < 0.4 and corr > -0.4):
print(str(coluna)+ ': ' + str(corr))
s =(full_train.dtypes == 'object')
 object_cols = list(s[s].index)
 OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
 OH_cols = pd.DataFrame(OH_encoder.fit_transform(full_train[object_cols]))
OH_cols.index = full_train.index
num = full_train.drop(object_cols, axis=1)
 OH = pd.concat([num, OH_cols], axis=1)
 OH.columns<split>
for coluna in train_raw.columns:
if(coluna == 'ranking_igm'):
continue
corr = np.corrcoef(train_raw['nota_mat'],train_raw[coluna])[0,1]
if(corr < -0.4):
print(str(coluna)+ ': ' + str(corr))
X = OH.drop('Survived', axis=1)
 y = OH['Survived']
X_train, X_valid, y_train, y_valid = train_test_split(X, y,
train_size=0.8, test_size=0.2,
random_state=0 )<predict_on_test>
test = df[df['nota_mat'].isnull() ]
model = RandomForestRegressor(n_estimators=1500, random_state=0)
 model.fit(X_train, y_train)
 preds = model.predict(X_valid)
 mae = mean_absolute_error(y_valid, preds)
 print(mae )<categorify>
del test['nota_mat']
test_data = test_data.drop(['Name', 'Parch','Ticket'], axis = 1)
 test_data['Cabin'] = test_data['Cabin'].where(test_data['Cabin'].isna() , 1)
 test_data['Cabin'] = test_data['Cabin'].fillna(0)
 OH_cols = pd.DataFrame(OH_encoder.fit_transform(test_data[object_cols]))
OH_cols.index = test_data.index
num = test_data.drop(object_cols, axis=1)
 OH_test_data = pd.concat([num, OH_cols], axis=1)
 OH_test_data.fillna(OH_test_data.mean() , inplace = True )<save_to_csv>
from sklearn.model_selection import train_test_split
prediction = model.predict(OH_test_data)
 test_data['Survived'] = prediction
submission = test_data[['PassengerId','Survived']]
submission['Survived'].values[submission['Survived'] >= 0.7] = 1
submission['Survived'].values[submission['Survived'] < 0.7] = 0
submission.Survived = submission.Survived.astype(int)
 submission.to_csv('titanic_randomforest.csv', index=False)
 submission.info()<load_from_csv>
from sklearn.model_selection import train_test_split
df1 = pd.read_csv('.. /input/myncaa/M-massey-0.54.csv')
 df2 = pd.read_csv('.. /input/myncaa/M-courtside-0.56.csv')
 df1.head()<save_to_csv>
train, valid = train_test_split(train_raw, random_state=42 )
df1['Pred'] =.2*df1['Pred'] +.8*df2['Pred']
df1.to_csv('sub.csv',index=False)
 df1.head()<import_modules>
removed_cols = ['nota_mat', 'Unnamed: 0', 'gasto_pc_saude']
warnings.filterwarnings('ignore')
 train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print("Skewness: %f" % train_df['SalePrice'].skew())
 print("Kurtosis: %f" % train_df['SalePrice'].kurt())
 train_ID = train_df['Id']
test_ID = test_df['Id']
train_df.drop("Id", axis = 1, inplace = True)
 test_df.drop("Id", axis = 1, inplace = True)
 train_df.head()<feature_engineering>
feats = [c for c in df.columns if c not in removed_cols]
total = full_data.isnull().sum().sort_values(ascending=False)
 percent =(full_data.isnull().sum() /full_data.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20)
 full_data["LotFrontage"] = full_data.groupby("Neighborhood")["LotFrontage"].transform(lambda x: x.fillna(x.median()))
full_data.fillna(full_data.mean() , inplace=True)
 full_data.fillna(full_data.mode().iloc[0], inplace=True)
 full_data.isnull().sum().max()<data_type_conversions>
from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor
from sklearn.ensemble import AdaBoostRegressor, GradientBoostingRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from xgboost import XGBClassifier
<feature_engineering>
models = {'RandomForest': RandomForestRegressor(random_state=42),
'ExtraTrees': ExtraTreesRegressor(random_state=42),
'GBM': GradientBoostingRegressor(random_state=42),
'DecisionTree': DecisionTreeRegressor(random_state=42),
'AdaBoost': AdaBoostRegressor(random_state=42),
'KNN 1': KNeighborsRegressor(n_neighbors=1),
'KNN 3': KNeighborsRegressor(n_neighbors=3),
'KNN 11': KNeighborsRegressor(n_neighbors=11),
'SVR': SVR() ,
'Linear Regression': LinearRegression() }
train_df['SalePrice'] = np.log1p(train_df['SalePrice'])
 numeric_feats = full_data.dtypes[full_data.dtypes != "object"].index
skewed_feats = full_data[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skew_index = skewed_feats.index
print(skew_index)
 for i in skew_index:
full_data[i] = boxcox1p(full_data[i], boxcox_normmax(full_data[i] + 1))<categorify>
from sklearn.metrics import mean_squared_error
full_data = pd.get_dummies(full_data)
 full_data.head()<prepare_x_and_y>
from sklearn.metrics import mean_squared_error
X_train = full_data[:ntrain]
X_test = full_data[ntrain:]
y = train_df.SalePrice.values
kf = KFold(n_splits=12, random_state=42, shuffle=True)
 def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train.values, y, scoring="neg_mean_squared_error", cv = kf))
return(rmse)
 model_ridge = Ridge()
alphas = [0.05, 0.1, 0.3, 1, 3, 5, 10, 15, 30, 50, 75]
cv_ridge = [rmse_cv(Ridge(alpha = alpha)).mean() for alpha in alphas]
cv_ridge = pd.Series(cv_ridge, index = alphas)
 cv_ridge.plot(title = "Validation")
 plt.xlabel("alpha")
 plt.ylabel("rmse")
 print(cv_ridge.min())
 model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005] ).fit(X_train, y)
 print(rmse_cv(model_lasso ).mean())
 matplotlib.rcParams['figure.figsize'] =(6.0, 6.0)
 preds = pd.DataFrame({"preds":model_lasso.predict(X_train), "true":y})
 preds["residuals"] = preds["true"] - preds["preds"]
preds.plot(x = "preds", y = "residuals",kind = "scatter")
 dtrain = xgb.DMatrix(X_train, label = y)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=500, early_stopping_rounds=100)
 model.loc[30:,["test-rmse-mean", "train-rmse-mean"]].plot() 
<choose_model_class>
def run_model(model, train, valid, feats, y_name):
model.fit(train[feats], train[y_name])
 preds = model.predict(valid[feats])
 return mean_squared_error(valid[y_name], preds)**(1/2 )
lightgbm = LGBMRegressor(objective='regression',
num_leaves=6,
learning_rate=0.01,
n_estimators=7000,
max_bin=200,
bagging_fraction=0.8,
bagging_freq=4,
bagging_seed=8,
feature_fraction=0.2,
feature_fraction_seed=8,
min_sum_hessian_in_leaf = 11,
verbose=-1,
random_state=42)
 xgboost = XGBRegressor(learning_rate=0.01,
n_estimators=6000,
max_depth=4,
min_child_weight=0,
gamma=0.6,
subsample=0.7,
colsample_bytree=0.7,
objective='reg:linear',
nthread=-1,
scale_pos_weight=1,
seed=27,
reg_alpha=0.00006,
random_state=42)
 ridge_alphas = [1e-15, 1e-10, 1e-8, 9e-4, 7e-4, 5e-4, 3e-4, 1e-4, 1e-3, 5e-2, 1e-2, 0.1, 0.3, 1, 3, 5, 10, 15, 18, 20, 30, 50, 75, 100]
ridge = make_pipeline(RobustScaler() , RidgeCV(alphas=ridge_alphas, cv=kf))
svr = make_pipeline(RobustScaler() , SVR(C= 20, epsilon= 0.008, gamma=0.0003))
gbr = GradientBoostingRegressor(n_estimators=6000,
learning_rate=0.01,
max_depth=4,
max_features='sqrt',
min_samples_leaf=15,
min_samples_split=10,
loss='huber',
random_state=42)
 rf = RandomForestRegressor(n_estimators=1200,
max_depth=15,
min_samples_split=5,
min_samples_leaf=5,
max_features=None,
oob_score=True,
random_state=42)
 stack_gen = StackingCVRegressor(regressors=(xgboost, lightgbm, svr, ridge, gbr, rf),
meta_regressor=xgboost,
use_features_in_secondary=True)
 scores = {}
score = rmse_cv(lightgbm)
 print("lightgbm: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['lgb'] =(score.mean() , score.std())
 score = rmse_cv(xgboost)
 print("xgboost: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['xgb'] =(score.mean() , score.std())
 score = rmse_cv(svr)
 print("SVR: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['svr'] =(score.mean() , score.std())
 score = rmse_cv(ridge)
 print("ridge: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['ridge'] =(score.mean() , score.std())
 score = rmse_cv(rf)
 print("rf: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['rf'] =(score.mean() , score.std())
 score = rmse_cv(gbr)
 print("gbr: {:.4f}({:.4f})".format(score.mean() , score.std()))
scores['gbr'] =(score.mean() , score.std())
 sns.set_style("white")
 fig = plt.figure(figsize=(24, 12))
ax = sns.pointplot(x=list(scores.keys()), y=[score for score, _ in scores.values() ], markers=['o'], linestyles=['-'])
 for i, score in enumerate(scores.values()):
ax.text(i, score[0] + 0.002, '{:.6f}'.format(score[0]), horizontalalignment='left', size='large', color='black', weight='semibold')
 plt.ylabel('Score(RMSE)', size=20, labelpad=12.5)
 plt.xlabel('Model', size=20, labelpad=12.5)
 plt.tick_params(axis='x', labelsize=13.5)
 plt.tick_params(axis='y', labelsize=12.5)
 plt.title('Scores of Models', size=20)
 plt.show()<train_model>
train_feat = train[feats]
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features)
<train_model>
scores = []
for name, model in models.items() :
score = run_model(model, train, valid, feats, 'nota_mat')
 scores.append(score)
 print(name+':', score )
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
print('stack_gen')
 stack_gen_model = stack_gen.fit(np.array(X_train.values), np.array(y))
print('lightgbm')
 lgb_model_full_data = lightgbm.fit(X_train, y)
 print('xgboost')
 xgb_model_full_data = xgboost.fit(X_train, y)
 print('Svr')
 svr_model_full_data = svr.fit(X_train, y)
 print('Ridge')
 ridge_model_full_data = ridge.fit(X_train, y)
 print('RandomForest')
 rf_model_full_data = rf.fit(X_train, y)
 print('GradientBoosting')
 gbr_model_full_data = gbr.fit(X_train, y)
 def blended_predictions(X):
return(( 0.1 * ridge_model_full_data.predict(X)) + \ 
(0.2 * svr_model_full_data.predict(X)) + \ 
(0.1 * gbr_model_full_data.predict(X)) + \ 
(0.1 * xgb_model_full_data.predict(X)) + \ 
(0.1 * lgb_model_full_data.predict(X)) + \ 
(0.05 * rf_model_full_data.predict(X)) + \ 
(0.35 * stack_gen_model.predict(np.array(X))))
blended_score = rmsle(y, blended_predictions(X_train))
scores['blended'] =(blended_score, 0)
 print('RMSLE score on train data:')
 print(blended_score )<save_to_csv>
rf = RandomForestRegressor(random_state=42)
 rf.fit(train[feats], train['nota_mat'])
 pd.Series(rf.feature_importances_, index=feats ).sort_values().plot.barh()
predictions = blended_predictions(X_test)
 submission = pd.DataFrame()
submission['Id'] = test_ID
submission['SalePrice'] = np.floor(np.expm1(predictions))
submission.to_csv('submission.csv',index=False )<load_from_csv>
alg = GradientBoostingRegressor(random_state=42, min_samples_split = 500, min_samples_leaf = 50, max_depth = 8 , max_features = 'sqrt', subsample = 0.8)
training_df = pd.read_csv(".. /input/train.csv")
 testing_df = pd.read_csv(".. /input/test.csv")
 combine = [training_df, testing_df]<sort_values>
alg.fit(train[feats], train['nota_mat'] )
training_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
train_preds = alg.predict(train[feats] )
training_df[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
mean_squared_error(train['nota_mat'], train_preds)**(1/2 )
training_df[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
valid_preds = alg.predict(valid[feats] )
training_df[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
mean_squared_error(valid['nota_mat'], valid_preds)**(1/2 )
column_choice_training.Sex[column_choice_training.Sex == 'female'] = 0
column_choice_training.Sex[column_choice_training.Sex == 'male'] = 1
column_choice_test.Sex[column_choice_test.Sex == 'female'] = 0
column_choice_test.Sex[column_choice_test.Sex == 'male'] = 1
column_choice_training.head()<feature_engineering>
test['nota_mat'] = np.exp(alg.predict(test[feats]))
freq_port = column_choice_training.Embarked.dropna().mode() [0]
column_choice_training.Embarked = column_choice_training.Embarked.fillna(freq_port)
 column_choice_test.Embarked = column_choice_test.Embarked.fillna(freq_port)
 column_choice_training.Embarked[column_choice_training.Embarked == 'S'] = 0
column_choice_training.Embarked[column_choice_training.Embarked == 'C'] = 1
column_choice_training.Embarked[column_choice_training.Embarked == 'Q'] = 2
column_choice_test.Embarked[column_choice_test.Embarked == 'S'] = 0
column_choice_test.Embarked[column_choice_test.Embarked == 'C'] = 1
column_choice_test.Embarked[column_choice_test.Embarked == 'Q'] = 2
column_choice_training.head()<feature_engineering>
test[['codigo_mun', 'nota_mat']].to_csv('Edison_Martins_13.csv', index=False )