Kaggle competition: Atividade_3_PMR3508
train = pd.merge(
 train, train.groupby(
 ["Country_Region"], observed=True, sort=False
).apply(lambda x: fit_logistic(x, n_jobs=8, n_samples=80, maxfev=16000)).reset_index() ,
on=["Country_Region"], how="left")
 predict_logistic(train )<train_model>
plt.rcParams['figure.figsize'] = [20, 10]
%matplotlib inline
def apply_xgb_model(train, x_columns, y_column, xgb_params):
X = train[x_columns].to_numpy()
y = train[y_column].to_numpy()
xgb_fit = XGBRegressor(**xgb_params ).fit(X, y)
 y_hat = xgb_fit.predict(X)
 train[f"yhat_xgb_{y_column}"] = y_hat
return RMSLE(y, y_hat), xgb_fit<init_hyperparams>
dataset = pd.read_csv(FOLDER+'train.csv' )
xgb_params_c = dict(
 gamma=0.1,
learning_rate=0.35,
n_estimators=221,
max_depth=15,
min_child_weight=1,
nthread=8,
objective="reg:squarederror")
 xgb_params_f = dict(
 gamma=0.1022,
learning_rate=0.338,
n_estimators=292,
max_depth=14,
min_child_weight=1,
nthread=8,
objective="reg:squarederror")
 x_columns = ['DayOfYear',
'Diabetes, blood, & endocrine diseases(%)', 'Respiratory diseases(%)',
'Diarrhea & common infectious diseases(%)',
'Nutritional deficiencies(%)',
'obesity - adult prevalence rate',
'pneumonia-death-rates', 'animal_fats', 'animal_products', 'eggs',
'offals', 'treenuts', 'vegetable_oils', 'nbr_surgeons',
'nbr_anaesthesiologists', 'population',
'school_shutdown_1case',
'school_shutdown_10case', 'school_shutdown_50case',
'school_shutdown_1death', 'case1_DayOfYear', 'case10_DayOfYear',
'case50_DayOfYear',
'school_closure_status_daily', 'case_delta1_10',
'case_death_delta1', 'case_delta1_100', 'days_since','Lat','Long','weekday',
'yhat_logistic_ConfirmedCases', 'yhat_logistic_Fatalities'
]
xgb_c_rmsle, xgb_c_fit = apply_xgb_model(train, x_columns, "ConfirmedCases", xgb_params_c)
 xgb_f_rmsle, xgb_f_fit = apply_xgb_model(train, x_columns, "Fatalities", xgb_params_f )<train_model>
class MeineLinearRegression(linear_model.LinearRegression):

 def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
 def R(self, y, X):
if X.shape[1] == 0:
return 0
reg = linear_model.LinearRegression()
reg.fit(X, y)
 y_pred = reg.predict(X)
 return r2_score(y_pred, y)
 def fit(self, X, y, sample_weight=None):
self = super().fit(X, y, sample_weight)
 y_pred = self.predict(X)
 Ryh = r2_score(y, y_pred)
 self.r2 = Ryh
X = np.array(X)
 y = np.array(y)
 self.rse = np.sqrt(np.sum(( self.predict(X)- y)** 2, axis=0)/ float(X.shape[0] - X.shape[1] - 1))
self.se = np.array([])
 for i, x in enumerate(X.T):
n = x - x.mean()
Rxg = self.R(x, np.delete(X, i, 1))
Sbk = np.sqrt(( 1 - Ryh)/(1 - Rxg)/(X.shape[0] - X.shape[1] - 1)) *np.std(y)/np.std(x)
 self.se = np.append(self.se, Sbk)
 self.t = self.coef_ / self.se
self.p = [2 *(1 - stats.t.cdf(np.abs(t_stu), df = y.shape[0] - X.shape[1])) for t_stu in self.t]
def interpolate(alpha, x0, x1):
return x0 * alpha + x1 *(1 - alpha)
 def RMSLE_interpolate(alpha, y, x0, x1):
return RMSLE(y, interpolate(alpha, x0, x1))
def fit_hybrid(
 train: pd.DataFrame, y_cols: List[str] = ["ConfirmedCases", "Fatalities"] 
)-> pd.DataFrame:
def fit_one(y_col: str):
opt = least_squares(
 fun=RMSLE_interpolate,
args=(
 train[y_col],
train[f"yhat_logistic_{y_col}"],
train[f"yhat_xgb_{y_col}"], 
),
x0=(0.5,),
bounds=(( 0.0),(1.0,)) , 
)
 return {f"{y_col}_alpha": opt.x[0], f"{y_col}_cost": opt.cost}
