# ---
# 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="MulQNrivr1mE"
# # Introduction
#

# + [markdown] id="atrX8QbOt7eP"
# ## Hack With CW
# Hack With CW is a community- focused Virtual Hackathon, designed especially for the needs of the community itself. Whether you are a beginner or expert ,here is a perfect chance to show your skills and witness a competitive yet inclusive developer community around it. In light of the CoronaVirus outbreak, your safety and well being are our primary concern. Hence all the activities Related to Hack With CW like workshops,talks will be conducted online.
#
# ## link : https://hackwithcw.tech/
#

# + [markdown] id="04WEFxThuS94"
# ## Problem Statement
# ### id: 
# 017
# ### Title: 
# Machine Learning based House Price Prediction System
# ### Category: 
# Smart City
# ### Description: 
# Normally house price is decided based on negotiation between seller and buyer. But there is a better way to decide price of a house. There are many factors which affect the price. For example, locality, average price in the locality, availability of transport, availability of health-care nearby, distance from schools, offices etc.. In US, nowadays there is a trend to use algorithms to decide price of a house. There is need of a machine learning algorithm which takes input different decisive parameters and outputs the estimated price.
#
# ## Model developed by : <NAME>

# + [markdown] id="Lytejfdqtz_3"
# # House Price Prediction Model - <NAME>

# + [markdown] id="A9l0oJ5TwFoG"
# ## Importing Modules 

# + colab={"base_uri": "https://localhost:8080/"} id="CM500zYzt3_0" outputId="ead33e9e-ef20-4a44-a5c0-16fda6264089"
# !pip install turicreate

# + id="XhYXsSjtu0cm"
import turicreate as tc
from turicreate import SFrame

# + colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 38} id="2r8Ps6zivJdW" outputId="4cdb956f-4aac-4b47-dd84-e90ed7df0ec1"
from google.colab import files
uploaded=files.upload()

# + colab={"base_uri": "https://localhost:8080/"} id="AqK2_bQivfog" outputId="2624f304-57a7-489c-9b77-f1636235bfac"
# !unzip home_data_zip.sframe.zip

# + [markdown] id="j1Do_4tSwQTl"
# ## Loading data

# + id="fp2Vj1i6v0Sl"
sales_data=SFrame('home_data.sframe')

# + colab={"base_uri": "https://localhost:8080/", "height": 667} id="C6ua0W6Fv_cs" outputId="f55406af-f08e-44e7-fe42-ad83dd2c9f84"
sales_data.head()

# + [markdown] id="NU13vye1wnfm"
# ## Exploring Data - Data Analysis

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="xqupcwiuwY6n" outputId="8375282c-df67-42c7-feb2-bc941932adfd"
sales_data.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 825} id="rVGBLychwtaj" outputId="ec78d0c8-0b81-4746-f397-d1af490ce96a"
tc.show(sales_data[1:5000]['sqft_living'],sales_data[1:5000]['price'])

# + [markdown] id="IeQDJfPsyEkg"
# ## Spliting the Data into Testing and Training Set

# + id="WWyRzbiHyK9I"
training_set,test_set = sales_data.random_split(.8,seed=0)

# + [markdown] id="5-dKm0cixScW"
# ## Model 1 : Simple Linear Regression Model 
# This model predicts price of the house only from square feet of the house 

# + colab={"base_uri": "https://localhost:8080/", "height": 306} id="qzWY5dUTxvtS" outputId="dd9dda9f-726a-4dc5-e6cb-7529cebee03c"
sqft_model_1 = tc.linear_regression.create(training_set,target='price',features=['sqft_living'],max_iterations=20)

# + [markdown] id="2VE1rnHKz1o2"
# ### Evaluating the quality of model 1 : sqft_model_1

# + colab={"base_uri": "https://localhost:8080/"} id="ypOdkZO7z9aB" outputId="d49147cd-3c3b-4bbd-bc41-2043a4cda503"
print (test_set['price'].mean())

