Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<count_values>
warnings.filterwarnings('ignore')
train_data['Size'].value_counts()<count_values>
train_df = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test_df = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )
test_data['Size'].value_counts()<data_type_conversions>
test_df['SalePrice'] = np.nan
test_df.head()
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True )<count_values>
train_df = train_df.drop(['Id'], axis = 1)
 test_df = test_df.drop(['Id'], axis = 1)
 target = train_df['SalePrice']
all_data = pd.concat([train_df, test_df], ignore_index = True)
 all_data.head()
train_data['TopThreeAmericanName'].value_counts()<count_values>
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
 mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
 mis_val_table_ren_columns = mis_val_table.rename(
 columns = {0 : 'Missing Values', 1 : '% of Total Values'})
 mis_val_table_ren_columns = mis_val_table_ren_columns[
mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(
 '% of Total Values', ascending=False ).round(1)
 print("Your selected dataframe has " + str(df.shape[1])+ " columns.
 "
"There are " + str(mis_val_table_ren_columns.shape[0])+
" columns that have missing values.")
 return mis_val_table_ren_columns
test_data['TopThreeAmericanName'].value_counts()<data_type_conversions>
missing_values = missing_values_table(all_data)
 missing_values
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )<count_values>
all_data.dtypes.value_counts()
train_data['PRIMEUNIT'].value_counts()<count_values>
all_data.select_dtypes('object' ).apply(pd.Series.nunique, axis = 0 )
test_data['PRIMEUNIT'].value_counts()<data_type_conversions>
all_data['PoolQC'] = all_data['PoolQC'].fillna('NA')
 print(all_data['PoolQC'].isnull().sum())
 all_data.head()
train_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True )<categorify>
all_data.loc[(all_data['PoolQC'] == 'NA')&(all_data['PoolArea'] != 0)]
train_data['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_data['AUCGUART'].replace("ARED","RED",inplace=True )<data_type_conversions>
all_data.at[2420, 'PoolQC'] = 'Fa'
all_data.at[2599, 'PoolQC'] = 'Fa'
all_data.at[2503, 'PoolQC'] = 'Fa'
train_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 test_data['AUCGUART'].fillna(value="AUC_unk",inplace=True )<drop_column>
maps = {
'Ex': 5,
'Gd': 4,
'TA': 3,
'Fa': 2,
'NA': 1
}
all_data['PoolQC'] = all_data['PoolQC'].replace(maps)
 all_data.head()
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1 )<drop_column>
all_data[all_data['LotFrontage'].isnull() == True].SalePrice.median()
all_data['LotFrontage'] = all_data['LotFrontage'].fillna(all_data['LotFrontage'].median())
 all_data['GarageCond'] = all_data['GarageCond'].fillna('NA' ).replace(maps)
 all_data['GarageQual'] = all_data['GarageQual'].fillna('NA' ).replace(maps)
 mapp = {
'Fin': 4,
'RFn': 3,
'Unf': 2,
'NA': 1
}
all_data['GarageFinish'] = all_data['GarageFinish'].fillna('NA' ).replace(mapp)
 all_data['GarageType'] = all_data['GarageType'].fillna('NA')
 all_data['GarageYrBlt'] = all_data['GarageYrBlt'].fillna(-1)
 all_data['GarageArea'] = all_data['GarageArea'].fillna(0.0)
 all_data['GarageCars'] = all_data['GarageCars'].fillna(0.0)
 all_data['BsmtHalfBath'] = all_data['BsmtHalfBath'].fillna(0.0)
 all_data['BsmtFullBath'] = all_data['BsmtFullBath'].fillna(0.0)
 mapp = {
'Gd':5,
'Av':4,
'Mn':3,
'No':2,
'NA':1
}
all_data['BsmtExposure'] = all_data['BsmtExposure'].fillna('NA' ).replace(mapp)
 all_data['BsmtCond'] = all_data['BsmtCond'].fillna('NA' ).replace(maps)
 all_data['BsmtQual'] = all_data['BsmtQual'].fillna('NA' ).replace(maps)
 mapp = {
'GLQ':7,
'ALQ':6,
'BLQ':5,
'Rec':4,
'LwQ':3,
'Unf':2,
'NA':1
}
all_data['BsmtFinType1'] = all_data['BsmtFinType1'].fillna('NA' ).replace(mapp)
 all_data['BsmtFinType2'] = all_data['BsmtFinType2'].fillna('NA' ).replace(mapp)
 all_data[['MasVnrType','MasVnrArea']][all_data['MasVnrType'].isnull() == True]
