Kaggle competition: Atividade_3_PMR3508
df_raw = pd.read_csv(f'{PATH}train.csv.zip')
 test = pd.read_csv(f'{PATH}test.csv.zip' )<count_missing_values>
train_data = pd.read_csv(".. /input/atividade-3-pmr3508/train.csv")
 test_data = pd.read_csv(".. /input/atividade-3-pmr3508/test.csv")
 train_Id = train_data.loc[:,'Id']
test_Id = test_data.loc[:,'Id']
test_data = test_data.drop('Id',axis = 'columns')
 train_data = train_data.drop('Id',axis = 'columns')
 train_data
display_all(df_raw.isnull().sum() /len(df_raw)) 
<compute_test_metric>
def adding_new_features(df):
df.loc[:,'mean_rooms'] = df.loc[:,'total_rooms']/df.loc[:,'households']
df.loc[:,'rooms_per_person'] = df.loc[:,'total_rooms']/df.loc[:,'population']
df.loc[:,'mean_bedrooms'] = df.loc[:,'total_bedrooms']/df.loc[:,'households']
df.loc[:,'bedrooms_per_person'] = df.loc[:,'total_bedrooms']/df.loc[:,'households']
df.loc[:,'persons_per_household'] = df.loc[:,'population']/df.loc[:,'households']
df.loc[:, 'median_income_per_person'] = df.loc[:,'median_income']/df.loc[:,'persons_per_household']
adding_new_features(train_data)
 adding_new_features(test_data)
 train_data
df, y, nas = proc_df(df_raw, 'y' )<train_model>
dist2coast = pd.read_csv(".. /input/distance-to-coast/dist2coast.txt",delim_whitespace = True)
m = RandomForestRegressor(n_jobs=-1)
 m.fit(df,y )<compute_test_metric>
dist2coast = pd.DataFrame(dist2coast)
 dist2coast = dist2coast.rename({'-179.98': 'longitude', '89.98': 'latitude', '712.935': 'dist2coast'}, axis='columns')
 dist2coast = dist2coast.query('-114.00 > longitude > -126.00')
 dist2coast = dist2coast.query('28.00 < latitude < 42.00')
 dist2coast
m.score(df,y )<create_dataframe>
def saving(name,y_predict):
df = pd.DataFrame()
df['Id'] = test_Id
df.set_index('Id', inplace=True)
 df['median_house_value'] =y_predict
print(df)
 return df.to_csv(name )
apply_cats(test, df_raw)
 sub = pd.DataFrame()
sub['ID'] = test.ID 
<categorify>
x_train_data = train_data.drop('median_house_value', axis = 'columns')
 y_train_data = train_data.loc[:,'median_house_value']
reg1 = tree.DecisionTreeRegressor(max_depth = 1)
 reg1 = reg1.fit(x_train_data, y_train_data)
 DTR_y = reg1.predict(test_data)
 saving("DecisionTreeRegression_1.csv",DTR_y)
test_df, _, _ = proc_df(test, na_dict=nas)
 df, _, _ = proc_df(df, na_dict=nas )<split>
Scores1 = []
for i in range(1,10):
reg1 = tree.DecisionTreeRegressor(max_depth = i)
 scores1 = cross_val_score(reg1, x_train_data, y_train_data, cv=10)
 Scores1.append(scores1.mean())
 Scores1_df = pd.DataFrame()
Scores1_df['Regression Tree Scoring']= Scores1
Scores1_df
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>
reg2 = KNeighborsRegressor(n_neighbors=50)
 reg2 = reg2.fit(x_train_data, y_train_data)
 knnR_y = reg2.predict(test_data)
 saving("50nn Regressor.csv", knnR_y )
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>
reg3 = linear_model.LassoLars(alpha=.1, positive = True)
 reg3.fit(x_train_data, y_train_data)
 print(reg3.coef_)
 LASSO_y = reg3.predict(test_data)
 saving("LASSO LARS.csv", LASSO_y)
sub['y'] = m.predict(test_df)
 sub.to_csv('rf_models_fastai300all.csv', index=False )<load_from_csv>
reg4 = MLPRegressor()
reg4.fit(x_train_data, y_train_data)
 MLP_y = reg4.predict(test_data)
 saving("MultiLayerPerceptrons Regressor.csv", MLP_y )
df= pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv' )<sort_values>
reg5 = linear_model.BayesianRidge()
reg5.fit(x_train_data, y_train_data)
 BRR_y = reg5.predict(test_data)
 scores5 = cross_val_score(reg5, x_train_data, y_train_data, cv = 10)
 saving("Bayesian Ridge Regressor.csv", BRR_y )
