I have 2 classes model
and impute
. I am defining a function mode_impute
inside impute
. Now I want to call mode_impute
inside impute
. How can I call it? I tried the following:
class impute(model):
def __init__(self):
super().__init__()
pass
def mode_impute(self):
mode_val = self.df6[self.var].value_counts().index[0]
self.df6[self.var].fillna(mode_val, inplace = True)
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute(self.x, i)
The above code gives me error NameError: name 'self' is not defined
EDIT 1:
I applied the changes as suggested in the comments:
class impute(model):
def __init__(self):
super().__init__()
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute(self.x, i)
def mode_impute(self):
mode_val = self.df6[self.var].value_counts().index[0]
self.df6[self.var].fillna(mode_val, inplace = True)
m = impute()
The last line where I create an instance of the class gives me the error
AttributeError: 'impute' object has no attribute 'x'
PS: I have just started learning OOP's for python so kindly explain your answer in a simple and easy to understand way. Thank you!
EDIT 2: Here is the model
class:-
class model:
def __init__(self):
pass
# LOAD THE DATA
def load_data(self, file_name = 'train1.csv'):
self.df = pd.read_csv(file_name, index_col = 0)
self.df1= self.df.copy(deep = True)
print(self.df1.info())
self.desc = self.df1.describe()
self.nan = self.df1.isnull().sum()
return self.df1, self.desc, self.nan
# CLEAN THE DATA
def remove_whitespace(self):
whitespace_list = ['MSZoning', 'Exterior1st', 'Exterior2nd']
for p in whitespace_list:
self.df1[p] = self.df1[p].str.replace(' ', '')
# FEATURE ENGINEERING
def new_feature(self):
self.df1['Age'] = (self.df1['YrSold'] - self.df1['YearBuilt']) + (self.df1['MoSold']/12)
self.df1['Age'] = round(self.df1['Age'], 2)
self.df1['FAR'] = (self.df1['1stFlrSF'] + self.df1['2ndFlrSF']) / self.df1['LotArea']
self.df1['FAR'] = round(self.df1['FAR'], 2)
self.df1['Remod'] = np.where(self.df1['YearRemodAdd'] == self.df1['YearBuilt'], 0, 1)
# REMOVE REDUNDANT FEATURES
def remove_features(self):
nan_list = ['Alley', 'YrSold', 'PoolQC', 'MiscFeature', 'MiscVal', 'GarageYrBlt', 'YearBuilt', 'MoSold',
'1stFlrSF', '2ndFlrSF', 'LotArea', 'YearRemodAdd', 'Street', 'Utilities', 'LandSlope',
'Condition2', 'RoofMatl', 'Heating', 'GarageCond']
self.new_df = self.df1.drop(nan_list, axis = 1)
# SEPARATE X AND Y
def x_y(self):
self.x = self.new_df.drop(['SalePrice'], axis = 1)
self.y = np.log(self.new_df['SalePrice'])