# + colab={"base_uri": "https://localhost:8080/"} id="T2Aj5Icy0H6a" outputId="035b1983-a421-4df5-910f-1fcf0efe8117"
print (sqft_model_1.evaluate(test_set))

# + colab={"base_uri": "https://localhost:8080/"} id="Y-8nFKnN5WlB" outputId="27302e1c-7f49-42bc-81cf-546ba972d681"
sqft_model_1.summary()

# + colab={"base_uri": "https://localhost:8080/", "height": 93} id="BBSUquF25uOt" outputId="ae0934aa-1b8f-49f3-ce3e-fcdec8d22c53"
sqft_model_1.coefficients

# + colab={"base_uri": "https://localhost:8080/", "height": 310} id="p7Za1fXK6FVW" outputId="4a0601cc-7f07-4cab-e082-9c5abb72f26b"
import matplotlib.pyplot as plt
# %matplotlib inline
plt.plot(test_set['sqft_living'],test_set['price'],'.',
        test_set['sqft_living'],sqft_model_1.predict(test_set),'*')

# + [markdown] id="NWTt6M1XlmxG"
# ### Saving Model - sqft_model_1 

# + id="vqYe0161viUN"
sqft_model_1.save('/content/drive/MyDrive/Hack-with-cw-Hackathon/Saved-Models/sqft_model_1_saved')
# Can be any path 
# note that the model is saved as directory(folder) and not as file , hence "sqft_model_1_saved" is a folder containing the model "sqft_model_1"
#in it as different files 

# + colab={"base_uri": "https://localhost:8080/", "height": 52} id="Xee-wRhCxO1n" outputId="ff8dbd63-217e-4710-f787-3d9deefeb24b"
loaded_sqft_model_1=tc.load_model('/content/drive/MyDrive/Hack-with-cw-Hackathon/Saved-Models/sqft_model_1_saved')
'''
the saved model can be loaded from the path mentioned in the quotes(' ')
and also note that if we want to access one model from another colab notebook ,, make sure to mount the drive and then 
use the same above thing
'''

# + [markdown] id="TnS64a1F_0Zs"
# ## Model 2 - More featured Linear Regression Model 

# + [markdown] id="KtEF5CCJOdPK"
# ### data used

# + colab={"base_uri": "https://localhost:8080/", "height": 701} id="Asdv8BTqJejp" outputId="99f142ff-09d1-4709-f2b8-5d9428939b9e"
sales_data

# + [markdown] id="btrPe-TCOkKw"
# ### features

# + id="SfwO9E-UJk6k"
my_features = ['bedrooms','bathrooms','sqft_living','sqft_lot','floors','condition','grade','zipcode']

# + [markdown] id="L2yn_IxhOpao"
# ### Visualizing the Data

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="3Ws_0NfnLwMp" outputId="a610e27f-9583-4da6-f304-9b00bfa6d630"
sales_data[my_features].show()

# + colab={"base_uri": "https://localhost:8080/", "height": 825} id="GpF-6Kz9L3Ew" outputId="ed505868-303f-494f-d73e-2daf4d77ac4a"
tc.show(sales_data['zipcode'],sales_data['price'])

# + [markdown] id="BnFNMFvxOxKw"
# ### Building a Model

# + colab={"base_uri": "https://localhost:8080/", "height": 306} id="kbGfG_uwO0c1" outputId="dc72757f-6b8d-417e-e572-dae1987f4512"
my_features_model_2 = tc.linear_regression.create(training_set,target='price',features=my_features)

# + [markdown] id="SDe-JOOPPDEP"
# ### Making Predictions using more featured model

# + [markdown] id="W4_NwflKPQYi"
# #### using data row from the dataset

# + id="KDTBgwDwPI-G"
house1 = sales_data[sales_data['id']=='5309101200']

# + colab={"base_uri": "https://localhost:8080/", "height": 154} id="CnieA_i9PNh-" outputId="bc66ac29-b275-4669-e0d8-c984b8350a74"
house1

