Kaggle competition: House Prices - Advanced Regression Techniques
pclass_dum = pd.get_dummies(df["Pclass"])
 test_pclass_dum = pd.get_dummies(test_df["Pclass"])
 df = pd.concat([df, pclass_dum], axis = 1)
 test_df = pd.concat([test_df, test_pclass_dum], axis = 1)
 df.rename({1:"pclass1", 2:"pclass2", 3:"pclass3"}, axis = 1, inplace = True)
 test_df.rename({1:"pclass1", 2:"pclass2", 3:"pclass3"}, axis = 1, inplace = True)
 df.drop(["Pclass"], axis = 1, inplace = True)
 test_df.drop(["Pclass"], axis = 1, inplace = True )<categorify>
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 train.columns
df["Sex"].replace("male", 0, inplace = True)
 test_df["Sex"].replace("male", 0, inplace = True)
 df["Sex"].replace("female", 1, inplace = True)
 test_df["Sex"].replace("female", 1, inplace = True)
 df["Embarked"].fillna("S", inplace = True)
 test_df["Embarked"].fillna("S", inplace = True)
 pclass_dum = pd.get_dummies(df["Embarked"])
 test_pclass_dum = pd.get_dummies(test_df["Embarked"])
 df = pd.concat([df, pclass_dum], axis = 1)
 test_df = pd.concat([test_df, test_pclass_dum], axis = 1)
 df.rename({"S":"embarked_s", "C":"embarked_c", "Q":"embarked_q"}, axis = 1, inplace = True)
 test_df.rename({"S":"embarked_s", "C":"embarked_c", "Q":"embarked_q"}, axis = 1, inplace = True)
 df.drop(["Embarked"], axis = 1, inplace = True)
 test_df.drop(["Embarked"], axis = 1, inplace = True )<feature_engineering>
train_x = train.drop("SalePrice",axis=1)
 train_y = train["SalePrice"]
all_data = pd.concat([train_x,test],axis=0,sort=True)
 train_ID = train['Id']
test_ID = test['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: "+str(test.shape))
print("all_data: "+str(all_data.shape))
def create_family_ranges(df):
familysize = []
for members in df["n_fam_mem"]:
if members == 1:
familysize.append(1)
 elif members == 2:
familysize.append(2)
 elif members>2 and members<=4:
familysize.append(3)
 elif members > 4:
familysize.append(4)
 return familysize
famsize = create_family_ranges(df)
 df["familysize"] = famsize
test_famsize = create_family_ranges(test_df)
 test_df["familysize"] = test_famsize<categorify>
all_data_na = all_data.isnull().sum() [all_data.isnull().sum() >0].sort_values(ascending=False)
 all_data_na
fsizedummies = pd.get_dummies(df["familysize"])
 test_fsizedummies = pd.get_dummies(test_df["familysize"])
 df = pd.concat([df, fsizedummies], axis = 1)
 test_df = pd.concat([test_df, test_fsizedummies], axis = 1)
 df.rename({1:"fam_single",2:"fam_small",3:"fam_medium", 4:"fam_big"}, axis = 1, inplace = True)
 test_df.rename({1:"fam_single",2:"fam_small",3:"fam_medium", 4:"fam_big"}, axis = 1, inplace = True )<prepare_x_and_y>
na_col_list = all_data.isnull().sum() [all_data.isnull().sum() >0].index.tolist()
all_data[na_col_list].dtypes.sort_values()
reg_df = df.drop(["Survived", "Name", "Cabin"], axis = 1)
 reg_df_test = test_df.drop(["Name", "Cabin"], axis = 1)
 age_reg_df = reg_df[reg_df["Age"].isna() == False]
age_reg_df_test = reg_df_test[reg_df_test["Age"].isna() == False]
new_age_df = age_reg_df.append(age_reg_df_test)
 new_age_X = new_age_df.drop(["Age"], axis = 1)
 new_age_y = new_age_df["Age"]
new_age_X["Fare"].fillna(df["Fare"].median() , inplace = True)
 linear_reg_model = LinearRegression().fit(new_age_X, new_age_y )<categorify>
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]
def get_age_indexes_to_replace(df):
age_temp_list = df["Age"].values.tolist()
indexes_age_replace = []
age_temp_list = [str(x)for x in age_temp_list]
for i, item in enumerate(age_temp_list):
if item == "nan":
indexes_age_replace.append(i)
 return indexes_age_replace
indexes_to_replace_main = get_age_indexes_to_replace(df)
 indexes_to_replace_test = get_age_indexes_to_replace(test_df)
 def linear_age_predictions(reg_df, indexes_age_replace):
reg_df_temp = reg_df.drop(["Age"], axis = 1)
 age_predictions = []
