Kaggle competition: House Prices - Advanced Regression Techniques
outlier_features = ["GrLivArea","TotalSF","GarageArea"]
means,sds = np.mean(dataset[outlier_features]),np.std(dataset[outlier_features])
 lower,upper = means - 2.5*sds , means + 2.5*sds
def compare(x):
count=0
for col in outlier_features:
if x[col]>lower[col] and x[col]<upper[col]:
count=count+1
if count==len(outlier_features):
return True
else:
return False
y= y.array
sub_data = dataset.loc[:index-1]
test_1 = dataset.loc[index:,:]
data_train_new = sub_data.loc[sub_data.apply(lambda x: compare(x),axis=1)]
y= y[sub_data.apply(lambda x: compare(x),axis=1)]
index = len(data_train_new)
 dataset = pd.concat([data_train_new,test_1] ).reset_index(drop=True )<feature_engineering>
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.preprocessing import PowerTransformer
numeric_feats = dataset.dtypes[(dataset.dtypes != "object")&(dataset.dtypes != "category")].index
skewed_feats = dataset[numeric_feats].apply(lambda x: skew(x)).sort_values(ascending=False)
 skewness = pd.DataFrame({'Skew' :skewed_feats})
 skewness = skewness[abs(skewness)> 0.5]
skewed_features = skewness.index
dataset[skewed_features] = np.log1p(dataset[skewed_features] )<drop_column>
train = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test_x = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 train.columns
dataset.drop("Id",axis=1,inplace=True )<categorify>
train_x = train.drop("SalePrice",axis=1)
 train_y = train["SalePrice"]
all_data = pd.concat([train_x,test_x],axis=0,sort=True)
 train_ID = train['Id']
test_ID = test_x['Id']
all_data.drop("Id", axis = 1, inplace = True)
 print("train_x: "+str(train_x.shape))
print("train_y: "+str(train_y.shape))
print("test_x: "+str(test_x.shape))
print("all_data: "+str(all_data.shape))
dataset = pd.get_dummies(dataset, drop_first= True)
 train_1 = dataset.loc[:index-1,:]
test_1 = dataset.loc[index:,:]<split>
all_data_na = all_data.isnull().sum() [all_data.isnull().sum() >0].sort_values(ascending=False)
 all_data_na
X_train,X_test,y_train,y_test = train_test_split(train_1,y,test_size=0.2, random_state=42 )<normalization>
na_col_list = all_data.isnull().sum() [all_data.isnull().sum() >0].index.tolist()
all_data[na_col_list].dtypes.sort_values()
rs = RobustScaler()
X_train= rs.fit_transform(X_train)
 X_test = rs.transform(X_test)
 X_submit = rs.transform(test_1.values )<choose_model_class>
all_data['LotFrontage'] = all_data.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
float_list = all_data[na_col_list].dtypes[all_data[na_col_list].dtypes == "float64"].index.tolist()
obj_list = all_data[na_col_list].dtypes[all_data[na_col_list].dtypes == "object"].index.tolist()
all_data[float_list] = all_data[float_list].fillna(0)
 all_data[obj_list] = all_data[obj_list].fillna("None")
 all_data.isnull().sum() [all_data.isnull().sum() > 0]
kf = KFold(n_splits=10,shuffle=True,random_state=42)
 rf = RandomForestRegressor(n_estimators=500)
 xgb = XGBRegressor()
gb= GradientBoostingRegressor()
svr =SVR(C=70,epsilon=.115)
 ridge = Ridge(alpha=1.75,solver ="lsqr")
 lasso = Lasso(alpha=.0009)
 clf=[\ 
("XGBoost",xgb,{"n_estimators":[200],"max_depth":[3],"learning_rate":[.1],"subsample":[1],"colsample_bytree":[1],"gamma":[0],"lambda":[.001,.01]})\
,("Gradient Boost",gb,{})\
,("SVR",svr,{"C":[60,65,70,75,80],"epsilon":[.11,.115,.12]})\
,("Ridge",ridge,{"alpha":[1.4,1.5,1.6,1.75,1.8]})\
,("Lasso",lasso,{"alpha":[.0005,.0006,.0007,.001,.01,.1]})]
est=[]
results_data=pd.DataFrame(columns=["Name","Train_Score","Test_Score"])
 i=0
for name,reg,param_grid in clf:
gs= GridSearchCV(reg,param_grid=param_grid,cv=kf,scoring="neg_mean_squared_error")
 gs.fit(X_train,y_train)
 y_pred = gs.best_estimator_.predict(X_test)
 test_score = np.sqrt(mean_squared_error(y_test, y_pred))
results_data.loc[i,]= [name,-gs.best_score_,test_score]
i=i+1
est.append([name,gs.best_estimator_])
 sc = StackingRegressor(estimators=est[1:],final_estimator= ridge,cv=kf)
 sc.fit(X_train,y_train)
 y_trn= sc.predict(X_train)
 y_tst= sc.predict(X_test)
 train_score = np.sqrt(mean_squared_error(y_train, y_trn))
test_score = np.sqrt(mean_squared_error(y_test, y_tst))
results_data.loc[i,]= ["Stack",train_score,test_score]<predict_on_test>
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )
y_submit= np.expm1(sc.predict(X_submit))<create_dataframe>
skewed_feats_over = skewed_feats[abs(skewed_feats)> 0.5].index
for i in skewed_feats_over:
print(min(all_data[i]))
data_submit= pd.DataFrame({"Id":test.Id,"SalePrice":y_submit} )<save_to_csv>
data_submit.to_csv("submit_housing.csv",index=False )<define_variables>
all_data.dtypes.value_counts()
train_dir = ".. /input/train.csv"
test_dir = ".. /input/test.csv"<load_from_csv>
all_data = pd.get_dummies(all_data,columns=cal_list)
 all_data.shape
df = pd.read_csv(train_dir)
 test_df = pd.read_csv(test_dir )<drop_column>
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
from sklearn.model_selection import GridSearchCV
import lightgbm as lgb
df.drop(["Ticket"], axis = 1, inplace = True)
 test_df.drop(["Ticket"], axis = 1, inplace = True)
 df.info()
df.head()<count_values>
train_x, valid_x, train_y, valid_y = train_test_split(
 train_x,
train_y,
test_size=0.3,
random_state=0 )
print(df["Survived"].value_counts())
 df["Survived"].value_counts().plot(kind = "pie" )<drop_column>
dtrain = xgb.DMatrix(train_x, label=train_y)
 dvalid = xgb.DMatrix(valid_x,label=valid_y)
 num_round = 5000
evallist = [(dvalid, 'eval'),(dtrain, 'train')]
evals_result = {}
param = {
'max_depth': 3,
'eta': 0.01,
'objective': 'reg:squarederror',
}
bst = xgb.train(
 param, dtrain,
num_round,
evallist,
evals_result=evals_result,
early_stopping_rounds = 1000 )
df.drop("PassengerId", axis = 1, inplace = True)
 test_df.drop("PassengerId", axis = 1, inplace = True )<categorify>
dtest = xgb.DMatrix(test_x)
 my_submission = pd.DataFrame()
my_submission["Id"] = test_ID
my_submission["SalePrice"] = np.exp(bst.predict(dtest))
my_submission.to_csv('submission.csv', index=False )