# + colab={"base_uri": "https://localhost:8080/"} id="n2VbpzGKPO1O" outputId="0af0fb13-ae7d-4963-8ebc-6edfa9f4b68a"
print (house1['price'])

# + colab={"base_uri": "https://localhost:8080/"} id="Psfvg9kjPYfg" outputId="c257182b-8994-4867-8251-92e06ba7d8e0"
print (my_features_model_2.predict(house1))

# + [markdown] id="bQmZrYApPnYw"
# #### Predicting using own entries

# + id="D6wZTt11PrNW"
houseData = {'bedrooms':[8], 
              'bathrooms':[25], 
              'sqft_living':[50000], 
              'sqft_lot':[225000],
              'floors':[4], 
              'zipcode':['98039'], 
              'condition':[10], 
              'grade':[10],
              'waterfront':[1],
              'view':[4],
              'sqft_above':[37500],
              'sqft_basement':[12500],
              'yr_built':[1994],
              'yr_renovated':[2010],
              'lat':[47.627606],
              'long':[-122.242054],
              'sqft_living15':[5000],
              'sqft_lot15':[40000]}

# + colab={"base_uri": "https://localhost:8080/"} id="0YF5jKWNP2hm" outputId="2a349889-0303-4a1a-ed1e-5b9ab3438d6b"
print (my_features_model_2.predict(tc.SFrame(houseData)))

# + [markdown] id="97zw43phegYZ"
# ### Evaluating the Features Model 2

# + colab={"base_uri": "https://localhost:8080/"} id="TylYxVUDemCn" outputId="6b61f3ab-28fd-488b-968a-a322e7ee1e76"
my_features_model_2.evaluate(test_set)

# + colab={"base_uri": "https://localhost:8080/", "height": 279} id="xlCZbBzdfUmg" outputId="afbdca99-de8a-4ca7-f870-d1339050fd6c"
my_features_model_2.coefficients

# + colab={"base_uri": "https://localhost:8080/"} id="fmJ9GO_ke2AG" outputId="680fe2b5-7811-4c41-d56f-960185f560cc"
my_features_model_2.summary()

# + [markdown] id="itaFTmspTtYo"
# ### Visualizing the featured model 2

# + colab={"base_uri": "https://localhost:8080/", "height": 310} id="ZESG6PmFT3ce" outputId="25973d09-6740-4e7d-c3af-ec9826c612e6"
import matplotlib.pyplot as plt
# %matplotlib inline
plt.plot(test_set['sqft_living'],test_set['price'],'.',
        test_set['sqft_living'],my_features_model_2.predict(test_set),'-')

# + [markdown] id="JoS8eRjxXBkH"
# ### Saving and Loading the Featured Model 2

# + id="r2L16rlLXWSl"
my_features_model_2.save('/content/drive/MyDrive/Hack-with-cw-Hackathon/Saved-Models/my_featured_model_2_saved')

# + id="PGyOqKFvX2U3"
loaded_my_features_model_2=tc.load_model('/content/drive/MyDrive/Hack-with-cw-Hackathon/Saved-Models/my_featured_model_2_saved')

# + [markdown] id="A_PNAq7lU_0g"
# ## Model 3 - Random Forest Regressor Model

# + [markdown] id="0VEEzoUZbNJC"
# ### Data used

# + colab={"base_uri": "https://localhost:8080/", "height": 701} id="KwO4y3rHaU1a" outputId="6bf4a920-017b-4fa5-accb-ec292b941ce9"
sales_data

# + [markdown] id="uzjwSfgObT6Y"
# ### Features

# + id="wTDR9gVbbSLB"
my_features = ['bedrooms','bathrooms','sqft_living','sqft_lot','floors','condition','grade','zipcode']

# + [markdown] id="EGauu2wMbeJ4"
# ### Visualizing Data

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="qez5K_ogbl1c" outputId="d6980d9e-c6fa-42c2-c1de-8ba06feebaf3"
sales_data[my_features].show()