total = df.isnull().sum().sort_values(ascending=False)
 percent =(df.isnull().sum() /df.isnull().count() ).sort_values(ascending=False)
 missing = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing.head(20 )<drop_column>
reg6 = RandomForestRegressor()
scores6 = cross_val_score(reg6, x_train_data, y_train_data, cv=10)
 scores6 = scores6.mean()
print(scores6)
 reg6.fit(x_train_data, y_train_data)
 RF_y = reg6.predict(test_data)
 saving("RandomForestsRegressor20.csv", RF_y)
 print(scores6 )
df.drop(['Alley'],axis=1,inplace=True)
 df.drop(['PoolQC'],axis=1,inplace=True)
 df.drop(['MiscFeature'],axis=1,inplace=True)
 df.drop(['Fence'],axis=1,inplace=True)
 df.drop(['FireplaceQu'],axis=1,inplace=True)
 df['LotFrontage']=df['LotFrontage'].fillna(df['LotFrontage'].mean())
 df['GarageCond']=df['GarageCond'].fillna(df['GarageCond'].mode() [0])
 df['GarageType']=df['GarageType'].fillna(df['GarageType'].mode() [0])
 df['GarageYrBlt']=df['GarageYrBlt'].fillna(df['GarageYrBlt'].mean())
 df['GarageFinish']=df['GarageFinish'].fillna(df['GarageFinish'].mode() [0])
 df['GarageQual']=df['GarageQual'].fillna(df['GarageQual'].mode() [0])
 df['BsmtExposure']=df['BsmtExposure'].fillna(df['BsmtExposure'].mode() [0])
 df['BsmtFinType2']=df['BsmtFinType2'].fillna(df['BsmtFinType2'].mode() [0])
 df['BsmtFinType1']=df['BsmtFinType1'].fillna(df['BsmtFinType1'].mode() [0])
 df['BsmtCond']=df['BsmtCond'].fillna(df['BsmtCond'].mode() [0])
 df['BsmtQual']=df['BsmtQual'].fillna(df['BsmtQual'].mode() [0])
 df['MasVnrArea']=df['MasVnrArea'].fillna(df['MasVnrArea'].mean())
 df['MasVnrType']=df['MasVnrType'].fillna(df['MasVnrType'].mode() [0])
 df['Electrical']=df['Electrical'].fillna(df['Electrical'].mode() [0] )<categorify>
reg7 = ExtraTreesRegressor()
reg7.fit(x_train_data, y_train_data)
 ET_y = reg7.predict(test_data)
 saving("ExtraTreesRegressor.csv", ET_y)
lencoders = {}
for col in df.select_dtypes(include=['object'] ).columns:
lencoders[col] = LabelEncoder()
df[col] = lencoders[col].fit_transform(df[col] )<train_model>
reg8 = AdaBoostRegressor(base_estimator = RandomForestRegressor(max_depth=20), n_estimators=50)
 reg8.fit(x_train_data, y_train_data)
 ADA_y = reg8.predict(test_data)
 saving("ADA Boost Regression.csv",ADA_y )
x_t = df.drop('SalePrice', axis=1)
 y_t = df['SalePrice']
clf_1 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='log2', max_depth = 4))
clf_2 = SelectFromModel(RandomForestClassifier(n_estimators=100, max_features='auto', max_depth = 4))
clf_1.fit(x_t, y_t)
 clf_2.fit(x_t, y_t)
 sel_feat_1 = x_t.columns[(clf_1.get_support())]
sel_feat_2 = x_t.columns[(clf_2.get_support())]
print(sel_feat_1)
 print(sel_feat_2 )<train_model>
dist2coast_reg = KNeighborsRegressor(n_neighbors=1)
 scores_dist = cross_val_score(dist2coast_reg,dist2coast.loc[:,:'latitude'],dist2coast.loc[:,'dist2coast':],cv=10)
 scores_dist = scores_dist.mean()
dist2coast_reg.fit(dist2coast.loc[:,:'latitude'],dist2coast.loc[:,'dist2coast':])
 train_pred_dist= dist2coast_reg.predict(train_data.loc[:,:'latitude'])
 test_pred_dist= dist2coast_reg.predict(test_data.loc[:,:'latitude'])
 train_data['dist2coast'] = train_pred_dist
test_data['dist2coast']= test_pred_dist
print(scores_dist)
 x_train_data = train_data.drop('median_house_value', axis = 'columns')
 y_train_data = train_data.loc[:,'median_house_value']
train_data
<train_model><EOS> 
reg_F = AdaBoostRegressor(base_estimator = RandomForestRegressor(max_depth=20), n_estimators=50)
 reg_F.fit(x_train_data, y_train_data)
 final = reg_F.predict(test_data)
 saving("Final ADA Boost Regression.csv",final )