-
-
Save amankharwal/9485d72193616c0ed265cf52987fdebb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Data Preparation | |
housing = strat_train_set.drop("median_house_value", axis=1) | |
housing_labels = strat_train_set["median_house_value"].copy() | |
median = housing["total_bedrooms"].median() | |
housing["total_bedrooms"].fillna(median, inplace=True) | |
housing_num = housing.drop("ocean_proximity", axis=1) | |
from sklearn.base import BaseEstimator, TransformerMixin | |
# column index | |
rooms_ix, bedrooms_ix, population_ix, households_ix = 3, 4, 5, 6 | |
class CombinedAttributesAdder(BaseEstimator, TransformerMixin): | |
def __init__(self, add_bedrooms_per_room=True): # no *args or **kargs | |
self.add_bedrooms_per_room = add_bedrooms_per_room | |
def fit(self, X, y=None): | |
return self # nothing else to do | |
def transform(self, X): | |
rooms_per_household = X[:, rooms_ix] / X[:, households_ix] | |
population_per_household = X[:, population_ix] / X[:, households_ix] | |
if self.add_bedrooms_per_room: | |
bedrooms_per_room = X[:, bedrooms_ix] / X[:, rooms_ix] | |
return np.c_[X, rooms_per_household, population_per_household, | |
bedrooms_per_room] | |
else: | |
return np.c_[X, rooms_per_household, population_per_household] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment