Kaggle competition: Who is a Friend?
<SOS> metric: categorizationaccuracy Kaggle data source: who-is-a-friend?<load_from_csv>
%matplotlib inline
plt.rcParams['figure.figsize'] = [9, 12]
warnings.simplefilter('ignore' )
def import_data(data):
df = pd.read_csv('.. /input/{}.csv'.format(data))
df = df.set_index('Id')
 return df
train = import_data('train')
 test = import_data('test' )<train_model>
train = pd.read_csv("/kaggle/input/whoisafriend/train.csv")
 test = pd.read_csv("/kaggle/input/whoisafriend/test.csv")
 sub = pd.read_csv("/kaggle/input/whoisafriend/sample_submission.csv")
 train.shape, test.shape, sub.shape
print('Train dataset:
Rows: {}
Columns: {}'.format(train.shape[0], train.shape[1]))
train.head()<drop_column>
agg_train = train.groupby(['Person A', 'Person B'])['Years of Knowing'].count().reset_index()
agg_train.rename({
"Years of Knowing": "Interaction Count"
}, axis=1, inplace=True)
 agg_test = test.groupby(['Person A', 'Person B'])['Years of Knowing'].count().reset_index()
agg_test.rename({
"Years of Knowing": "Interaction Count"
}, axis=1, inplace=True )
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<450000)].index )<define_variables>
train = pd.merge(train, agg_train, on=['Person A', 'Person B'], how='left')
 test = pd.merge(test, agg_test, on=['Person A', 'Person B'], how='left' )
commonNa = [
'MSZoning','Electrical','KitchenQual',
'Exterior1st','Exterior2nd','SaleType',
'LotFrontage','Functional'
]
toZero = [
'MasVnrArea','GarageYrBlt','BsmtHalfBath',
'BsmtFullBath','GarageArea','GarageCars',
'BsmtFinSF1','BsmtFinSF2','BsmtUnfSF',
'TotalBsmtSF'
]
toNoData = [
'PoolQC','MiscFeature','Alley',
'Fence','FireplaceQu','GarageType',
'GarageFinish','GarageQual',
'GarageCond','BsmtQual','BsmtCond',
'BsmtExposure','BsmtFinType1','BsmtFinType2',
'MasVnrType'
]
def fillNa_fe(df):
df['Functional'] = df['Functional'].fillna('Typ')
 for i in commonNa:
df[i] = df[i].fillna(df[i].mode() [0])
 for i in toNoData:
df[i] = df[i].fillna('None')
 for i in toZero:
df[i] = df[i].fillna(0)
 df.drop(
 ['Utilities'],
axis=1,
inplace=True 
)
 df['totalSF'] = df['TotalBsmtSF'] \
+ df['1stFlrSF'] \
+ df['2ndFlrSF'] \
+ df['GrLivArea']
return df
train, test = fillNa_fe(train), fillNa_fe(test )<count_missing_values>
test['Friends'] = np.nan
test['Friends'] = test['Interaction Count'].apply(lambda x: 1 if x > 7 else 0 )
for df in [train,test]:
print(df[[col for col in df.columns if df[col].isna().sum() > 0]].isna().sum().sort_values(ascending = False ).any() )<feature_engineering>
test[['ID', 'Friends']].to_csv("1.0_sub.csv", index=False )