for i in indexes_age_replace:
x = reg_df_temp.iloc[i]
x = np.array(x ).reshape(1,-1)
 pred = linear_reg_model.predict(x)
 age_predictions.append(pred)
 return age_predictions
age_predictions_main = linear_age_predictions(reg_df, indexes_to_replace_main)
 age_predictions_test = linear_age_predictions(reg_df_test, indexes_to_replace_test)
 def fill_age_nan(df, indexes_age_replace, age_predictions):
for i, item in enumerate(indexes_age_replace):
df["Age"][item] = age_predictions[i]
return df
df = fill_age_nan(df, indexes_to_replace_main, age_predictions_main)
 df_test = fill_age_nan(test_df, indexes_to_replace_test, age_predictions_test )<categorify>
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )
def age_to_int(df):
agelist = df["Age"].values.tolist()
for i in range(len(agelist)) :
if agelist[i] < 14:
agelist[i] = 0
elif agelist[i] >= 14 and agelist[i] < 25:
agelist[i] = 1
elif agelist[i]>=25 and agelist[i]<60:
agelist[i] = 2
elif agelist[i]>=60:
agelist[i] = 3
ageint = pd.DataFrame(agelist)
 return ageint<data_type_conversions>
skewed_feats_over = skewed_feats[abs(skewed_feats)> 0.5].index
for i in skewed_feats_over:
print(min(all_data[i]))
ageint = age_to_int(df)
 df["Ageint"] = ageint
df.drop("Age", axis = 1, inplace = True)
 test_ageint = age_to_int(test_df)
 test_df["Ageint"] = test_ageint
test_df.drop("Age", axis = 1, inplace = True )<categorify>
all_data["FeetPerRoom"] = all_data["TotalSF"]/all_data["TotRmsAbvGrd"]
all_data['YearBuiltAndRemod']=all_data['YearBuilt']+all_data['YearRemodAdd']
all_data['Total_Bathrooms'] =(all_data['FullBath'] +(0.5 * all_data['HalfBath'])+
all_data['BsmtFullBath'] +(0.5 * all_data['BsmtHalfBath']))
all_data['Total_porch_sf'] =(all_data['OpenPorchSF'] + all_data['3SsnPorch'] +
all_data['EnclosedPorch'] + all_data['ScreenPorch'] +
all_data['WoodDeckSF'])
 all_data['haspool'] = all_data['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 all_data['has2ndfloor'] = all_data['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 all_data['hasgarage'] = all_data['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 all_data['hasbsmt'] = all_data['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 all_data['hasfireplace'] = all_data['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )
def conv_fare_ranges(df):
fare_ranges = []
for fare in df["actual_fare"]:
if fare < 7:
fare_ranges.append(0)
 elif fare >=7 and fare < 14:
fare_ranges.append(1)
 elif fare >=14 and fare < 30:
fare_ranges.append(2)
 elif fare >=30 and fare < 50:
fare_ranges.append(3)
 elif fare >=50:
fare_ranges.append(4)
 return fare_ranges
fare_ranges = conv_fare_ranges(df)
 df["fare_ranges"] = fare_ranges
test_fare_ranges = conv_fare_ranges(test_df)
 test_df["fare_ranges"] = test_fare_ranges<categorify>
all_data.dtypes.value_counts()
df["Cabin"].fillna("unknown", inplace = True)
 test_df["Cabin"].fillna("unknown", inplace = True )<drop_column>
all_data = pd.get_dummies(all_data,columns=cal_list)
 all_data.shape
cabins = [i[0] if i!= 'unknown' else 'unknown' for i in df['Cabin']]
test_cabins = [i[0] if i!= 'unknown' else 'unknown' for i in test_df['Cabin']]
df.drop(["Cabin"], axis = 1, inplace = True)
 test_df.drop(["Cabin"], axis = 1, inplace = True)
 df["cabintype"] = cabins
test_df["cabintype"] = test_cabins<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(["cabintype"], axis = 1, inplace = True)
 test_df.drop(["cabintype"], axis = 1, inplace = True )<categorify>
train_x, valid_x, train_y, valid_y = train_test_split(
 train_x,
train_y,
test_size=0.3,
random_state=0)
def name_to_int(df):
name = df["Name"].values.tolist()
namelist = []
for i in name:
index = 1
inew = i.split()
if inew[0].endswith(","):
index = 1
elif inew[1].endswith(","):
index = 2
elif inew[2].endswith(","):
index = 3
namelist.append(inew[index])
 titlelist = []
for i in range(len(namelist)) :
titlelist.append(namelist[i])
 return titlelist<count_values>
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 )
titlelist = name_to_int(df)
 df["titles"] = titlelist
df["titles"].value_counts()
testtitlelist = name_to_int(test_df)
 test_df["titles"] = testtitlelist
df["titles"].value_counts()<categorify>
dtest = xgb.DMatrix(test)
 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 )