# ---
# 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="7IXUfiQ2UKj6" colab_type="text"
# Lambda School Data Science, Unit 2: Predictive Modeling
#
# # Regression & Classification, Module 2
#
# ## Assignment
#
# You'll continue to **predict how much it costs to rent an apartment in NYC,** using the dataset from renthop.com.
#
# - [X] Do train/test split. Use data from April & May 2016 to train. Use data from June 2016 to test.
# - [X] Engineer at least two new features. (See below for explanation & ideas.)
# - [X] Fit a linear regression model with at least two features.
# - [X] Get the model's coefficients and intercept.
# - [X] Get regression metrics RMSE, MAE, and $R^2$, for both the train and test data.
# - [X] What's the best test MAE you can get? Share your score and features used with your cohort on Slack!
# - [X] As always, commit your notebook to your fork of the GitHub repo.
#
#
# #### [Feature Engineering](https://en.wikipedia.org/wiki/Feature_engineering)
#
# > "Some machine learning projects succeed and some fail. What makes the difference? Easily the most important factor is the features used." — <NAME>, ["A Few Useful Things to Know about Machine Learning"](https://homes.cs.washington.edu/~pedrod/papers/cacm12.pdf)
#
# > "Coming up with features is difficult, time-consuming, requires expert knowledge. 'Applied machine learning' is basically feature engineering." — <NAME>, [Machine Learning and AI via Brain simulations](https://forum.stanford.edu/events/2011/2011slides/plenary/2011plenaryNg.pdf) 
#
# > Feature engineering is the process of using domain knowledge of the data to create features that make machine learning algorithms work. 
#
# #### Feature Ideas
# - Does the apartment have a description?
# - How long is the description?
# - How many total perks does each apartment have?
# - Are cats _or_ dogs allowed?
# - Are cats _and_ dogs allowed?
# - Total number of rooms (beds + baths)
# - Ratio of beds to baths
# - What's the neighborhood, based on address or latitude & longitude?
#
# ## Stretch Goals
# - [ ] If you want more math, skim [_An Introduction to Statistical Learning_](http://faculty.marshall.usc.edu/gareth-james/ISL/ISLR%20Seventh%20Printing.pdf),  Chapter 3.1, Simple Linear Regression, & Chapter 3.2, Multiple Linear Regression
# - [ ] If you want more introduction, watch [<NAME>, Statistics 101: Simple Linear Regression](https://www.youtube.com/watch?v=ZkjP5RJLQF4)
# (20 minutes, over 1 million views)
# - [ ] Do the [Plotly Dash](https://dash.plot.ly/) Tutorial, Parts 1 & 2.
# - [ ] Add your own stretch goal(s) !

# + id="o9eSnDYhUGD7" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="04afc87e-a4b0-498c-d884-e7f7675f5a82"
# If you're in Colab...
import os, sys
in_colab = 'google.colab' in sys.modules

if in_colab:
    # Install required python packages:
    # pandas-profiling, version >= 2.0
    # plotly, version >= 4.0
    # !pip install --upgrade pandas-profiling plotly
    
    # Pull files from Github repo
    os.chdir('/content')
    # !git init .
    # !git remote add origin https://github.com/LambdaSchool/DS-Unit-2-Regression-Classification.git
    # !git pull origin master
    
    # Change into directory for module
    os.chdir('module1')

# + id="ipBYS77PUwNR" colab_type="code" colab={}
# Ignore this Numpy warning when using Plotly Express:
# FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
import warnings
warnings.filterwarnings(action='ignore', category=FutureWarning, module='numpy')

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

# Read New York City apartment rental listing data
df = pd.read_csv('../data/renthop-nyc.csv')
assert df.shape == (49352, 34)

# Remove the most extreme 1% prices,
# the most extreme .1% latitudes, &
# the most extreme .1% longitudes
df = df[(df['price'] >= np.percentile(df['price'], 0.5)) & 
        (df['price'] <= np.percentile(df['price'], 99.5)) & 
        (df['latitude'] >= np.percentile(df['latitude'], 0.05)) & 
        (df['latitude'] < np.percentile(df['latitude'], 99.95)) &
        (df['longitude'] >= np.percentile(df['longitude'], 0.05)) & 
        (df['longitude'] <= np.percentile(df['longitude'], 99.95))]

# + id="7gNLC7EurHFd" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 501} outputId="458a306f-d3ce-4f7b-dcbc-355ee5c3963d"
df.head()

# + id="cnt1DCOhbNKV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 501} outputId="f1a9aa39-d11f-418a-8d2f-3fd89c5c2876"
df.sort_values(by='price', ascending=True).tail()

# + id="-2JVRxLQrM_h" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="0d84704f-2db8-47c7-fff8-0e7286e9ce74"
df.shape

