# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# Se cargan las librerías que se van a utilizar en ambos ejemplos
import math
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import sklearn

from sklearn.impute import SimpleImputer
from sklearn.compose import make_column_transformer

from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import make_pipeline


from sklearn.preprocessing import PolynomialFeatures  # <------ library to perform Polynomial Regression

from sklearn.linear_model import Ridge
from sklearn.linear_model import LinearRegression

from sklearn import metrics
from sklearn.metrics import mean_squared_error

from sklearn.preprocessing import scale 
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score

from sklearn.linear_model import Lasso
from sklearn.model_selection import GridSearchCV

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

pd.set_option('display.max_rows', 90) # by default is 10, if change to None print ALL
pd.set_option('display.max_columns', 90) # by default is 10, if change to None print ALL

# +
## 1) EXTRAER DATOS
# Los datos pueden encontrarse en diferentes formatos, en nuestro caso están en formato csv.

# Se carga la base de datos
train = pd.read_csv('train.csv') #Se encuentra en la misma carpeta que el jupyter notebook
test = pd.read_csv('test.csv') #Se encuentra en la misma carpeta que el jupyter notebook
print(train.shape) 
print(test.shape) 
train
# -

# # Eliminate of some columns 

# Veamos a eleminar las columnas que tienen más del 50% de sus valores como nulas en train

col_plus_50percent_null = train.isnull().sum()[train.isnull().sum()>train.shape[0]/2]
col_plus_50percent_null

# Observemos que también hay casi las mismas columnas en test 

test.isnull().sum()[test.isnull().sum()>test.shape[0]/2]

# Entonces nos queda 

features_drop = ['PoolQC','MiscFeature','Alley','Fence']
train = train.drop(features_drop, axis=1)
test  =  test.drop(features_drop, axis=1)  

# Comprovemos que ya no tenemos esas variables

col_plus_50percent_null = train.isnull().sum()[train.isnull().sum()>train.shape[0]/2]
col_plus_50percent_null

test.isnull().sum()[test.isnull().sum()>test.shape[0]/2]

# # Separación de variables
#
# Separemos las variables en `X_train`, `X_test`, `y_train`, `y_test`, al igual que elijamos que columnas son numericas, ordinales y nominales

# +

numerical = train.select_dtypes(include=np.number).columns.tolist()
numerical.remove('Id')
numerical.remove('SalePrice')

nominal = train.select_dtypes(exclude=np.number).columns.tolist()

# ordinal = ["LandSlope", "OverallQual", "OverallCond", "YearRemodAdd",
#           "ExterQual", "ExterCond", "BsmtQual", "BsmtCond", "BsmtExposure",
#           "KitchenQual", "Functional", "GarageCond", "PavedDrive"]

ordinal = []

X = train[nominal + ordinal + numerical] #LotFrontage y MasVnrType tiene NaNs
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

X_REAL_test = test[nominal + ordinal + numerical]
# -



# # Pipelines auxiliares
#
# Para separar mejor el procesamiento de nuestros datos, utilizamos tres pipelines auxiliares

# +
# Pipeline datos ordinales
ordinal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OrdinalEncoder())
])

# Pipeline datos nominales
nominal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(sparse=True, handle_unknown="ignore"))
])

# Pipeline datos numéricos
numerical_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="mean")),
    ("scaler", StandardScaler())
])

# Pegado de los tres pipelines
preprocessing_pipeline = ColumnTransformer([
    ("nominal_preprocessor", nominal_pipeline, nominal),
    ("ordinal_preprocessor", ordinal_pipeline, ordinal),
    ("numerical_preprocessor", numerical_pipeline, numerical)
])

# -

# Finalmente agregamos todo en un solo pipeline

# +
# preprocessed_features = preprocessing_pipeline.fit_transform(train_features)

# ML_model = Lasso(alpha=190)
# ML_model = Ridge(alpha=20)
ML_model = LinearRegression()

complete_pipeline = Pipeline([
    ("preprocessor", preprocessing_pipeline),
    ("estimator", ML_model)
])
# -

complete_pipeline

# # Predicciones

# +
complete_pipeline.fit(X_train, y_train)
y_pred = complete_pipeline.predict(X_test)

print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score:', r2_score(y_test, y_pred)) 

p1 = max(max(y_pred), max(y_test))
p2 = min(min(y_pred), min(y_test))
plt.plot([p1, p2], [p1, p2], 'b-')
plt.scatter(y_test,y_pred)
# -

# # Generación de archivo para Kaggle