result = {}
for y_col in y_cols:
result.update(fit_one(y_col))
return pd.DataFrame([result])
 def predict_hybrid(
 df: pd.DataFrame,
x_col: str = "DayOfYear",
y_cols: List[str] = ["ConfirmedCases", "Fatalities"], 
):
def predict_one(col):
df[f"yhat_hybrid_{col}"] = interpolate(
 df[f"{y_col}_alpha"].to_numpy() ,
df[f"yhat_logistic_{y_col}"].to_numpy() ,
df[f"yhat_xgb_{y_col}"].to_numpy() , 
)
 for y_col in y_cols:
predict_one(y_col )<merge>
baseline = dataset.copy()
target = 'median_house_value'
result = []
np.seterr(all='ignore')
 for label in baseline.columns[1:-1]:
X = baseline[label]
y = baseline[target]
reg = MeineLinearRegression()
reg.fit(X.values.reshape(-1, 1), y)
 F, p = f_regression(X.values.reshape(-1, 1), y)
 result.append({'Label': label,
'Beta': '{0:.2f}'.format(reg.coef_[0]),
't':'{0:.2f}'.format(reg.t[0]),
'p-value':'{0:.6f}'.format(p[0]),
'R²':"{0:.2f}%".format(reg.r2 * 100),
'F-value':'{0:.2f}'.format(F[0])})
 pd.DataFrame(result, columns=['Label', 'Beta', 'R²', 't', 'F-value', 'p-value'] )
train = pd.merge(
 train,
train.groupby(["Country_Region"], observed=True, sort=False)
.apply(lambda x: fit_hybrid(x)) 
.reset_index() ,
on=["Country_Region"],
how="left", 
)
<predict_on_test>
X = baseline[baseline.columns[1:-1]]
y = baseline[target]
reg = MeineLinearRegression()
reg.fit(X, y)
 result_2 = []
for n, label in enumerate(X.columns):
result_2.append({'Label': label,
'Beta': '{0:.2f}'.format(reg.coef_[n]),
't':'{0:.2f}'.format(reg.t[n]),
'p-value':'{0:.6f}'.format(reg.p[n])})
 print("R²: {0:.2f}%".format(reg.r2 * 100))
pd.DataFrame(result_2 )
predict_hybrid(train )<compute_test_metric>
test_1 = dataset[dataset['median_house_value'] < 500000]
target = 'median_house_value'
X = test_1[test_1.columns[1:-1]]
y = test_1[target]
evaluation = linear_model.LinearRegression()
score = cross_val_score(evaluation, X, y, cv = 10, scoring='r2')
 print('R² = {0:.3f} ± {1:.3f}'.format(np.mean(score), 2*np.std(score)))
 reg = MeineLinearRegression()
reg.fit(X, y)
 result_2 = []
for n, label in enumerate(X.columns):
result_2.append({'Label': label,
'Beta': '{0:.2f}'.format(reg.coef_[n]),
't':'{0:.2f}'.format(reg.t[n]),
'p-value':'{0:.6f}'.format(reg.p[n])})
 pd.DataFrame(result_2 )
print(
 "Confirmed:
"
f'Logistic\t{RMSLE(train["ConfirmedCases"], train["yhat_logistic_ConfirmedCases"])}
'
f'XGBoost\t{RMSLE(train["ConfirmedCases"], train["yhat_xgb_ConfirmedCases"])}
'
f'Hybrid\t{RMSLE(train["ConfirmedCases"], train["yhat_hybrid_ConfirmedCases"])}
'
f"Fatalities:
"
f'Logistic\t{RMSLE(train["Fatalities"], train["yhat_logistic_Fatalities"])}
'
f'XGBoost\t{RMSLE(train["Fatalities"], train["yhat_xgb_Fatalities"])}
'
f'Hybrid\t{RMSLE(train["Fatalities"], train["yhat_hybrid_Fatalities"])}
'
)<merge>
non_lin = dataset.copy()
stat = []
target = 'median_house_value'
for label_1 in non_lin.columns[3:-1]:
for label_2 in non_lin.columns[3:-1]:
if label_2 != label_1:
X = non_lin[label_1]/non_lin[label_2]
reg = linear_model.LinearRegression()
reg.fit(X.values.reshape(-1, 1), non_lin[target])
 F, p = f_regression(X.values.reshape(-1, 1), non_lin[target])
 y = reg.predict(X.values.reshape(-1, 1))
