# ---
# 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/1985shree/Data-science-Zoomcamp-projects/blob/main/MML_zoomcamp_week3_classification_churn_prediction.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": "OK"}}, "base_uri": "https://localhost:8080/", "height": 75} id="BdC-qKp8ATZj" outputId="0064c671-8632-41dc-9761-92e9f0af5157"
#uploading ipynb from local folder to google drive/colab folder
from google.colab import files
uploaded = files.upload()

# + id="xP1KKVOPDaOo"
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt

# + colab={"base_uri": "https://localhost:8080/", "height": 417} id="dPHHaJbIenHL" outputId="9e5875d6-1040-488d-eb53-31afa7d4049a"
import io

df = pd.read_csv(io.BytesIO(uploaded['AB_NYC_2019.csv']))
df.head()

# + id="ZS4z7t9mgy9J"
data = df [['neighbourhood_group', 'room_type','latitude','longitude','price','minimum_nights', 'number_of_reviews', 'reviews_per_month', 'calculated_host_listings_count', 'availability_365']]

# + colab={"base_uri": "https://localhost:8080/", "height": 363} id="85naq2KmhIPR" outputId="7c3180a4-b533-4bc3-e2fd-376b6706d1ba"
data.head(10).T

# + colab={"base_uri": "https://localhost:8080/", "height": 300} id="U4tigdOrhbJ6" outputId="91db9f09-ce0d-449b-e19a-d16cda8dfb69"
data.describe().T

# + colab={"base_uri": "https://localhost:8080/"} id="4GV07THG6k2X" outputId="2a7d6606-5b5d-4ec6-dc79-15a1138204f9"
pd.value_counts(data.neighbourhood_group)

# + [markdown] id="34qSPGt37B-T"
# Q1 Answer: Manhattan

# + [markdown] id="VFbiGY0gB-aq"
# **setting up validation framework** 

# + colab={"base_uri": "https://localhost:8080/"} id="HE7jVp5K9Pd8" outputId="cbd4739a-6a10-4b97-9cf3-3de4035a8abc"
# !pip install scikit-learn


# + id="S5Fpwo4lCORK"
from sklearn.model_selection import train_test_split

# + colab={"base_uri": "https://localhost:8080/"} id="S66KyzR6gyBj" outputId="6545e681-619e-4f73-c25b-a213dae0a9f5"
data.copy()

df_full_train, df_test = train_test_split(data, test_size = 0.2, random_state = 42)
df_train, df_val = train_test_split(df_full_train, test_size = 0.25, random_state = 42)
print( len (df_train), len(df_val), len(df_test))

df_train.reset_index(drop = True)
df_val.reset_index(drop = True)
df_test.reset_index(drop = True)

y_train = df_train['price']
y_val = df_val['price']
y_test = df_test['price']

# + id="MBCNeiRkL-39"
del(df_train['price'])
del(df_val['price'])
del(df_test['price'])

# + id="EKVVDXsHL-9J"


# + [markdown] id="jBuWekm5CRsf"
# **EDA**

# + id="FDHTo7RJCUln" colab={"base_uri": "https://localhost:8080/", "height": 269} outputId="7969410c-83df-4f1c-9508-2189d3ed1be8"


# + colab={"base_uri": "https://localhost:8080/", "height": 504} id="Tq8zNXrCON0Z" outputId="9726e5ec-a49e-4f1d-b4d4-f49f9fd3658e"


# + id="RplxKtS_RVcz"
above_average_train = (y_train >= 152).astype(int)
above_average_val =(y_val >= 152).astype(int) 
above_average_test = (y_test >= 152).astype(int)

# + colab={"base_uri": "https://localhost:8080/"} id="zz-WcxZvVLKZ" outputId="5e2de528-b364-4462-d212-f8bfe8a22f6f"
df_train.dtypes

# + id="g4FDXxW9Ywe7"


# + id="loLPps26Y6eV"


# + [markdown] id="jQllWI8wCZFK"
# **feature importance/feature engineering: churn rate and risk ratio**

# + id="m-QdVyYXCjPY"


# + id="I2qhRGGXalbe"


# + [markdown] id="ylDjKOqfbvgb"
# Answer to Q3 : room type

# + [markdown] id="qHVqpSQqCjf5"
# **feature importance: mutual information**

# + id="TQsXMcqlCz_Z"
categorical = ['neighbourhood_group', 'room_type']
df_train_categorical = df_train[categorical]

# + colab={"base_uri": "https://localhost:8080/"} id="sdPuc2dXcHTy" outputId="18e0a338-aeb3-47b3-ecbf-99cea5cf22af"
from sklearn.metrics import mutual_info_score
neighbourhood_score = mutual_info_score(df_train_categorical.neighbourhood_group, y_train )
room_type_score = mutual_info_score(df_train_categorical.room_type, y_train )
print (round(neighbourhood_score, 2), round(room_type_score, 2))


# + [markdown] id="KIVzPJDjC0fJ"
# **feature importance: correlation**

# + id="5ILxH_vzC58K" colab={"base_uri": "https://localhost:8080/", "height": 269} outputId="8e75ca37-af49-4abc-d8d9-bb2469efd32e"
#correlation matrix
train_corr = df_train.corr()
train_corr

# + colab={"base_uri": "https://localhost:8080/", "height": 504} id="DgwqmIwXcQsS" outputId="6ea4626f-8612-461b-98f3-885d2e16fd02"
#visualizing correlation matrix
import seaborn as sns
ax = sns.heatmap(train_corr, vmin = -1, vmax = 1, center = 0, cmap=sns.diverging_palette(20, 500, n=200), square=True)

ax.set_xticklabels(ax.get_xticklabels(),rotation=45, horizontalalignment='right')

# + [markdown] id="DzMRPKWhcZxy"
# reviews per month, number of reviews

# + [markdown] id="cAWVvwbHcVHZ"
#

# + [markdown] id="iCt9OmK9C-SP"
# **one-hot encoding**

# + id="QjKxFMq7DB0g" colab={"base_uri": "https://localhost:8080/"} outputId="179357a0-39a6-4f24-f72e-30165eec885d"
from sklearn.feature_extraction import DictVectorizer
train_dicts = df_train.to_dict(orient = 'records')
train_dicts[0]


# + id="CNR5Y4KPfX1y"
dv = DictVectorizer()
X_train = dv.fit_transform(train_dicts)

# + id="VryG9CPKgveJ"
val_dicts = train_dicts = df_val.to_dict(orient = 'records')

# + id="b52SIiGUgsLR"
X_val = dv.transform(val_dicts)

# + id="hvNY6PRRDEAh" colab={"base_uri": "https://localhost:8080/"} outputId="35820ae4-27c4-4602-f29d-ddf293d24ba3"
print(X_train[0])

# + [markdown] id="TWxbXez5DEgK"
# **logistic regression**

# + id="jTnPclcjDHEZ"


# + [markdown] id="TdAMFPdoDKZY"
# **training logistic regression with scikit-learn**

# + id="rkxikA6nDTQq"


# + [markdown] id="JCjXYPg0DhtC"
# **model interpretation**

# + id="Apps-5uRDpIo"


# + [markdown] id="elnw5y0zDpV4"
# **using the model**

StarcoderdataJupyterScriptsDedupFiltered