# +
y_REAL_test = complete_pipeline.predict(X_REAL_test)

pred=pd.DataFrame(y_REAL_test)
sub_df=pd.read_csv('sample_submission.csv')
datasets=pd.concat([sub_df['Id'],pred],axis=1)
datasets.columns=['Id','SalePrice']
datasets.to_csv('sample_submission.csv',index=False)
# -

# Para subir el archivo es [aquí](https://www.kaggle.com/c/house-prices-advanced-regression-techniques/overview/evaluation)













# +
# FUNCIONA PIPELINE LASSO WITH 

numerical = train.select_dtypes(include=np.number).columns.tolist()
numerical.remove('Id')
numerical.remove('SalePrice')

nominal = train.select_dtypes(exclude=np.number).columns.tolist()

ordinal = ["LandSlope", "OverallQual", "OverallCond", "YearRemodAdd",
          "ExterQual", "ExterCond", "BsmtQual", "BsmtCond", "BsmtExposure",
          "KitchenQual", "Functional", "GarageCond", "PavedDrive"]

ordinal = []


X = train[nominal + ordinal + numerical] #LotFrontage y MasVnrType tiene NaNs
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

ordinal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OrdinalEncoder())
])

nominal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(sparse=True, handle_unknown="ignore"))
])

numerical_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="mean")),
    ("scaler", StandardScaler())
])


# here we are going to instantiate a ColumnTransformer object with a list of tuples
# each of which has a the name of the preprocessor
# the transformation pipeline (could be a transformer)
# and the list of column names we wish to transform

preprocessing_pipeline = ColumnTransformer([
    ("nominal_preprocessor", nominal_pipeline, nominal),
    ("ordinal_preprocessor", ordinal_pipeline, ordinal),
    ("numerical_preprocessor", numerical_pipeline, numerical)
])

## If you want to test this pipeline run the following code

# preprocessed_features = preprocessing_pipeline.fit_transform(train_features)


ML_model = Lasso(alpha=1)
ML_model = Ridge(alpha=.1)
# ML_model = LinearRegression()

complete_pipeline = Pipeline([
    ("preprocessor", preprocessing_pipeline),
#     ("scaler", StandardScaler()), # No mejora la estimación escalando
#     ('poly_features', PolynomialFeatures(degree=2)), # empeora con polynomal features
    ("estimator", ML_model)
])


complete_pipeline.fit(X_train, y_train)

y_pred = complete_pipeline.predict(X_test)

print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 

p1 = max(max(y_pred), max(y_test))
p2 = min(min(y_pred), min(y_test))
plt.plot([p1, p2], [p1, p2], 'b-')
plt.scatter(y_test,y_pred)

# -



# # ALL IN ONE

# +
# FUNCIONA PIPELINE LASSO WITH 

numerical = train.select_dtypes(include=np.number).columns.tolist()
numerical.remove('Id')
numerical.remove('SalePrice')

nominal = train.select_dtypes(exclude=np.number).columns.tolist()

ordinal = ["LandSlope", "OverallQual", "OverallCond", "YearRemodAdd",
          "ExterQual", "ExterCond", "BsmtQual", "BsmtCond", "BsmtExposure",
          "KitchenQual", "Functional", "GarageCond", "PavedDrive"]

ordinal = []


X = train[nominal + ordinal + numerical] #LotFrontage y MasVnrType tiene NaNs
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

ordinal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OrdinalEncoder())
])

nominal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(sparse=True, handle_unknown="ignore"))
])

numerical_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="mean")),
    ("scaler", StandardScaler())
])


# here we are going to instantiate a ColumnTransformer object with a list of tuples
# each of which has a the name of the preprocessor
# the transformation pipeline (could be a transformer)
# and the list of column names we wish to transform

preprocessing_pipeline = ColumnTransformer([
    ("nominal_preprocessor", nominal_pipeline, nominal),
    ("ordinal_preprocessor", ordinal_pipeline, ordinal),
    ("numerical_preprocessor", numerical_pipeline, numerical)
])

## If you want to test this pipeline run the following code

# preprocessed_features = preprocessing_pipeline.fit_transform(train_features)


ML_model = Lasso(alpha=190)
ML_model = LinearRegression()

complete_pipeline = Pipeline([
    ("preprocessor", preprocessing_pipeline),
#     ("scaler", StandardScaler()), # No mejora la estimación escalando
#     ('poly_features', PolynomialFeatures(degree=2)),
    ("estimator", LinearRegression())
])


complete_pipeline.fit(X_train, y_train)