r2 = r2_score(non_lin[target], y)
 stat.append({'Label': label_1+'/'+label_2,
'Beta': '{0:.2f}'.format(reg.coef_[0]),
'F':'{0:.0f}'.format(F[0]),
'p-value':'{0:.6f}'.format(p[0]),
'R²':"{0:.2f}%".format(r2 * 100),
'corr':'{0:.5f}'.format(np.corrcoef(X,non_lin[target])[0][1]),
'raw_R²':r2})
 output = pd.DataFrame(stat, columns = ['Label', 'Beta', 'R²', 'p-value', 'F', 'corr', 'raw_R²'])
 output = output[output['raw_R²'] > 0.01 ]
output.sort_values(by='raw_R²', ascending=False )
test = pd.merge(
 test,
train[["Country_Region"] +
['ConfirmedCases_p_0', 'ConfirmedCases_p_1', 'ConfirmedCases_p_2']+
['Fatalities_p_0','Fatalities_p_1', 'Fatalities_p_2'] +
["Fatalities_alpha"] +
["ConfirmedCases_alpha"]].groupby(['Country_Region'] ).head(1), on="Country_Region", how="left" )<predict_on_test>
def add_features(dset):
mean_houses = pd.Series(dset['households']/dset['population'], name = 'mean_households')
 rooms_ratio = pd.Series(dset['total_rooms']/dset['total_bedrooms'], name = 'ratio')
 return pd.concat([mean_houses, rooms_ratio, dset], axis = 1 )
predict_logistic(test)
 test["yhat_xgb_ConfirmedCases"] = xgb_c_fit.predict(test[x_columns].to_numpy())
 test["yhat_xgb_Fatalities"] = xgb_f_fit.predict(test[x_columns].to_numpy())
 predict_hybrid(test )<prepare_output>
stat = []
target = 'median_house_value'
for n, label_1 in enumerate(non_lin.columns[3:-1]):
for label_2 in non_lin.columns[3+n:-1]:
X = non_lin[label_1]*non_lin[label_2]
reg = linear_model.LinearRegression()
reg.fit(X.values.reshape(-1, 1), non_lin[target])
 F, p = f_regression(X.values.reshape(-1, 1), non_lin[target])
 y = reg.predict(X.values.reshape(-1, 1))
r2 = r2_score(non_lin[target], y)
 stat.append({'Label': label_1+'*'+label_2,
'Beta': '{0:.5f}'.format(reg.coef_[0]),
't':'{0:.2f}'.format(np.sqrt(F[0])) ,
'p-value':'{0:.6f}'.format(p[0]),
'R²':"{0:.2f}%".format(r2 * 100),
'corr':'{0:.5f}'.format(np.corrcoef(X,non_lin[target])[0][1]),
'raw_R²':r2})
 output = pd.DataFrame(stat, columns = ['Label', 'Beta', 'R²', 'p-value', 't', 'corr', 'raw_R²'])
 output = output[output['raw_R²'] > 0.01 ]
output.sort_values(by='raw_R²', ascending=False )
submission = test[["ForecastId", "yhat_hybrid_ConfirmedCases", "yhat_hybrid_Fatalities"]].round(2 ).rename(
 columns={
"yhat_hybrid_ConfirmedCases": "ConfirmedCases",
"yhat_hybrid_Fatalities": "Fatalities",
} 
)
 submission["ConfirmedCases"] = np.maximum(0, submission["ConfirmedCases"])
 submission["Fatalities"] = np.maximum(0, submission["Fatalities"] )<save_to_csv>
test = pd.read_csv(FOLDER+'test.csv')
 test.head()
submission.to_csv("submission.csv", index=False )<save_to_csv>
def submit_pred(dset, dtest, model = linear_model.LinearRegression() , name = 'submit'):
target = 'median_house_value'
labels = dset.columns.drop('Id' ).drop(target)
 X = dset[labels]
