<jupyter_start><jupyter_text>**Handling Text and Categorical Attributes**<jupyter_code>housing_cat = housing[['ocean_proximity']]
housing_cat.head()
from sklearn.preprocessing import OrdinalEncoder
ordinal_encoder = OrdinalEncoder()
housing_cat_encoded =ordinal_encoder.fit_transform(housing_cat)
housing_cat_encoded[:5]
ordinal_encoder.categories_
from sklearn.preprocessing import OneHotEncoder
cat_encoder = OneHotEncoder()
housing_cat_1hot = cat_encoder.fit_transform(housing_cat)
housing_cat_1hot
housing_cat_1hot.toarray()
cat_encoder.categories_
housing.head()
# rooms_per_bedroom  = room/bedroom
# population_per_household = population / household
# bedrooms_per_room = bedrooms / room
from sklearn.base import BaseEstimator, TransformerMixin

rooms_ix, bedroom_ix , population_ix , households_ix = 3,4,5,6

class CombinedAtrributesAdder():

   def __init__(self,add_bedrooms_per_room = True):
    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,y=None):
    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[:,bedroom_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]


housing.values
attr_adder = CombinedAtrributesAdder(add_bedrooms_per_room=False)
housing_extra_atrribs = attr_adder.transform(housing.values)
housing_extra_atrribs[:5,:]
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

num_pipeline = Pipeline([
                         ('imputer',SimpleImputer(strategy="median")),
                         ('attribs_adder',CombinedAtrributesAdder()),
                         ('std_scalar',StandardScaler())
])

housing_num_tr = num_pipeline.fit_transform(housing_num)
housing_num_tr[:]
# Data Containes Categorical Data 

from sklearn.compose import ColumnTransformer

num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]


full_pipeline = ColumnTransformer([
                                   
                ("num", num_pipeline, num_attribs),
                ("cat",OneHotEncoder(),cat_attribs)
])

housing_prepared = full_pipeline.fit_transform(housing)
housing_prepared<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09209987720016373, 'fraction_numerical': 0.0036839950880065493, 'mean_word_length': 5.11, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '315702', 'license': 'MIT', 'n_tokens_mistral': 829, 'n_tokens_neox': 815, 'n_words': 123, 'path': 'End_to_END_Machine_Learning_Project.ipynb', 'repo_name': 'sainathpawar/END-To-END-Machine-Learinng-Project', 'size': '2443'}