# + [markdown] id="SqfEqsPxb1NF"
# ### Random Forest Regressor Model Building

# + colab={"base_uri": "https://localhost:8080/", "height": 391} id="IuWXVbs-btzl" outputId="8665a77e-7aca-4a25-da9d-8fcbd4c83895"
random_forest_regressor_model_3= tc.random_forest_regression.create(training_set,target='price',features=my_features,max_iterations=360)

# + [markdown] id="XzUicilBfgbi"
# ### Predicting using Random Reg model 3

# + [markdown] id="2xuhqF1hgBkn"
# #### using data from the data set

# + id="AeTLUbuRfl7_"
house_value_model_3 = sales_data[sales_data['id']=='5309101200']

# + colab={"base_uri": "https://localhost:8080/", "height": 154} id="rASMNJ1ZgUIO" outputId="5095a71f-c94d-4cfd-be3f-a8f19015d131"
house_value_model_3

# + colab={"base_uri": "https://localhost:8080/"} id="GMnifDBugjV-" outputId="226e96cc-2786-4d52-f7aa-0c9cc0379707"
print(house_value_model_3['price'])

# + colab={"base_uri": "https://localhost:8080/"} id="CEvpeQxkgor_" outputId="6de0dfd1-6824-4de7-c24b-48e344644757"
random_forest_regressor_model_3.predict(house_value_model_3)

# + [markdown] id="7a990kIbhQk4"
# #### Predicting using own value

# + id="7d9oQoLihT_2"
houseData_model_3 = {'bedrooms':[9], 
              'bathrooms':[25], 
              'sqft_living':[50000], 
              'sqft_lot':[225000],
              'floors':[5], 
              'zipcode':['98039'], 
              'condition':[10], 
              'grade':[10],
              'waterfront':[1],
              'view':[4],
              'sqft_above':[37500],
              'sqft_basement':[12500],
              'yr_built':[1994],
              'yr_renovated':[2010],
              'lat':[47.627606],
              'long':[-122.242054],
              'sqft_living15':[5000],
              'sqft_lot15':[40000]}

# + colab={"base_uri": "https://localhost:8080/"} id="X6TJoLOvik9p" outputId="006807ac-5e5c-489c-a295-97b36bbe069f"
random_forest_regressor_model_3.predict(tc.SFrame(houseData_model_3))

# + [markdown] id="9RBh1pHNddVR"
# ### Evaluating the random reg model 3

# + colab={"base_uri": "https://localhost:8080/"} id="GQHPXNJ5digL" outputId="dd61b393-d45f-4eb5-c6c1-9f5b5430df36"
print(test_set['price'].mean())

# + colab={"base_uri": "https://localhost:8080/"} id="dGz0ovsWdsOF" outputId="4e20a9ca-d243-4181-cb92-44b85dc5408c"
random_forest_regressor_model_3.evaluate(test_set)

# + colab={"base_uri": "https://localhost:8080/"} id="1UIZi9NHeGH0" outputId="976b51d8-8a2e-4e6b-be92-f8a3a9ef57b1"
random_forest_regressor_model_3.summary()

# + [markdown] id="sdUh1JitftA-"
# ### Visualizing Random Forest Reg Model 3 

# + colab={"base_uri": "https://localhost:8080/", "height": 310} id="StUIOmKyfyYS" outputId="f75e0020-5dea-4e3b-df10-b9f2664be3ee"
import matplotlib.pyplot as plt
# %matplotlib inline
plt.plot(test_set['sqft_living'],test_set['price'],'.',
        test_set['sqft_living'],random_forest_regressor_model_3.predict(test_set),'-')

# + [markdown] id="qFnwLlMWjJvm"
# ### Saving and Loading Model - Random Forest Regressor 3

