Kaggle competition: House Prices - Advanced Regression Techniques
test_data.isna().sum()<count_missing_values>
train= pd.read_csv(".. /input/train.csv")
 test =pd.read_csv(".. /input/test.csv" )
odata = odata.drop(['PassengerId','Ticket','Cabin'],axis=1)
 odata.isna().sum()<create_dataframe>
print("Train : "+str(train.shape))
idUn = len(set(train.Id))
idTo = train.shape[0]
idDup = idTo - idUn
print(str(idDup)+" duplicates available in this dataset" )
data = odata.copy()<import_modules>
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index)
import re<count_missing_values>
train_n = train.shape[0]
test_n = test.shape[0]
y_train = train.SalePrice.values
y_test = train['SalePrice']
all_data = pd.concat(( train,test),sort=False ).reset_index(drop = True)
 all_data.drop(['SalePrice'], axis=1, inplace = True)
 print("all_data size is : {}".format(all_data.shape))
data.Name.str.extract('(\w{1,8})\.' ).isna().sum()<feature_engineering>
all_data.isnull().sum().sort_values(ascending=False)
data['suffix'] = data.Name.str.extract('(\w{1,8})\.')
 data.drop('Name', axis=1, inplace=True)
 data.head()<feature_engineering>
all_data_na_values = all_data.isnull().sum()
all_data_na_values = all_data_na_values.drop(all_data_na_values[all_data_na_values == 0].index ).sort_values(ascending=False)[:30]
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na,'Missing Values' :all_data_na_values})
 missing_data.head(20 )
data.suffix = data.suffix.apply(lambda x: x if(x in ['Mr', 'Miss', 'Mrs', 'Master'])else 'rare' )<count_missing_values>
all_data['PoolQC'] = all_data['PoolQC'].fillna("None")
 all_data['MiscFeature'] = all_data['MiscFeature'].fillna("None")
 all_data["Alley"] = all_data["Alley"].fillna("None")
 all_data["Fence"] = all_data["Fence"].fillna("None")
 all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None")
 all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(lambda x: x.fillna(x.median()))
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_data[col] = all_data[col].fillna('None')
 for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0)
 for col in('BsmtCond', 'BsmtExposure', 'BsmtQual', 'BsmtFinType2', 'BsmtFinType1'):
all_data[col] = all_data[col].fillna('None')
 for col in('BsmtHalfBath', 'BsmtFullBath', 'TotalBsmtSF', 'BsmtUnfSF', 'BsmtFinSF2', 'BsmtFinSF1'):
all_data[col] = all_data[col].fillna(0)
 all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0)
 all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0])
 all_data = all_data.drop(['Utilities'], axis=1)
 all_data["Functional"] = all_data["Functional"].fillna("Typ")
 all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0])
 all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0])
 all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0])
 all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0])
data.isna().sum()<import_modules>
all_data_na_values = all_data.isnull().sum()
all_data_na_values = all_data_na_values.drop(all_data_na_values[all_data_na_values == 0].index ).sort_values(ascending=False)[:30]
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na,'Missing Values' :all_data_na_values,'Data_type':all_data_na.dtype})
 missing_data.head()
import math<sort_values>
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )
print(sorted(set(data.Fare.apply(math.ceil))))<feature_engineering>
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
print('Shape all_data: {}'.format(all_data.shape))
data['grouped_fare'] = data.Fare.apply(faregrp )<filter>
all_data['TotalSF'] = all_data['TotalBsmtSF']+all_data['1stFlrSF']+all_data['2ndFlrSF']
narows = data[data.Age.isna() ]
narows<predict_on_test>
all_data = pd.get_dummies(all_data)
 print(all_data.shape )
tempdata = data.dropna()
x = tempdata[['Parch', 'SibSp', 'Pclass']]
y = tempdata.Age
modelR(x,y)
 agepred = rfr()
agepred.fit(x, y)
 predicted = agepred.predict(narows[['Parch', 'SibSp', 'Pclass']])
 data.Age.fillna(pd.Series(predicted, index=narows.index), inplace=True )<categorify>
