# ---
# 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
# ---

# ## Example of user_input

user_input = {
    "borough": "Manhattan",
    "bedrooms": 2,
    "bathrooms": 1,
    "size_sqft": 1000,
    "min_to_subway": 5,
    "building_age_yrs": 30,    
    "floor": 5,
    "no_fee": 0,
    "has_roofdeck": 0,
    "has_patio": 0,
    "has_gym": 0,
    "has_washer_dryer": 1,
    "has_doorman": 0,
    "has_elevator": 1,
    "has_dishwasher": 0
}

# ## import dependencies

# Set the seed value for the notebook so the results are reproducible
from numpy.random import seed
seed(1)

import tensorflow
tensorflow.keras.__version__

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pickle

# ## Import Source Data

df=pd.read_csv('../input_data/streeteasy.csv')
df.shape

df.head(3)

# ## "Dummify" Source Data

# +
# Use Pandas get_dummies to convert categorical data

trimmed_df = df.drop(columns=["rental_id","building_id"])
### BEGIN SOLUTION
dummied_df = pd.get_dummies(df)
dummied_df.head(3)
### END SOLUTION
# -

# ## Dummify User Input

# +
new_input_dict = {}

for feature in user_input:
    if feature == "borough":
        boroughs = sorted(list(df["borough"].unique()))
        for borough in boroughs:
            if user_input["borough"] == borough:
                new_input_dict[f"borough_{borough}"] = 1
            else:
                new_input_dict[f"borough_{borough}"] = 0
    elif feature == "neighborhood":
        neighborhoods = sorted(list(df["neighborhood"].unique()))
        for neighborhood in neighborhoods:
            if user_input["neighborhood"] == neighborhood:
                new_input_dict[f"neighborhood_{neighborhood}"] = 1
            else:
                new_input_dict[f"neighborhood_{neighborhood}"] = 0
    elif feature == "submarket":
        submarkets = sorted(list(df["submarket"].unique()))
        for submarket in submarkets:
            if user_input["submarket"] == submarket:
                new_input_dict[f"submarket_{submarket}"] = 1
            else:
                new_input_dict[f"submarket_{submarket}"] = 0
        
    else:
        new_input_dict[feature] = user_input[feature]

new_input_dict    
# -

# ## Select Features

selected_features = list(new_input_dict.keys())
selection_df = dummied_df[selected_features]
selection_df.head(3)

# +
# Assign X (data) and y (target)

### BEGIN SOLUTION
X = selection_df
y = df["rent"].values.reshape(-1, 1)
print(X.shape, y.shape)
### END SOLUTION
# -

# ## Train/Test Split Data

# +
# Split the data into training and testing

### BEGIN SOLUTION
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
### END SOLUTION
# -

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

# ## Scale Data

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from tensorflow.keras.utils import to_categorical

# +
from sklearn.preprocessing import StandardScaler

# Create a StandardScater model and fit it to the training data

### BEGIN SOLUTION
X_scaler = StandardScaler().fit(X_train)
y_scaler = StandardScaler().fit(y_train)
### END SOLUTION

# +
# Transform the training and testing data using the X_scaler and y_scaler models

### BEGIN SOLUTION
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)
y_train_scaled = y_scaler.transform(y_train)
y_test_scaled = y_scaler.transform(y_test)
### END SOLUTION
# -

print(X_train_scaled.shape)
print(X_test_scaled.shape)
print(y_train_scaled.shape)
print(y_test_scaled.shape)

# # Model Variables

all_models = {"models":[],
             "mse":[],
             "r2":[]
             }

# # LinearRegression 

# train_scaled
from sklearn.linear_model import LinearRegression 
lm_model = LinearRegression()
lm_model.fit(X_train_scaled, y_train_scaled)
lm_predictions = lm_model.predict(X_train_scaled)

# +
#pred_train_scaled= model.predict(X_train_scaled)
from sklearn.metrics import mean_squared_error
print(np.sqrt(mean_squared_error(y_train_scaled,lm_predictions)))

lm_pred= lm_model.predict(X_test_scaled)
print(np.sqrt(mean_squared_error(y_test_scaled,lm_pred))) 

# +
from sklearn.metrics import mean_squared_error

MSE = mean_squared_error(y_train_scaled, lm_predictions)
r2 = lm_model.score(X_test_scaled, y_test_scaled)

print(f"MSE: {MSE}, R2: {r2}")
# -

all_models["models"].append(lm_model)
all_models["mse"].append(MSE)
all_models["r2"].append(r2)

# # Lasso

# +
# LASSO model
# Note: Use an alpha of .01 when creating the model for this activity
from sklearn.linear_model import Lasso

### BEGIN SOLUTION
lasso = Lasso(alpha=.01).fit(X_train_scaled, y_train_scaled)

lasso_predictions = lasso.predict(X_test_scaled)

MSE = mean_squared_error(y_test_scaled, lasso_predictions)
r2 = lasso.score(X_test_scaled, y_test_scaled)
### END SOLUTION
print(f"MSE: {MSE}, R2: {r2}")
# -