all_data['MasVnrType'] = all_data['MasVnrType'].fillna('None')
 all_data['MasVnrArea'] = all_data['MasVnrArea'].fillna(0.0)
 all_data.at[2610, 'MasVnrType'] = 'Stone'
all_data['BsmtFinSF1'] = all_data['BsmtFinSF1'].fillna(0.0)
 all_data['BsmtFinSF2'] = all_data['BsmtFinSF2'].fillna(0.0)
 all_data['BsmtUnfSF'] = all_data['BsmtUnfSF'].fillna(0.0)
 all_data['TotalBsmtSF'] = all_data['TotalBsmtSF'].fillna(0.0)
 all_data['SaleType'] = all_data.SaleType.fillna('Oth')
 all_data['KitchenQual'] = all_data.KitchenQual.fillna('Oth')
 for col in all_data.columns:
if(( all_data[col].isnull().sum() > 0)&(col != 'SalePrice')) :
all_data[col] = all_data[col].fillna('NA')
 all_data.head()
train_data.drop('PurchDate',axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True )<drop_column>
linearFeat = []
cor = all_data.corr() ['SalePrice'].sort_values(ascending=False)[all_data.corr() ['SalePrice'] > 0.5]
cor.head(20 )
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'<drop_column>
all_Data = all_data.copy()
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']<feature_engineering>
all_data = all_data.drop('SalePrice', axis = 1 )
for i in not_categorical:
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
maximum_test=np.max(test_data[i])
 test_data[i]=test_data[i]/maximum_test<drop_column>
scaler = StandardScaler()
numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
for i in numeric_feats:
all_data[i] = scaler.fit_transform(np.array(all_data[i] ).reshape(-1, 1))
all_data.head()
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']<filter>
le = LabelEncoder()
le_count = 0
for col in all_data:
if all_data[col].dtype == 'object':
if len(list(all_data[col].unique())) <= 2:
le.fit(all_data[col])
 all_data[col] = le.transform(all_data[col])
 le_count += 1
print('%d columns were label encoded.' % le_count )
train_data[categorical[0]]<categorify>
all_data = pd.get_dummies(all_data)
 all_data.head()
pd.get_dummies(train_data[categorical[0]] )<categorify>
model = RandomForestRegressor(random_state=1, max_depth=10)
 train = all_data[:train_df.shape[0]]
test = all_data[train_df.shape[0]:]
x_train, x_valid, y_train, y_valid = train_test_split(train, target_log, test_size=0.3, random_state = 1)
 model.fit(x_train,y_train )
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )<feature_engineering>
y_pred_val = model.predict(x_valid)
 print(math.sqrt(mean_squared_error(y_pred_val, y_valid)) )
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))<feature_engineering>
all_data = all_Data.copy()
all_data.head()
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))<drop_column>
all_data['totalBathroom'] = 0.5*all_data['HalfBath'] + all_data['FullBath'] + 0.5*all_data['BsmtHalfBath'] + all_data['FullBath']
all_data.head()
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]<prepare_x_and_y>
all_data['age'] = all_data['YrSold'] - all_data['YearRemodAdd']
ismodeled = []
deltaYr = all_data['YearRemodAdd'] - all_data['YearBuilt']
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']<split>
ismodeled = []
for i in deltaYr:
if i == 0:
ismodeled.append(1)
 else:
ismodeled.append(0)
 all_data['IsModeled'] = ismodeled
all_data.head()
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )<import_modules>
deltaYr = all_data['YrSold'] - all_data['YearBuilt']
isnew = []
for i in deltaYr:
if i == 0:
isnew.append(1)
 else:
