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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/ewuerfel66/DS-Unit-2-Linear-Models/blob/master/LinearRegression_EricWuerfel.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="CXvBJyQ8LilZ" colab_type="text"
# # Linear Regression with SciKitLearn

# + [markdown] id="CvB-fOhlLoAB" colab_type="text"
# ## Imports

# + id="Yu36xOkKLd74" colab_type="code" colab={}
import pandas as pd
import numpy as np

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error

import category_encoders as ce

# + id="MK6NibahLx7G" colab_type="code" colab={}
LOCAL = '../data/nyc/nyc-rent-2016.csv'
WEB = 'https://raw.githubusercontent.com/LambdaSchool/DS-Unit-2-Linear-Models/master/data/nyc/nyc-rent-2016.csv'

df = pd.read_csv(WEB)
assert df.shape == (48300, 34)

# + id="7D7u4CIPYOFE" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 504} outputId="173f277c-36da-4124-d398-11d52a8d4f30"
df.head()

# + [markdown] id="OoFCqqRIMPU1" colab_type="text"
# ## Train/Test Split

# + id="-i9IEhjwMGrH" colab_type="code" colab={}
df['created'] = pd.to_datetime(df['created'], infer_datetime_format=True)

# + id="6jCPhfHIMeWo" colab_type="code" colab={}
# Extract the month that the apartment listing was created
df['month'] = df['created'].dt.month

# + id="9EsY9InJMoS-" colab_type="code" colab={}
# Here's the actual split into TRAIN/TEST
train = df.query('month < 6')
test = df.query('month == 6')

# + id="56H5mKZsM2tc" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="d1f9cdd8-e6a6-4ab0-bd9f-2acda3b8faa1"
train.shape, test.shape, df.shape

# + [markdown] id="MRCmIVbrNTMy" colab_type="text"
# ## Baseline Model
#
# **MAE: $1020.06**

# + id="bI8qGoutM8nx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="4ea03b33-7a7d-4f6a-f599-6f631ce04075"
# A baseline regression
train['price'].median()

# + id="tLM-3iI9NZ8a" colab_type="code" colab={}
# How far off are our baseline predictions on average?
y_test = test['price']
# y_pred = train['price'].median() * len(test)
y_pred = np.full_like(y_test, fill_value=train['price'].median())

# + id="RTcYz4u3Ncr9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="a591d157-69e8-41cb-b55e-861c49ccd10a"
# lots of room for improvement
mean_absolute_error(y_test, y_pred)

# + [markdown] id="3TKXARtwNwUa" colab_type="text"
# ## Linear Regression
#
# **MAE: $624.07**
#
# * Several Features
# * One-Hot Encoding

# + id="fXJk6AJ_O-Rj" colab_type="code" colab={}
# Select features and target
features = ['bedrooms', 'bathrooms', 'fitness_center', 'interest_level', 'longitude']
target = 'price'

# Create train and test dataframes/vectors
X_train = train[features]
y_train = train[target]

X_test = test[features]
y_test = test[target]

# One-Hot Encoder
encoder = ce.OneHotEncoder(use_cat_names=True)
X_train = encoder.fit_transform(X_train)
X_test = encoder.transform(X_test)

# Create & fit the model
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

# + id="2TZzhfgpQQcc" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="b7eebd61-6d38-4118-ac51-0d7ff4855b8e"
mean_absolute_error(y_test, y_pred)

# + id="oqFeY1oKdF6q" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 89} outputId="4453c40a-b1a0-40bd-d989-927e741c4199"
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)

# + [markdown] id="l8YBlqBYQxtG" colab_type="text"
# ## With PCA
#
# **MAE: $674.33**
#
# (not worth it)

# + id="lsW6FrgTQipH" colab_type="code" colab={}
# Imports
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

# + id="SuUZShCGR7aO" colab_type="code" colab={}
# Select features and target
features = ['bedrooms', 'bathrooms', 'fitness_center', 'longitude']
target = 'price'

# Create train and test dataframes/vectors
X_train = train[features]
y_train = train[target]

X_test = test[features]
y_test = test[target]

