Closed mattbche closed 3 years ago
Hi @mattbche,
Thanks for your question. It looks like there's a typo in your definition of the CombinedAttributesAdder
class: there's a comma missing on the line of the error. Instead of:
bedrooms_per_room = X[:,bedrooms_ix] / X[:rooms_ix]
It should be:
bedrooms_per_room = X[:,bedrooms_ix] / X[:, rooms_ix]
Here's the full class:
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]
FYI, here's the process I went through to find this bug:
"ValueError: operands could not be broadcast together with shapes (20640,) (3,13)"
. This tells me that there's an operation failing because the shapes of the operands don't go well together.bedrooms_per_room = X[:,bedrooms_ix] / X[:rooms_ix]
. That's where the error is happening.X[:, bedrooms_ix]
must not be compatible with the shape of X[:rooms_ix]
.I hope this will help you debug future errors!
Closing this issue, but feel free to reopen it if the problem persists. Please make sure you're using the exact same code as in the book. You can check by looking at the notebooks in this project.
Cheers!
Thank you for the rapid response!
I tried to run the transformation pipeline in Chapter 2 with no success. I do not know why I am getting a value error. Any help would be appreciated.
And here is the result: