Kaggle competition: IESB Norte - IGM - Maio 2019
stack_model = stacking(mod=[lasso,ridge,svr,ker,ela,bay],meta_model=ker )<compute_test_metric>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 df=test.append(train)
 feats = [c for c in df.columns if c not in ['codigo_mun', 'comissionados_por_servidor']]
score = rmse_cv(stack_model,a,b)
 print(score.mean() )<prepare_x_and_y>
regx = re.compile('\(\d+\)')
 regx2 = re.compile('\,?\.?')
 df['populacao']=df['populacao'].str.replace(regx2,'' ).str.replace(regx, '' ).astype(dtype=np.int )
X_train_stack, X_test_stack = stack_model.get_oof(a,b,test_X_scaled )<concatenate>
df['area']=df['area'].str.replace(',','' ).astype(float )
X_train_add = np.hstack(( a,X_train_stack))<concatenate>
df['densidade_dem']=df['densidade_dem'].str.replace(',','' ).astype(float)
 df['cat_porte'] = df['porte'].astype('category' ).cat.codes
df['cat_regiao'] = df['regiao'].astype('category' ).cat.codes
df['cat_estado'] = df['estado'].astype('category' ).cat.codes
X_test_add = np.hstack(( test_X_scaled,X_test_stack))<compute_test_metric>
feats = [c for c in df.columns if c not in ['codigo_mun', 'comissionados_por_servidor','nota_mat', 'densidade_dem',
'participacao_transf_receita', 'servidores', 'perc_pop_econ_ativa',
'gasto_pc_saude', 'hab_p_medico',
'exp_anos_estudo', 'regiao', 'estado', 'porte', 'municipio']]
score = rmse_cv(stack_model,X_train_add,b)
 print(score.mean() )<train_model>
train=df[~df.nota_mat.isnull() ]
test_submissao=df[df.nota_mat.isnull() ]
stack_model.fit(a,b )<predict_on_test>
train_2, test = train_test_split(train, test_size=0.20, random_state=42)
 train_2, valid = train_test_split(train_2, test_size=0.20, random_state=42)
 rf = RandomForestClassifier(random_state=42, n_estimators=300, min_samples_split=5, max_depth=4)
pred = np.exp(stack_model.predict(test_X_scaled))<save_to_csv>
rf.fit(train_2[feats], train_2['nota_mat'])
 preds = rf.predict(valid[feats])
result=pd.DataFrame({'Id':test.Id, 'SalePrice':pred})
 result.to_csv("submission.csv",index=False )<load_from_csv>
test_submissao['nota_mat']=rf.predict(test_submissao[feats])
 test_submissao[['codigo_mun','nota_mat']].to_csv('respostas.csv', index=False)
df_train = pd.read_csv('.. /input/titanic/train.csv')
 df_test = pd.read_csv('.. /input/titanic/test.csv')
 df_sub = pd.read_csv('.. /input/titanic/gender_submission.csv' )<categorify>
accuracy_score(valid['nota_mat'], preds)
df_train.drop(['Name','Ticket','Cabin'],axis=1,inplace=True)
 df_test.drop(['Name','Ticket','Cabin'],axis=1,inplace=True)
 sex = pd.get_dummies(df_train['Sex'],drop_first=True)
 embark = pd.get_dummies(df_train['Embarked'],drop_first=True)
 df_train = pd.concat([df_train,sex,embark],axis=1)
 df_train.drop(['Sex','Embarked'],axis=1,inplace=True)
 sex = pd.get_dummies(df_test['Sex'],drop_first=True)
 embark = pd.get_dummies(df_test['Embarked'],drop_first=True)
 df_test = pd.concat([df_test,sex,embark],axis=1)
 df_test.drop(['Sex','Embarked'],axis=1,inplace=True)
 df_train.fillna(df_train.mean() ,inplace=True)
 df_test.fillna(df_test.mean() ,inplace=True)
 Scaler1 = StandardScaler()
Scaler2 = StandardScaler()
train_columns = df_train.columns
test_columns = df_test.columns
df_train = pd.DataFrame(Scaler1.fit_transform(df_train))
df_test = pd.DataFrame(Scaler2.fit_transform(df_test))
df_train.columns = train_columns
df_test.columns = test_columns
features = df_train.iloc[:,2:].columns.tolist()
target = df_train.loc[:, 'Survived'].name
X_train = df_train.iloc[:,2:].values
y_train = df_train.loc[:, 'Survived'].values<import_modules>
accuracy_score(test['nota_mat'], rf.predict(test[feats]))
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.autograd import Variable<concatenate>
rf=RandomForestRegressor(random_state=42, n_estimators=100)
 rf.fit(train_2[feats], train_2['nota_mat'])
 preds = rf.predict(valid[feats])
 mean_squared_error(valid['nota_mat'], preds)**(1/2)
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.fc1 = nn.Linear(8, 512)
 self.fc2 = nn.Linear(512, 512)
 self.fc3 = nn.Linear(512, 2)
 self.dropout = nn.Dropout(0.2)
 def forward(self, x):
x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = F.relu(self.fc2(x))
x = self.dropout(x)
 x = self.fc3(x)
 return x
model = Net()
print(model )<choose_model_class>
dt=DecisionTreeRegressor(random_state=44)
 dt.fit(train_2[feats], train_2['nota_mat'])
 dt.predict(train_2[feats])
 data = tree.export_graphviz(dt, out_file=None, feature_names=feats, class_names=['nota_mat'],
filled=True, rounded=True, special_characters=True, max_depth=2 )