0

I'm new in python and ML. I wrote this code for practicing. It displays this error. how do I solve this reshape problem?

import pandas as pd
import sklearn as skl

data=pd.read_csv('housing.csv')
housing=data['ocean_proximity']
from sklearn.preprocessing import OneHotEncoder
encoder=OneHotEncoder()
housing_cat_fac=housing.factorize()
housing_final=encoder.fit_transform(housing_cat_fac.reshape(1,-1))
AttributeError                            Traceback (most recent call last)
<ipython-input-31-b13a23adc636> in <cell line: 9>()
      7 encoder=OneHotEncoder()
      8 housing_cat_fac=housing.factorize()
----> 9 housing_final=encoder.fit_transform(housing_cat_fac.reshape(1,-1))

AttributeError: 'tuple' object has no attribute 'reshape'
1
  • In general if x.y gives an attribute error, either you read the docs for x class objects wrong, or (more likely) x` isn't the class you expected. With reshape you probably expected x to be an array, not a tuple. Either way you need to reread the relevant docs.
    – hpaulj
    Commented May 29 at 22:37

1 Answer 1

2

factorize returns a tuple of the factorized values and unique original values.

Thus housing_cat_fac=housing.factorize() will make housing_cat_fac a tuple.

You should use:

housing_cat_fac = housing.factorize()[0]

Or, if you want to keep the original values:

housing_cat_fac, uniq_vals = housing.factorize()

Then housing_cat_fac will be a numpy array.

Not the answer you're looking for? Browse other questions tagged or ask your own question.