y_pred = complete_pipeline.predict(X_test)


print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 

p1 = max(max(y_pred), max(y_test))
p2 = min(min(y_pred), min(y_test))
plt.plot([p1, p2], [p1, p2], 'b-')
plt.scatter(y_test,y_pred)

# -





aux = ct.fit_transform(X)
aux = pd.df(aux)

# +

preprocessed_features = preprocessing_pipeline.fit_transform(X_train)
preprocessed_features

# +
from sklearn.linear_model import Lasso
from sklearn.model_selection import GridSearchCV
lasso=Lasso()
parameters={'alpha':[1e-15,1e-10,1e-8,1e-3,1e-2,1,5,10,20,30,35,40,45,50,55,100,200,300]}
lasso_regressor=GridSearchCV(lasso,parameters,scoring='neg_mean_squared_error',cv=5)

lasso_regressor.fit(preprocessing_pipeline.fit_transform(X_train),y_train)
print(lasso_regressor.best_params_)
print(lasso_regressor.best_score_)
# -

# # Encontrando alpha de Lasso (alpha = 180)

parameters={'alpha':[100,150,170,180,190,200,220,250,300]}
ML_model=Lasso()
grid = GridSearchCV(ML_model,parameters,scoring='neg_mean_squared_error',cv=5)
grid.fit(preprocessing_pipeline.fit_transform(X_train),y_train)
# Convert the results of CV into a dataframe
results = pd.DataFrame(grid.cv_results_)[['params', 'mean_test_score', 'rank_test_score']]
results.sort_values('rank_test_score')

# # Encontrando alpha de Ridge (alpha = 20)

parameters={'alpha':[1e-15,1e-10,1e-8,1e-3,1e-2,1,5,10,20,30,35,40,45,50,55,100,200,300]}
ML_model=Ridge()
grid = GridSearchCV(ML_model,parameters,scoring='neg_mean_squared_error',cv=5)
grid.fit(preprocessing_pipeline.fit_transform(X_train),y_train)
# Convert the results of CV into a dataframe
results = pd.DataFrame(grid.cv_results_)[['params', 'mean_test_score', 'rank_test_score']]
results.sort_values('rank_test_score')

# ## Numeric missing values 

# # One Hot Encoder





model

# +
# https://salvatore-raieli.medium.com/a-complete-guide-to-linear-regression-using-gene-expression-data-regularization-f980ba6b11f7


model = Lasso(alpha = 180)
model.fit(preprocessing_pipeline.fit_transform(X_train), y_train)

y_pred = complete_pipeline.predict(X_test)


coefs = model.coef_.flatten()
names = X_train.columns
genes = list(zip(names, coefs))
feature =pd.DataFrame(genes, columns = ["genes", "coefs"])
feature0 = feature.loc[(feature!=0).any(axis=1)]
feature0 = feature[(feature != 0).all(1)]
feature0.shape, feature.shape


print(feature0.shape, feature.shape)

coefs =feature0.sort_values(by=['coefs'])
plt.figure(figsize=(20, 15))
g = sns.barplot(x="genes", y="coefs", data=coefs, color= "lightblue")
g.figsize=(16,10)
plt.xticks(rotation=45)
# -

feature0













# +
# FUNCIONA LASSO

X = train[['MSSubClass', 'LotArea', 'OverallQual']]
y = train['SalePrice']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

model_lasso = Lasso(alpha=0.01)
model_lasso.fit(X_train, y_train) 
y_pred= model_lasso.predict(X_test)


print('Predictions with Polynomial Regression')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 
plt.scatter(y_test,y_pred)


# +
# LASSO PIPELINE FUNCIONA

X = train[['MSSubClass','LotArea','OverallQual','LotFrontage']]#LotFrontage y MasVnrType tiene NaNs
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

imp_mean = SimpleImputer(missing_values =np.nan, strategy='mean')

columns_imp_mean = ['LotFrontage']

scaler = StandardScaler()

column_trans = make_column_transformer(
    (imp_mean,columns_imp_mean),
    remainder = 'passthrough')


ML_model = Lasso(alpha=0.01)

pipe = make_pipeline(column_trans, ML_model)

print(cross_val_score(pipe,X_train,y_train,cv=5))
pipe.fit(X_train,y_train)


y_pred= pipe.predict(X_test)


print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 
plt.scatter(y_test,y_pred)
# -



# primero hace la división de cross-validation y después hace el pipeline, 
# La diferencia de hacerlo así es que entonces cuando toma promedios para calcular como llenar los missing values, 
# estos promedios son con respecto al cross-validation
cross_val_score(pipe,X,y,cv=5,scoring='accuracy').mean() 