# + id="NklxHziLjQoY"
random_forest_regressor_model_3.save('/content/drive/MyDrive/Hack-with-cw-Hackathon/Saved-Models/random_forest_regressor_model3_saved')

# + id="9UxRqDWajgqf"
loaded_random_forest_regressor_model_3=tc.load_model('/content/drive/MyDrive/Hack-with-cw-Hackathon/Saved-Models/random_forest_regressor_model3_saved')

# + [markdown] id="5LlyCxyIQCGL"
# ## Comparing Models 
#
# 1.   Square feet Linear Regressor Model 1
# 2.   My Featured Linear Regressor Model 2
# 3.   Random Forest Regressor Model 3
#
#
#
#

# + colab={"base_uri": "https://localhost:8080/"} id="f14rD96FQDPt" outputId="9997f4b2-9e8a-44e7-89f0-2d7ed190e80c"
my_features

# + [markdown] id="K-FxloALS1_u"
# ### Metrics 

# + colab={"base_uri": "https://localhost:8080/"} id="HM6764AwQM3t" outputId="07264c00-1768-4d7e-8f2a-003854be7056"
print (" Metrics for Square Feet             Model 1 -->",sqft_model_1.evaluate(test_set))
print (" Metrics for Featured                Model 2 -->",my_features_model_2.evaluate(test_set))
print (" Metrics for Random Forest Regressor Model 3 -->",random_forest_regressor_model_3.evaluate(test_set))

# + [markdown] id="xAHgaD1MS_tl"
# ###Summary of Models

# + colab={"base_uri": "https://localhost:8080/"} id="aaWYHvdgRyon" outputId="5a02e2d5-c94b-44c0-ea67-1de21843d05c"
sqft_model_1.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="n53sStQkR0xd" outputId="1e264a6c-c80b-4a0b-8ce4-19d1d0d52bad"
my_features_model_2.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="lTPSLPuUlX3q" outputId="e8580b85-8089-4c5b-e017-65cd2fb3285f"
random_forest_regressor_model_3.summary()

# + [markdown] id="V-MByKJ9UUh3"
# ### Plotting the actual price Vs Predicted Price

# + [markdown] id="46J_QZX0UlSO"
# #### Sqft model 1

# + colab={"base_uri": "https://localhost:8080/", "height": 310} id="BGM_fDynSsxF" outputId="fb02de7e-ec0f-4147-f6e9-49d70054900f"
import matplotlib.pyplot as plt
# %matplotlib inline
plt.plot(test_set['sqft_living'],test_set['price'],'.',
        test_set['sqft_living'],sqft_model_1.predict(test_set),'*')

# + [markdown] id="HxDnLJqvUn4l"
# #### featured model 2

# + colab={"base_uri": "https://localhost:8080/", "height": 310} id="PfFQZclhUrr5" outputId="4a2a6936-cd69-4000-8c57-ff18ade58102"
import matplotlib.pyplot as plt
# %matplotlib inline
plt.plot(test_set['sqft_living'],test_set['price'],'.',
        test_set['sqft_living'],my_features_model_2.predict(test_set),'*')

# + [markdown] id="yypwTrdSlrBV"
# #### Random Forest Regressor Model 3 

# + colab={"base_uri": "https://localhost:8080/", "height": 310} id="xLdvsJcllvo5" outputId="61d7cbe4-6bcb-405c-9335-031ff8821192"
import matplotlib.pyplot as plt
# %matplotlib inline
plt.plot(test_set['sqft_living'],test_set['price'],'.',
        test_set['sqft_living'],random_forest_regressor_model_3.predict(test_set),'*')

# + [markdown] id="wOK7_pPuj835"
# # <NAME>

# + colab={"base_uri": "https://localhost:8080/", "height": 17} id="li5TmvA7kANW" outputId="3270a109-3423-4f32-b9dd-761157dc7a78"
from google.colab import files
files.download('/content/drive/MyDrive/Hack-with-cw-Hackathon/Saved-Models')

StarcoderdataJupyterScriptsDedupFiltered