regr = xgb.XGBRegressor(colsample_bytree=0.2,
gamma=0.0,
learning_rate=0.1,
max_depth=4,
min_child_weight=1.5,
n_estimators=7200,
reg_alpha=0.9,
reg_lambda=0.6,
subsample=0.2,
seed=42,
silent=1)
 regr.fit(train_new,y_train)
 y_pred = regr.predict(train_new)
 y_test = train['SalePrice']
print("XGB Score :",(np.sqrt(mean_squared_error(y_test, y_pred))))
y_pred_xgb = regr.predict(test_new)
 y_pred_xgb = np.exp(y_pred_xgb)
 pred_df = pd.DataFrame(y_pred_xgb, index=test_ID, columns=["SalePrice"])
 pred_df.to_csv('submission1.csv', header=True, index_label='Id' )
def agegrp(age):
if 0<= age <=10:
return 1
if 10< age <=18:
return 2
if 18< age <=25:
return 3
if 25< age <=35:
return 4
if 35< age <=47:
return 5
if 47< age <=60:
return 6
if 60< age <=70:
return 7
if 70< age <=80:
return 8<feature_engineering>
model_lasso =Lasso(alpha=0.0005,normalize=True, max_iter=1e5)
 model_lasso.fit(train_new,y_train)
 y_pred_lasso = model_lasso.predict(train_new)
 score_lasso = np.sqrt(mean_squared_error(y_train, y_pred_lasso))
print("Lasso Score :",score_lasso)
 y_pred_lasso_test = model_lasso.predict(test_new)
 y_pred_lasso_test = np.exp(y_pred_lasso_test )
data['age_grp'] = data.Age.apply(agegrp )<count_values>
model_rd = Ridge(alpha = 4.84)
 model_rd.fit(train_new,y_train)
 y_pred_rd = model_rd.predict(train_new)
 score_rd = np.sqrt(mean_squared_error(y_train, y_pred_rd))
print("Ridge Score :",score_rd)
 y_pred_rd_test = model_rd.predict(test_new)
 y_pred_rd_test = np.exp(y_pred_rd_test )
data.Age[data.Age<=18].count()<filter>
model_enet = ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3)
 model_enet.fit(train_new,y_train)
 y_pred_enet = model_enet.predict(train_new)
 score_enet = np.sqrt(mean_squared_error(y_train, y_pred_enet))
print("ElasticNet Score :",score_enet)
 y_pred_enet_test = model_enet.predict(test_new)
 y_pred_enet_test = np.exp(y_pred_enet_test )
data.Age[data.Survived==1][data.Age<=18].count()<correct_missing_values>
model_rf = RandomForestRegressor(n_estimators = 12,max_depth = 3,n_jobs = -1)
 model_rf.fit(train_new,y_train)
 y_pred_rf = model_rf.predict(train_new)
 score_rf = np.sqrt(mean_squared_error(y_train, y_pred_rf))
print("RandomForest Score :",score_rf)
 y_pred_rf_test = model_rf.predict(test_new)
 y_pred_rf_test = np.exp(y_pred_rf_test )
data.Embarked.fillna(data.Embarked.mode() [0], inplace=True )<set_options>
model_gb = GradientBoostingRegressor(n_estimators = 40,max_depth = 2)
 model_gb.fit(train_new,y_train)
 y_pred_gb = model_gb.predict(train_new)
 score_gb = np.sqrt(mean_squared_error(y_train, y_pred_gb))
print("GradientBoosting Score :",score_gb)
 y_pred_gb_test = model_gb.predict(test_new)
 y_pred_gb_test = np.exp(y_pred_gb_test )
<feature_engineering><EOS> 
dt = DecisionTreeRegressor(max_depth=4)
 model_ad = AdaBoostRegressor(dt, learning_rate = 0.1, n_estimators=300,random_state= None)
 model_ad.fit(train_new,y_train)
 y_pred_ad = model_ad.predict(train_new)
 score_ad = np.sqrt(mean_squared_error(y_train, y_pred_ad))
print("AdaBoost Model :",score_ad)
 y_pred_ad_test = model_ad.predict(test_new)
 y_pred_ad_test = np.exp(y_pred_ad_test )