all_models["models"].append(lasso)
all_models["mse"].append(MSE)
all_models["r2"].append(r2)

# # Ridge

# +
# Ridge model
# Note: Use an alpha of .01 when creating the model for this activity
from sklearn.linear_model import Ridge

### BEGIN SOLUTION
ridge = Ridge(alpha=.01).fit(X_train_scaled, y_train_scaled)

ridge_predictions = ridge.predict(X_test_scaled)

MSE = mean_squared_error(y_test_scaled, ridge_predictions)
r2 = ridge.score(X_test_scaled, y_test_scaled)
### END SOLUTION

print(f"MSE: {MSE}, R2: {r2}")
# -

all_models["models"].append(ridge)
all_models["mse"].append(MSE)
all_models["r2"].append(r2)

# # ElasticNet model

# +
# ElasticNet model
# Note: Use an alpha of .01 when creating the model for this activity
from sklearn.linear_model import ElasticNet

### BEGIN SOLUTION
elasticnet = ElasticNet(alpha=.01).fit(X_train_scaled, y_train_scaled)

el_predictions = elasticnet.predict(X_test_scaled)

MSE = mean_squared_error(y_test_scaled, el_predictions)
r2 = elasticnet.score(X_test_scaled, y_test_scaled)
### END SOLUTION

print(f"MSE: {MSE}, R2: {r2}")
# -

all_models["models"].append(elasticnet)
all_models["mse"].append(MSE)
all_models["r2"].append(r2)

# # XGBoost Regressor

# +
from xgboost import XGBRegressor
XGBModel = XGBRegressor()
XGBModel.fit(X_train_scaled, y_train_scaled , verbose=False)

# Get the mean absolute error on the validation data :
XGBpredictions = XGBModel.predict(X_test_scaled)

MSE = mean_squared_error(y_test_scaled, XGBpredictions)
r2 = XGBModel.score(X_test_scaled, y_test_scaled)
### END SOLUTION

print(f"MSE: {MSE}, R2: {r2}")
# -

all_models["models"].append(XGBModel)
all_models["mse"].append(MSE)
all_models["r2"].append(r2)

# # Neural Network

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Define model
model = Sequential()
model.add(Dense(500, input_dim=len(new_input_dict.values()), activation= "relu"))
model.add(Dense(100, activation= "relu"))
model.add(Dense(50, activation= "relu"))
model.add(Dense(1))

# Compile and fit the model
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mean_squared_error'])   

# Fit the model to the training data
model.fit(
    X_train_scaled,
    y_train_scaled,
    epochs=100,
    shuffle=True,
    verbose=2
)

# +
pred_train_scaled= model.predict(X_train_scaled)
MSE = np.sqrt(mean_squared_error(y_train_scaled,pred_train_scaled))
print(MSE)

pred = model.predict(X_test_scaled)
r2 = np.sqrt(mean_squared_error(y_test_scaled, pred))
print(r2) 
# -

all_models["models"].append(model)
all_models["mse"].append(MSE)
all_models["r2"].append(r2)

all_models

# ## Scale User Input Data

input_values = list(new_input_dict.values())
input_values_scaled = X_scaler.transform(pd.DataFrame(new_input_dict, index=[0]))
input_values_scaled

# ## Get Predictions

# +
lm_pred_array = y_scaler.inverse_transform([lm_model.predict(input_values_scaled),])
lm_pred = lm_pred_array[0][0][0]

lasso_pred_array = y_scaler.inverse_transform([lasso.predict(input_values_scaled),])
lasso_pred = lasso_pred_array[0][0]

ridge_pred_array = y_scaler.inverse_transform([ridge.predict(input_values_scaled),])
ridge_pred = ridge_pred_array[0][0][0]

elas_pred_array = y_scaler.inverse_transform([elasticnet.predict(input_values_scaled),])
elasticnet_pred = elas_pred_array[0][0]

XGB_pred_array = y_scaler.inverse_transform([XGBModel.predict(input_values_scaled),])
XGB_pred = XGB_pred_array[0][0]

nn_pred_array = y_scaler.inverse_transform([model.predict(input_values_scaled),])
nn_pred = nn_pred_array[0][0][0]
# -

# ## Package Results

results = {
    "lm": {"model":"Linear Regression",
          "r2": all_models["r2"][0],
          "prediction": lm_pred},
    "lasso": {"model":"Linear Regression",
          "r2": all_models["r2"][1],
          "prediction": lasso_pred},
    "ridge": {"model":"Ridge",
          "r2": all_models["r2"][2],
          "prediction": ridge_pred},
    "elas": {"model":"ElasticNet",
          "r2": all_models["r2"][3],
          "prediction": elasticnet_pred},
    "xgb": {"model":"XGBoost Regressor",
          "r2": all_models["r2"][4],
          "prediction": XGB_pred},
    "nn": {"model":"Neural Network",
          "r2": all_models["r2"][5],
          "prediction": nn_pred},
}

results

StarcoderdataJupyterScriptsDedupFiltered