<jupyter_start><jupyter_text>**Housing Prices Competition - Advanced Regression Techniques**https://www.kaggle.com/c/house-prices-advanced-regression-techniques<jupyter_code>train_d_full = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
train_d_full.head()

train_d_full.shape
# Rows x Columns
print(train_d_full['SalePrice'].mean())
test_d = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
test_d.head()<jupyter_output><empty_output><jupyter_text>Mean Sale Price (Target Feature) = $180,921<jupyter_code>y = train_d_full.SalePrice
for i in train_d_full.columns:
    st = set(train_d_full[i])
    if(len(st) == 2):
        print(i)
        print(st)
cols_dropped = ['SalePrice', 'Utilities', 'Street']
#Utilities and Streets Dropped as they have no contribution to dictating House Prices
train_d = train_d_full.drop(cols_dropped, axis = 1)
cols_dropped_tst = ['Utilities', 'Street']
test_d = test_d.drop(cols_dropped_tst, axis = 1)
# Split training data into training and validation subsets
# Smaller Data set so will use cross validation after
from sklearn.model_selection import train_test_split
X = train_d
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 0)
# Define Numerical Columns and Categorical Columns

# "Cardinality" means the number of unique values in a column
# Select categorical columns with relatively low cardinality (convenient but arbitrary)
# Keep all Columns
#categorical_cols = [cname for cname in train_d_full.columns if train_d_full[cname].nunique() < 10 and 
       #                 train_d_full[cname].dtype == "object"]'
categorical_cols = [cname for cname in train_X.columns if train_X[cname].dtype == "object"]

# Select numerical columns
numerical_cols = [cname for cname in train_X.columns if train_X[cname].dtype in ['int64', 'float64']]

# Keep selected columns only
my_cols = categorical_cols + numerical_cols
X_train = train_X[my_cols].copy()
X_val = val_X[my_cols].copy()<jupyter_output><empty_output><jupyter_text>*PreProcessing Below (Using Pipeline) : *<jupyter_code># Columns of Categorical Data
c = (train_X.dtypes == 'object')
object_cols = list(c[c].index)


from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder

# Preprocessing for numerical data
numerical_transformer = SimpleImputer(strategy='mean')

# Preprocessing for categorical data
categorical_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent')),
    ('onehot', OneHotEncoder(handle_unknown='ignore'))
])

# Bundle preprocessing for numerical and categorical data
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numerical_transformer, numerical_cols),
        ('cat', categorical_transformer, categorical_cols)
    ])
# Missing Data
cols_missing = [col for col in train_X.columns
                     if train_X[col].isnull().any()]
print(cols_missing)
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import cross_val_score
# Parameter Tuning
# Baseline MAE :  $ 17293.552183219177
# Mean NMAE Scores :  $ 18295.807229238017

# Tune n_estimators and learning rate until flat line score reached

# Use Grid Search to optimize model and find best (smallest) mae on validation data 
model = XGBRegressor(n_estimators = 600, learning_rate = 0.05)

pipeline_fin = Pipeline(steps=[('preprocessor', preprocessor),
                              ('model', model)
                             ])

pipeline_fin.fit(X_train, train_y,)


predictions = pipeline_fin.predict(X_val)
print('MAE XGBoost : ', mean_absolute_error(predictions, val_y))

# Cross Validation Scores Below : 
scores = -1 * cross_val_score(pipeline_fin, X, y,
                              cv=5,
                              scoring='neg_mean_absolute_error')
print('Cross Validation : ', list(scores))
print('Mean Cross NMAE Scores : ', scores.mean())

# Make CSV file for submission

# Final Predictions :
print(len(X.columns))
pipeline_fin.fit(X,y)
preds_test = pipeline_fin.predict(test_d)
output = pd.DataFrame({'Id': test_d.Id, 'SalePrice': preds_test})
output.to_csv('submission_hp_first.csv', index = False)

print("Your submission was successfully saved!")<jupyter_output>78
Your submission was successfully saved!
{'fraction_non_alphanumeric': 0.1025350837483024, 'fraction_numerical': 0.013580805794477138, 'mean_word_length': 4.9156626506024095, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '662464', 'license': 'MIT', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1415, 'n_words': 349, 'path': 'hp-draft-1.ipynb', 'repo_name': 'ronitpatt/Housing-Prices-Competition', 'size': '4418'}