# +
# FUNCIONA PIPELINE LASSO WITH 
nominal = ["MSZoning", "LotShape", "LandContour", "LotConfig", "Neighborhood",
           "Condition1", "BldgType", "RoofStyle",
           "Foundation", "CentralAir", "SaleType", "SaleCondition"]

ordinal = ["LandSlope", "OverallQual", "OverallCond", "YearRemodAdd",
          "ExterQual", "ExterCond", "BsmtQual", "BsmtCond", "BsmtExposure",
          "KitchenQual", "Functional", "GarageCond", "PavedDrive"]

numerical = ["LotFrontage", "LotArea", "MasVnrArea", "BsmtFinSF1", "BsmtUnfSF",
            "TotalBsmtSF", "1stFlrSF", "2ndFlrSF", "GrLivArea", "GarageArea",
            "OpenPorchSF"]

X = train[nominal + ordinal + numerical] #LotFrontage y MasVnrType tiene NaNs
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

ordinal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OrdinalEncoder())
])

nominal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(sparse=True, handle_unknown="ignore"))
])

numerical_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="mean")),
    ("scaler", StandardScaler())
])

# here we are going to instantiate a ColumnTransformer object with a list of tuples
# each of which has a the name of the preprocessor
# the transformation pipeline (could be a transformer)
# and the list of column names we wish to transform

preprocessing_pipeline = ColumnTransformer([
    ("nominal_preprocessor", nominal_pipeline, nominal),
    ("ordinal_preprocessor", ordinal_pipeline, ordinal),
    ("numerical_preprocessor", numerical_pipeline, numerical)
])

## If you want to test this pipeline run the following code

# preprocessed_features = preprocessing_pipeline.fit_transform(train_features)


complete_pipeline = Pipeline([
    ("preprocessor", preprocessing_pipeline),
    ("scaler", StandardScaler()), # No mejora la estimación escalando
    ("estimator", LinearRegression())
])

complete_pipeline.fit(X_train, y_train)

y_pred = complete_pipeline.predict(X_test)


print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 
plt.scatter(y_test,y_pred)

# -













# +
# FUNCIONA PIPELINE LASSO WITH 
nominal = ["MSZoning", "LotShape", "LandContour", "LotConfig", "Neighborhood",
           "Condition1", "BldgType", "RoofStyle",
           "Foundation", "CentralAir", "SaleType", "SaleCondition"]

ordinal = ["LandSlope", "OverallQual", "OverallCond", "YearRemodAdd",
          "ExterQual", "ExterCond", "BsmtQual", "BsmtCond", "BsmtExposure",
          "KitchenQual", "Functional", "GarageCond", "PavedDrive"]

numerical = ["LotFrontage", "LotArea", "MasVnrArea", "BsmtFinSF1", "BsmtUnfSF",
            "TotalBsmtSF", "1stFlrSF", "2ndFlrSF", "GrLivArea", "GarageArea",
            "OpenPorchSF"]

X = train[nominal + ordinal + numerical] #LotFrontage y MasVnrType tiene NaNs
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

ordinal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OrdinalEncoder())
])

nominal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(sparse=True, handle_unknown="ignore"))
])

numerical_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="mean")),
    ("scaler", StandardScaler())
])


# here we are going to instantiate a ColumnTransformer object with a list of tuples
# each of which has a the name of the preprocessor
# the transformation pipeline (could be a transformer)
# and the list of column names we wish to transform

preprocessing_pipeline = ColumnTransformer([
    ("nominal_preprocessor", nominal_pipeline, nominal),
    ("ordinal_preprocessor", ordinal_pipeline, ordinal),
    ("numerical_preprocessor", numerical_pipeline, numerical)
])

## If you want to test this pipeline run the following code

# preprocessed_features = preprocessing_pipeline.fit_transform(train_features)


from sklearn.linear_model import LinearRegression

complete_pipeline = Pipeline([
    ("preprocessor", preprocessing_pipeline),
    ("estimator", LinearRegression())
])

complete_pipeline.fit(X_train, y_train)


y_pred = complete_pipeline.predict(X_test)


print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 
plt.scatter(y_test,y_pred)

# -





# *A general rule of thumb: drop a dummy-encoded column if using a linear-based model, and do not drop it if using a tree-based model*







# +
true_value = y_test
predicted_value = y_pred

