Kaggle competition: IESB - 2019
NFOLDS = 200
folds = KFold(n_splits=NFOLDS)
 columns = X_train.columns
splits = folds.split(X_train, y_train )<define_variables>
df = pd.read_csv('.. /input/train.csv' )
y_preds_lgb = np.zeros(test_df.shape[0])
 y_oof_lgb = np.zeros(X_train.shape[0] )<train_model>
test = pd.read_csv('.. /input/test.csv' )
for fold_n,(train_index, valid_index)in enumerate(splits):
print('Fold:',fold_n+1)
 X_train1, X_valid1 = X_train[columns].iloc[train_index], X_train[columns].iloc[valid_index]
y_train1, y_valid1 = y_train.iloc[train_index], y_train.iloc[valid_index]
dtrain = lgb.Dataset(X_train1, label=y_train1)
 dvalid = lgb.Dataset(X_valid1, label=y_valid1)
 clf = lgb.train(params_lgb, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200)
 y_pred_valid = clf.predict(X_valid1)
 y_oof_lgb[valid_index] = y_pred_valid
y_preds_lgb += clf.predict(test_df)/ NFOLDS
del X_train1, X_valid1, y_train1, y_valid1
gc.collect()<prepare_x_and_y>
df['area']=[float(a)for a in [i.replace(',','')for i in list(df['area'].values)]]
<load_from_csv>
df['densidade_dem']=[float(i)for i in [i.replace(',','')for i in [str(i)for i in df['densidade_dem']]]]
submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = y_preds_lgb
submission_df<load_from_csv>
test['area']=[float(a)for a in [i.replace(',','')for i in list(test['area'].values)]]
<save_to_csv>
test['densidade_dem']=[float(i)for i in [i.replace(',','')for i in [str(i)for i in test['densidade_dem']]]]
submission_df.to_csv('submission.csv', index=False )<set_options>
test['ranking_igm'].isnull().sum() ,sum(test['ranking_igm'].isnull() ==False )
print(os.listdir(".. /input"))
%matplotlib inline
mpl.rc('axes', labelsize=14)
 mpl.rc('xtick', labelsize=12)
 mpl.rc('ytick', labelsize=12)
 warnings.filterwarnings("ignore" )<load_from_csv>
feats=[a for a in df.columns if a not in ['nota_mat']+d]
train=pd.read_csv('.. /input/train.csv' )<load_from_csv>
for i in df[feats].columns:
if df[i].isnull().sum() >0:
if df[i].dtypes!='object':
df[i]=df[i].fillna(df[i].mean())
 else:
continue
else:
continue
test=pd.read_csv('.. /input/test.csv' )<count_missing_values>
df[feats].isnull().sum()
train.isnull().sum()<count_missing_values>
for i in test[feats].columns:
if test[i].isnull().sum() >0:
if test[i].dtypes!='object':
test[i]=test[i].fillna(test[i].mean())
 else:
continue
else:
continue
train.isnull().sum()<count_missing_values>
df.loc[:,feats].isnull().sum()
test.isnull().sum()<count_missing_values>
for i in range(0,len(df.columns)) :
df[df.columns[i]] = np.log(df[df.columns[i]] + 1 )
test.isnull().sum()<count_values>
for i in range(0,len(test.columns)) :
test[test.columns[i]] = np.log(test[test.columns[i]] + 1)
train['Survived'].value_counts()<count_values>
from sklearn.model_selection import train_test_split
train['Pclass'].value_counts()<groupby>
train, valid = train_test_split(df, random_state=42 )
train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean()<count_values>
train, valid = train_test_split(df, random_state=42 )
train['Sex'].value_counts()<groupby>
train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean()<groupby>
train.groupby('Pclass' ).apply(lambda x:x.groupby('Sex')['Survived'].mean() ).style.background_gradient(cmap='cool' )<count_unique_values>
param_grid = {
'bootstrap': [True],
'max_depth': [80, 90, 100, 110],
'max_features': [2, 3],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [8, 10, 12],
'n_estimators': [100, 200, 300, 1000]
}
rf = RandomForestRegressor()
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid,
cv = 3, n_jobs = -1, verbose = 2 )
train['Cabin'].nunique()<count_unique_values>
grid_search.fit(train[feats], train['nota_mat'] )
train['Ticket'].nunique()<count_unique_values>
grid_search.best_params_
train['PassengerId'].nunique()<count_values>
from sklearn.ensemble import RandomForestRegressor
train['Embarked'].value_counts()<data_type_conversions>
from sklearn.ensemble import RandomForestRegressor
train['Embarked'].fillna('S', inplace=True )<count_values>
rf = RandomForestRegressor(random_state=42, n_jobs=-1, min_samples_leaf=3,max_features=3,min_samples_split=8,bootstrap=True,max_depth=90,
n_estimators=200 )
train['Embarked'].value_counts()<feature_engineering>
rf.fit(train[feats], train['nota_mat'] )
train['FamilySize'] = train['SibSp'] + train['Parch']
test['FamilySize'] = test['SibSp'] + test['Parch']<import_modules>
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import LabelEncoder<feature_engineering>
from sklearn.metrics import mean_squared_error
test['Fare'].fillna(test['Fare'].median() , inplace = True)
 train['FareCategory'] = pd.qcut(train['Fare'], 5)
 test['FareCategory'] = pd.qcut(test['Fare'], 5 )<categorify>
mean_squared_error(rf.predict(valid[feats]), valid['nota_mat'])**(1/2 )
train['Fare_Code'] = LabelEncoder().fit_transform(train['FareCategory'] )<categorify>
final=pd.DataFrame([np.exp(test.codigo_mun ).values,np.exp(rf.predict(test[feats])) ] ).T
test['Fare_Code'] = LabelEncoder().fit_transform(test['FareCategory'] )<feature_engineering>
final.columns=['codigo_mun','nota_mat']
train['Title'] = 0
for salut in train:
train['Title'] = train.Name.str.extract('([A-Za-z]+)\.')
 test['Title'] = 0
for salut in test:
test['Title'] = test.Name.str.extract('([A-Za-z]+)\.' )<categorify>
test2=pd.read_csv('.. /input/test.csv' )
mapping = {'Mlle': 'Miss',
'Major': 'Mr',
'Col': 'Mr',
'Sir': 'Mr',
'Don': 'Mr',
'Mme': 'Miss',
'Jonkheer': 'Mr',
'Lady': 'Mrs',
'Capt': 'Mr',
'Countess': 'Mrs',
'Ms': 'Miss',
'Dona': 'Mrs'
}
train.replace({'Title': mapping}, inplace=True)
 test.replace({'Title': mapping}, inplace=True )<feature_engineering>
test2.codigo_mun=[i.replace("ID_ID_","")for i in test2.codigo_mun]
data_df = train.append(test)
 titles = ['Dr', 'Master', 'Miss', 'Mr', 'Mrs', 'Rev']
for title in titles:
age_to_impute = data_df.groupby('Title')['Age'].median() [titles.index(title)]
data_df.loc[(data_df['Age'].isnull())&(data_df['Title'] == title), 'Age'] = age_to_impute<feature_engineering>
final.codigo_mun=[int(i)for i in final.codigo_mun]
train['Age'] = data_df['Age'][:891]
test['Age'] = data_df['Age'][891:]<set_options>
final.to_csv('Breno.csv',index=False )