isnew.append(0)
 all_data['IsNew'] = isnew
all_data.head()
from sklearn.neighbors import KNeighborsClassifier<import_modules>
all_data['totalSquareFeet'] = all_data['GrLivArea'] + all_data['TotalBsmtSF']
all_data.head()
from sklearn.neighbors import KNeighborsClassifier<import_modules>
numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = all_data[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )
from sklearn.neighbors import KNeighborsClassifier<train_model>
cor = all_data.corr() ['SalePrice'].sort_values(ascending=False)[all_data.corr() ['SalePrice'] > 0.5]
cor.head(20 )
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )<compute_test_metric>
all_data = all_data.drop('SalePrice', axis = 1 )
KNN.score(X_test,y_test )<predict_on_test>
scaler = StandardScaler()
numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
for i in numeric_feats:
all_data[i] = scaler.fit_transform(np.array(all_data[i] ).reshape(-1, 1))
all_data.head()
predict=KNN.predict(test_data.drop('RefId',axis=1))<prepare_output>
le = LabelEncoder()
le_count = 0
for col in all_data:
if all_data[col].dtype == 'object':
if len(list(all_data[col].unique())) <= 2:
le.fit(all_data[col])
 all_data[col] = le.transform(all_data[col])
 le_count += 1
print('%d columns were label encoded.' % le_count )
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()<prepare_output>
all_data = pd.get_dummies(all_data )
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )<save_to_csv>
kpca = KernelPCA(n_components = 80)
 all_data_pca = kpca.fit_transform(all_data)
 all_data_pca = pd.DataFrame(all_data_pca)
 all_data_pca.head()
Submission.head()
Submission.to_csv('Submission.csv' )<load_from_csv>
all_data_pca['Id'] = all_data_pca.index
all_data_pca.head()
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()<load_from_csv>
train = all_data_pca[:train_df.shape[0]]
test = all_data_pca[train_df.shape[0]:]
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()<count_missing_values>
target = pd.DataFrame({'SalePrice': target_log})
 target.shape
train_data.isnull().sum()<count_missing_values>
score = []
predict_val = pd.DataFrame(test['Id'])
 skf = KFold(n_splits = 5, shuffle=True, random_state=123)
 skf.get_n_splits(train, target_log)
 oof_rfr_df = pd.DataFrame()
predictions = pd.DataFrame(test['Id'])
 x_test = test.drop(['Id'], axis = 1)
 for fold,(trn_idx, val_idx)in enumerate(skf.split(train, target)) :
x_train, y_train = train.iloc[trn_idx], target.iloc[trn_idx]['SalePrice']
x_valid, y_valid = train.iloc[val_idx], target.iloc[val_idx]['SalePrice']
index = x_valid['Id']
yp = 0
x_train = x_train.drop(['Id'], axis = 1)
 x_valid = x_valid.drop(['Id'], axis = 1)
 rfr = RandomForestRegressor(random_state=1, max_depth=10)
 rfr.fit(x_train,y_train)
 score.append(math.sqrt(mean_squared_error(rfr.predict(x_valid), y_valid)))
 yp += rfr.predict(x_test)
 fold_pred = pd.DataFrame({'ID': index,
'label':rfr.predict(x_valid)})
 oof_rfr_df = pd.concat([oof_rfr_df, fold_pred], axis=0)
 predictions['fold{}'.format(fold+1)] = yp
test_data.isnull().sum()<count_values>
rfr_predict = pd.DataFrame()
rfr_predict['predict'] =(predictions['fold1']+predictions['fold2']+predictions['fold3']+predictions['fold4']+predictions['fold5'])/5
rfr_predict.head()
train_data['IsBadBuy'].value_counts()<count_values>
explainer = shap.TreeExplainer(rfr)
 shap_values = explainer.shap_values(x_valid )
train_data['Model'].value_counts()<drop_column>
predict = np.exp(rfr_predict['predict']-1)
 mysubmit = pd.DataFrame({'Id': test['Id']+1,
'SalePrice': predict})
 mysubmit.head()
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )<count_values>
mysubmit.to_csv('mysubmit.csv', index=False )