# Encode Data
# encoder = ce.OneHotEncoder(use_cat_names=True)
# X_train = encoder.fit_transform(X_train)
# X_test = encoder.transform(X_test)

#features = ['bedrooms', 'fitness_center', 'interest_level_high', 'interest_level_medium', 'interest_level_low', 'longitude']

# + id="LAd24WpPVZyQ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e0d7b309-6b46-49ac-d382-3a77b3864662"
df.shape, X_train.shape, X_test.shape

# + id="db9svrzyQ5yO" colab_type="code" colab={}
# Get the data ready to be Standardized
X_train = X_train.loc[:, features].values
X_test = X_test.loc[:, features].values

# + id="6rVUBHVVRKiw" colab_type="code" colab={}
X_train = StandardScaler().fit_transform(X_train)
X_test = StandardScaler().fit_transform(X_test)

# + id="6lMUrnfnR1b0" colab_type="code" colab={}
X_train = pd.DataFrame(data = X_train, columns = features)
X_test = pd.DataFrame(data = X_test, columns = features)

# + id="7bnSmBZeTIfh" colab_type="code" colab={}
# Do some PCA
pca = PCA(n_components=2)
train_PCs = pca.fit_transform(X_train)
test_PCs = pca.fit_transform(X_test)

# + id="DwYQRP-ITV5B" colab_type="code" colab={}
train_PCdf = pd.DataFrame(data = train_PCs, columns = ['PC 1', 'PC 2'])
test_PCdf = pd.DataFrame(data = test_PCs, columns = ['PC 1', 'PC 2'])

# + id="nfj_FgFZUD7w" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="1740b866-d649-42ca-dde4-cf90b7f6db94"
train_PCdf.shape, test_PCdf.shape

# + id="Zt10I27MTZNF" colab_type="code" colab={}
# Create & fit the model
model = LinearRegression()
model.fit(train_PCdf, y_train)
y_pred = model.predict(test_PCdf)

# + id="v3P8gsQuTpJo" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="d561aa89-8556-44e7-975c-163e68f75a09"
mean_absolute_error(y_test, y_pred)

# + [markdown] id="PSAD81lYY9jn" colab_type="text"
# ## Amenities feature
#
# **MAE: $653.52**

# + id="u7Yz3AB5Tx1X" colab_type="code" colab={}
df['amenities'] = True
df['amenities'] = (df['elevator']+
                   df['cats_allowed']+
                   df['dogs_allowed']+
                   df['hardwood_floors']+
                   df['doorman']+
                   df['dishwasher']+
                   df['no_fee']+
                   df['laundry_in_building']+
                   df['fitness_center']+
                   df['laundry_in_unit']+
                   df['roof_deck']+
                   df['outdoor_space']+
                   df['dining_room']+
                   df['high_speed_internet']+
                   df['balcony']+
                   df['swimming_pool']+
                   df['new_construction']+
                   df['terrace'])

# + id="GyH223RFZmYx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 504} outputId="c75b197b-8115-4e42-d1d4-e59916be9c58"
df.head()

# + [markdown] id="LHPEGm_6b5Yx" colab_type="text"
# **gotta redo the train/test split now**

# + colab_type="code" id="y8z38DELb_03" colab={}
# Here's the actual split into TRAIN/TEST
train = df.query('month < 6')
test = df.query('month == 6')

# + colab_type="code" outputId="3e870861-746e-4829-bc80-726d4101a7ec" id="sdSeEvhgb_09" colab={"base_uri": "https://localhost:8080/", "height": 34}
train.shape, test.shape, df.shape

# + [markdown] id="ecRbK7kfcFbv" colab_type="text"
# Now the model

# + id="wP3R-qT0Zota" colab_type="code" colab={}
# Select features and target
features = ['bedrooms', 'bathrooms', 'amenities', 'longitude']
target = 'price'

# Create train and test dataframes/vectors
X_train = train[features]
y_train = train[target]

X_test = test[features]
y_test = test[target]

# Create & fit the model
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

# + id="2e1jN5PDcUh2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="a45a79ab-4380-4ff8-baf4-537877e29bcf"
mean_absolute_error(y_test, y_pred)

StarcoderdataJupyterScriptsDedupFiltered