plt.figure(figsize=(10,10))
plt.scatter(true_value, predicted_value, c='crimson')
# plt.yscale('log')
# plt.xscale('log')

p1 = max(max(predicted_value), max(true_value))
p2 = min(min(predicted_value), min(true_value))
plt.plot([p1, p2], [p1, p2], 'b-')
plt.xlabel('True Values', fontsize=15)
plt.ylabel('Predictions', fontsize=15)
plt.axis('equal')
plt.show()
# -









# The next cell is from [here](https://mahmoudyusof.github.io/general/scikit-learn-pipelines/)

# +
# The next cell is from https://mahmoudyusof.github.io/general/scikit-learn-pipelines/


train_df = pd.read_csv("train.csv")
test_df = pd.read_csv("test.csv")

## let's create a validation set from the training set

msk = np.random.rand(len(train_df)) < 0.8

val_df = train_df[~msk]

train_df = train_df[msk]


nominal = ["MSZoning", "LotShape", "LandContour", "LotConfig", "Neighborhood",
           "Condition1", "BldgType", "RoofStyle",
           "Foundation", "CentralAir", "SaleType", "SaleCondition"]

ordinal = ["LandSlope", "OverallQual", "OverallCond", "YearRemodAdd",
          "ExterQual", "ExterCond", "BsmtQual", "BsmtCond", "BsmtExposure",
          "KitchenQual", "Functional", "GarageCond", "PavedDrive"]

numerical = ["LotFrontage", "LotArea", "MasVnrArea", "BsmtFinSF1", "BsmtUnfSF",
            "TotalBsmtSF", "1stFlrSF", "2ndFlrSF", "GrLivArea", "GarageArea",
            "OpenPorchSF"]

train_features = train_df[nominal + ordinal + numerical]
train_label = train_df["SalePrice"]

val_features = val_df[nominal + ordinal + numerical]
val_label = val_df["SalePrice"]

test_features = test_df[nominal + ordinal + numerical]


from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OrdinalEncoder, OneHotEncoder

ordinal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OrdinalEncoder())
])

nominal_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(sparse=True, handle_unknown="ignore"))
])

numerical_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="mean")),
    ("scaler", StandardScaler())
])


from sklearn.compose import ColumnTransformer

# here we are going to instantiate a ColumnTransformer object with a list of tuples
# each of which has a the name of the preprocessor
# the transformation pipeline (could be a transformer)
# and the list of column names we wish to transform

preprocessing_pipeline = ColumnTransformer([
    ("nominal_preprocessor", nominal_pipeline, nominal),
    ("ordinal_preprocessor", ordinal_pipeline, ordinal),
    ("numerical_preprocessor", numerical_pipeline, numerical)
])

## If you want to test this pipeline run the following code

# preprocessed_features = preprocessing_pipeline.fit_transform(train_features)


from sklearn.linear_model import LinearRegression

complete_pipeline = Pipeline([
    ("preprocessor", preprocessing_pipeline),
    ("estimator", LinearRegression())
])


complete_pipeline.fit(train_features, train_label)






# score = complete_pipeline.score(val_features, val_label)

# print(score)

# predictions = complete_pipeline.predict(test_features)





# +
# pipe = make_pipeline(column_trans, ML_model)

# print(cross_val_score(complete_pipeline,X_train,y_train,cv=5))
# pipe.fit(X_train,y_train)


y_pred = complete_pipeline.predict(X_test)


print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 
plt.scatter(y_test,y_pred)
# -



# +
# LASSO PIPELINE FUNCIONA

X = train[['MSSubClass','LotArea','OverallQual','LotFrontage']]#LotFrontage y MasVnrType tiene NaNs
y = train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

imp_mean = SimpleImputer(missing_values =np.nan, strategy='mean')

columns_imp_mean = ['LotFrontage']

scaler = StandardScaler()

column_trans = make_column_transformer(
    (imp_mean,columns_imp_mean),
    remainder = 'passthrough')


ML_model = Lasso(alpha=0.01)

pipe = make_pipeline(column_trans, ML_model)

print(cross_val_score(pipe,X_train,y_train,cv=5))
pipe.fit(X_train,y_train)


y_pred= pipe.predict(X_test)


print('ERRORS OF PREDICTIONS')
print('MAE:', metrics.mean_absolute_error(y_test, y_pred)) 
print('MSE:', metrics.mean_squared_error(y_test, y_pred)) 
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) 
print('r2_score', r2_score(y_test, y_pred)) 
plt.scatter(y_test,y_pred)

# -

























StarcoderdataJupyterScriptsDedupFiltered