y = dset[target]
score = cross_val_score(model, X, y, cv = 10, scoring='r2')
 print('R² = {0:.3f} ± {1:.3f}'.format(np.mean(score), 2*np.std(score)))
 model.fit(X, y)
 y_test_raw = model.predict(dtest[labels])
 y_test = [np.max([entry, 0])for entry in y_test_raw]
output = pd.concat([test['Id'], pd.Series(y_test, name=target)], axis=1)
 output.to_csv('./{0}.csv'.format(name), index=False )
submission.to_csv("submission.csv", index=False )<set_options>
submit_pred(dataset, test, name = 'raw_linear_model' )
color = sns.color_palette()
%matplotlib inline<define_variables>
submit_pred(add_features(dataset), add_features(test), name = 'improv_linear_model' )
PATH = '.. /input/mercedes-benz-greener-manufacturing/'
!ls {PATH}<load_from_csv>
d_dset = add_dist(add_features(dataset))
d_test = add_dist(add_features(test))
df_raw = pd.read_csv(f'{PATH}train.csv.zip')
 test = pd.read_csv(f'{PATH}test.csv.zip' )<count_missing_values>
submit_pred(d_dset,
d_test,
name = 'linear_model_dists_gps' )
display_all(df_raw.isnull().sum() /len(df_raw)) 
<compute_test_metric>
submit_pred(d_dset.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
d_test.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
name = 'linear_model_dists' )
df, y, nas = proc_df(df_raw, 'y' )<train_model>
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = linear_model.LassoLars(alpha=.1),
name = 'linear_model_lasso' )
m = RandomForestRegressor(n_jobs=-1)
 m.fit(df,y )<compute_test_metric>
submit_pred(d_dset.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
d_test.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
model = linear_model.LassoLars(alpha=.1),
name = 'linear_model_lasso' )
m.score(df,y )<create_dataframe>
mean = []
stds = []
K = 1
M = 60
for i in range(K, M, 3):
knn = neighbors.KNeighborsRegressor(n_neighbors = i)
 score = cross_val_score(knn, d_dset.drop(target, axis = 1), d_dset[target], scoring = 'r2', cv = 10)
 mean.append(np.mean(score))
stds.append(np.std(score))
table = pd.DataFrame()
table['Mean'] = mean
table['Std'] = stds
table
apply_cats(test, df_raw)
 sub = pd.DataFrame()
sub['ID'] = test.ID 
<categorify>
submit_pred(d_dset,
d_test,
model = linear_model.BayesianRidge() ,
name = 'linear_model_bayes_gps' )
test_df, _, _ = proc_df(test, na_dict=nas)
 df, _, _ = proc_df(df, na_dict=nas )<split>
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = linear_model.BayesianRidge() ,
name = 'linear_model_bayes' )
def split_vals(a,n): return a[:n].copy() , a[n:].copy()
n_valid = 800
n_trn = len(df)-n_valid
raw_train, raw_valid = split_vals(df_raw, n_trn)
 X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 X_train.shape, y_train.shape, X_valid.shape<train_model>
submit_pred(d_dset,
d_test,
model = ensemble.RandomForestRegressor(n_estimators = 100),
name = 'random_forest_gps' )
m = RandomForestRegressor(n_jobs=-1, max_samples=250, oob_score=True, n_estimators=120)
 %time m.fit(X_train, y_train)
 def print_score(m):
res = [m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res)
 print_score(m)
<save_to_csv>
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = ensemble.RandomForestRegressor(n_estimators = 100),
name = 'random_forest' )
sub['y'] = m.predict(test_df)
 sub.to_csv('rf_models_fastai300all.csv', index=False )<set_options>
submit_pred(d_dset,
d_test,
model = ensemble.ExtraTreesRegressor(n_estimators = 100),
name = 'extra_trees_gps' )
color = sns.color_palette()
%matplotlib inline<define_variables>
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = ensemble.ExtraTreesRegressor(n_estimators = 100),
name = 'extra_trees' )
PATH = '.. /input/mercedes-benz-greener-manufacturing/'
!ls {PATH}<load_from_csv>
submit_pred(d_dset,
d_test,
model = ensemble.AdaBoostRegressor(base_estimator =
ensemble.RandomForestRegressor(max_depth=20, n_estimators=10), n_estimators=50),
name = 'ada_boost_gps' )