Kaggle competition: COVID19 Global Forecasting (Week 2)
tl = TomekLinks()
X_tl, y_tl = tl.fit_resample(X,y)
 y_tl['Target'].value_counts()<train_model>
PATH_WEEK2='/kaggle/input/covid19-global-forecasting-week-2'
df_train = pd.read_csv(f'{PATH_WEEK2}/train.csv')
 df_test = pd.read_csv(f'{PATH_WEEK2}/test.csv' )
df_under = pd.concat([X_tl, y_tl], axis=1)
 train, test = train_test_split(df_under, test_size=0.2, random_state=42)
 rf.fit(train[feats], train['Target'])
 preds_test = rf.predict(test[feats])
 accuracy_score(test['Target'], preds_test )<train_model>
df_train.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_test.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_train.rename(columns={'Province_State':'State'}, inplace=True)
 df_test.rename(columns={'Province_State':'State'}, inplace=True )
sm = SMOTE()
X_sm, y_sm = sm.fit_resample(X,y)
 y_sm['Target'].value_counts()<train_model>
df_train['Date'] = pd.to_datetime(df_train['Date'], infer_datetime_format=True)
 df_test['Date'] = pd.to_datetime(df_test['Date'], infer_datetime_format=True )
df_over = pd.concat([X_sm, y_sm], axis=1)
 train, test = train_test_split(df_over, test_size=0.2, random_state=42)
 rf.fit(train[feats], train['Target'])
 preds_test = rf.predict(test[feats])
 accuracy_score(test['Target'], preds_test )<load_from_csv>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
df = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/train.csv')
 test = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/test.csv')
 df.shape, test.shape<count_values>
X_Train = df_train.copy()
X_Train['State'].fillna(EMPTY_VAL, inplace=True)
 X_Train['State'] = X_Train.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Train.loc[:, 'Date'] = X_Train.Date.dt.strftime("%m%d")
 X_Train["Date"] = X_Train["Date"].astype(int)
 X_Train.head()
heads = df.loc[df['parentesco1'] == 1].copy()
train_campos = df.loc[(df['Target'].notnull())&(df['parentesco1'] == 1), ['Target', 'idhogar']]
l_counts = train_campos['Target'].value_counts().sort_index()
l_counts 
<count_unique_values>
X_Test = df_test.copy()
X_Test['State'].fillna(EMPTY_VAL, inplace=True)
 X_Test['State'] = X_Test.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Test.loc[:, 'Date'] = X_Test.Date.dt.strftime("%m%d")
 X_Test["Date"] = X_Test["Date"].astype(int)
 X_Test.head()
all_equal = df.groupby('idhogar')['Target'].apply(lambda x: x.nunique() == 1)
 not_equal = all_equal[all_equal != True]
print('Encontramos {} Individuos na mesma familia no mesmo domícilio que possuem um nível de probreza diferente e precisamos corrigir.'.format(len(not_equal)) )<feature_engineering>
le = preprocessing.LabelEncoder()
for household in not_equal.index:
true_target = int(df[(df['idhogar'] == household)&(df['parentesco1'] == 1.0)]['Target'])
 df.loc[df['idhogar'] == household, 'Target'] = true_target
all_equal = df.groupby('idhogar')['Target'].apply(lambda x: x.nunique() == 1)
 not_equal = all_equal[all_equal != True]
print('Encontramos {} familias no mesmo domícilio que possuem um nível de probreza diferente.'.format(len(not_equal)) )<groupby>
X_Train.Country = le.fit_transform(X_Train.Country)
 X_Train['State'] = le.fit_transform(X_Train['State'])
 X_Train.head()
households_leader = df.groupby('idhogar')['parentesco1'].sum()
households_no_head = df.loc[df['idhogar'].isin(households_leader[households_leader == 0].index), :]
print('There are {} households without a head.'.format(households_no_head['idhogar'].nunique())) 
<count_unique_values>
X_Test.Country = le.fit_transform(X_Test.Country)
 X_Test['State'] = le.fit_transform(X_Test['State'])
 X_Test.head()
households_no_head_equal = households_no_head.groupby('idhogar')['Target'].apply(lambda x: x.nunique() == 1)
 print('{} Households with no head have different labels.'.format(sum(households_no_head_equal == False)) )<concatenate>
df_train.loc[df_train.Country == 'Afghanistan', :]
df_all = df.append(test)
 df_all.shape<drop_column>
filterwarnings('ignore' )
df_all.drop('area2', axis = 1, inplace = True )<drop_column>
param_grid = {'n_estimators': [1000]}
def gridSearchCV(model, X_Train, y_Train, param_grid, cv=10, scoring='neg_mean_squared_error'):
start = time.time()
grid_cv = GridSearchCV(model, param_grid, cv=10, scoring="neg_mean_squared_error")
 grid_cv.fit(X_Train, y_Train)
 return grid_cv.best_estimator_
df_all['walls'] = np.argmax(np.array(df_all[['epared1', 'epared2', 'epared3']]),
axis = 1)
 df_all = df_all.drop(columns = ['epared1', 'epared2', 'epared3'])
<count_values>
le = preprocessing.LabelEncoder()
df_all['walls'].value_counts()<prepare_x_and_y>
df_out.ForecastId = df_out.ForecastId.astype('int')
 df_out.tail()
<prepare_x_and_y><EOS> 
df_out.to_csv('submission.csv', index=False )