# + id="QxfHSisgrN0R" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 304} outputId="532de943-ea7c-4da8-8a34-0c7c0f843129"
df.describe()

# + id="Ku8R3KvBrPTI" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="b65ce4d1-89a9-41b1-eb19-c3416f6fa713"
df.interest_level.unique()

# + id="9z7oAbKirsIv" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 501} outputId="c19d2549-e7c0-4e97-d3cf-382d74e57c54"
interest = {'low':1,'medium':'2','high':3}
df['interest_level_num'] = df.interest_level.map(interest)
df.head()

# + id="OjUXaAWbsQl1" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="ced0a1bd-5c9c-48ee-fb02-2d9a609b7d22"
df.display_address.nunique()

# + id="DpRyCdPlt414" colab_type="code" colab={}
am = df.columns[10:34]



# + id="5bhurSVQxqn4" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 622} outputId="ba4b4cc7-0990-47d9-b18a-e8a2151069ec"
df.dtypes

# + id="fnvMwPPoxX_1" colab_type="code" colab={}
import numpy as np

# + id="tLyjmVv9w46S" colab_type="code" colab={}


# + id="2KG97hb0uW5S" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 501} outputId="b7e722c5-c901-47dc-cb05-9a62dd2b6be2"
df['amin'] = df[am].sum(axis=1)
df.head()

# + id="sT80PnkOsgMN" colab_type="code" colab={}
import numpy as np
from sklearn.model_selection import train_test_split

X = df[['bedrooms','bathrooms','interest_level_num','amin','latitude','longitude']]
y = df['price']

# + id="S-fKPeC8zM2j" colab_type="code" colab={}
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

# + id="B2s3KHDjzSB5" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="db338782-abdb-4204-f1b5-0be7304cf35c"
from sklearn.linear_model import LinearRegression

model = LinearRegression()

model.fit(X_train,y_train)

# + id="XHWRCWTHX5hL" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 50} outputId="696f3844-0025-492b-adba-7e90a7ddbf4c"
model.coef_

# + id="WfGxWjpEYRvY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="8ce97a28-1d32-495e-d8d6-668ff7dbe3de"
model.intercept_

# + id="q9tTBZeo07rq" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 501} outputId="846dc91b-04fa-47f5-e557-10b7e11b06c1"
df.head()

# + id="1ntpAP8_ZTiY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="9aefe9c7-9bef-40ea-e093-a2226ab0a3fd"
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(18, 18))
sns.scatterplot(y=df['longitude'],x=df['latitude'],hue='price',size='price',data=df, sizes=(10,200))

# + id="Duw2LQT91IxE" colab_type="code" colab={}
y_pred = model.predict(X_test)

# + id="VWhJoMy61YWn" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="17f1cd75-ce21-47a5-c56c-4e8dd5d68a7b"
from sklearn.metrics import mean_squared_error
from math import sqrt
from sklearn.metrics import r2_score
mse = sqrt(mean_squared_error(y_test, y_pred))
mse

# + id="PVx98N1xV84s" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="5929229e-5a09-46d2-aa8a-a2c85569a9d5"
r2_score(y_test,y_pred)

# + id="sHcVNvOsXTaZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6f3ad2b7-e30c-42d9-a5c9-57a8aa19813e"
y_pred_train = model.predict(X_train)
r2_score(y_train,y_pred_train)


# + id="h7D5pVSXXljl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="1e0faf1b-ba27-4c5e-dbb5-5dcc31084bf8"
def rmse(predictions, targets):
    return np.sqrt(((predictions - targets) ** 2).mean())

rmse(y_pred,y_stest)

#RMSE for the Test data

# + id="Jid8jfDvcpd-" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e760d26f-1f40-4e30-bc50-5713312675b3"
rmse(y_pred_train,y_train)

#RMSE for the training data

# + id="IX-yj2lbcvXv" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="b31cf74c-f5b2-4232-e640-60334fdc6248"


sqrt(mean_squared_error(y_train,y_pred_train))

# + id="nDDn_xHcc-5P" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="4cba6bed-6b58-44f2-de24-5ec85330447f"
sqrt(mean_squared_error(y_test,y_pred))

# + id="nzcgfpP3dXem" colab_type="code" colab={}
#checking with Sklearn

# + id="Cl0N3Fh8dawt" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 50} outputId="371a4c00-0c61-4626-e4af-9d26fd475df5"
from sklearn.metrics import mean_absolute_error

print('MAE TEST ' + str(mean_absolute_error(y_test,y_pred)))
print('MAE TRAIN ' + str(mean_absolute_error(y_train,y_pred_train)))

# + id="UUwfrYMqeQoh" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered