"""
<h1>Table of Contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><ul class="toc-item"><li><span><a href="#Loading-Libraries" data-toc-modified-id="Loading-Libraries-0.1"><span class="toc-item-num">0.1&nbsp;&nbsp;</span>Loading Libraries</a></span></li></ul></li><li><span><a href="#Descriptive-Analysis" data-toc-modified-id="Descriptive-Analysis-1"><span class="toc-item-num">1&nbsp;&nbsp;</span>Descriptive Analysis</a></span><ul class="toc-item"><li><span><a href="#Trailer-of-data" data-toc-modified-id="Trailer-of-data-1.1"><span class="toc-item-num">1.1&nbsp;&nbsp;</span>Trailer of data</a></span><ul class="toc-item"><li><span><a href="#Shape" data-toc-modified-id="Shape-1.1.1"><span class="toc-item-num">1.1.1&nbsp;&nbsp;</span>Shape</a></span></li><li><span><a href="#Column-Names" data-toc-modified-id="Column-Names-1.1.2"><span class="toc-item-num">1.1.2&nbsp;&nbsp;</span>Column Names</a></span></li><li><span><a href="#Converting-haves-and-haves_not" data-toc-modified-id="Converting-haves-and-haves_not-1.1.3"><span class="toc-item-num">1.1.3&nbsp;&nbsp;</span>Converting haves and haves_not</a></span></li><li><span><a href="#General-Stats" data-toc-modified-id="General-Stats-1.1.4"><span class="toc-item-num">1.1.4&nbsp;&nbsp;</span>General Stats</a></span></li></ul></li><li><span><a href="#KDE---Normal" data-toc-modified-id="KDE---Normal-1.2"><span class="toc-item-num">1.2&nbsp;&nbsp;</span>KDE - Normal</a></span></li><li><span><a href="#Histogram" data-toc-modified-id="Histogram-1.3"><span class="toc-item-num">1.3&nbsp;&nbsp;</span>Histogram</a></span></li><li><span><a href="#Box-PLot" data-toc-modified-id="Box-PLot-1.4"><span class="toc-item-num">1.4&nbsp;&nbsp;</span>Box PLot</a></span></li><li><span><a href="#Scatter-Plot" data-toc-modified-id="Scatter-Plot-1.5"><span class="toc-item-num">1.5&nbsp;&nbsp;</span>Scatter Plot</a></span></li><li><span><a href="#Heat-Map" data-toc-modified-id="Heat-Map-1.6"><span class="toc-item-num">1.6&nbsp;&nbsp;</span>Heat Map</a></span></li></ul></li><li><span><a href="#Predcitive" data-toc-modified-id="Predcitive-2"><span class="toc-item-num">2&nbsp;&nbsp;</span>Predcitive</a></span><ul class="toc-item"><li><span><a href="#OLS-v1-adding-only-nominal-variables" data-toc-modified-id="OLS-v1-adding-only-nominal-variables-2.1"><span class="toc-item-num">2.1&nbsp;&nbsp;</span>OLS v1 adding only nominal variables</a></span></li><li><span><a href="#OLS-v2-adding-all-variables" data-toc-modified-id="OLS-v2-adding-all-variables-2.2"><span class="toc-item-num">2.2&nbsp;&nbsp;</span>OLS v2 adding all variables</a></span></li><li><span><a href="#Stepwise-OLS" data-toc-modified-id="Stepwise-OLS-2.3"><span class="toc-item-num">2.3&nbsp;&nbsp;</span>Stepwise OLS</a></span></li><li><span><a href="#PCA" data-toc-modified-id="PCA-2.4"><span class="toc-item-num">2.4&nbsp;&nbsp;</span>PCA</a></span></li><li><span><a href="#Clustering" data-toc-modified-id="Clustering-2.5"><span class="toc-item-num">2.5&nbsp;&nbsp;</span>Clustering</a></span></li><li><span><a href="#Verifying-clusters" data-toc-modified-id="Verifying-clusters-2.6"><span class="toc-item-num">2.6&nbsp;&nbsp;</span>Verifying clusters</a></span></li><li><span><a href="#Wordcloud" data-toc-modified-id="Wordcloud-2.7"><span class="toc-item-num">2.7&nbsp;&nbsp;</span>Wordcloud</a></span></li></ul></li><li><span><a href="#Prescriptive" data-toc-modified-id="Prescriptive-3"><span class="toc-item-num">3&nbsp;&nbsp;</span>Prescriptive</a></span><ul class="toc-item"><li><span><a href="#Q:-Does-Rating-change-evry-year-?" data-toc-modified-id="Q:-Does-Rating-change-evry-year-?-3.1"><span class="toc-item-num">3.1&nbsp;&nbsp;</span>Q: Does Rating change evry year ?</a></span><ul class="toc-item"><li><span><a href="#Normal-Distribution" data-toc-modified-id="Normal-Distribution-3.1.1"><span class="toc-item-num">3.1.1&nbsp;&nbsp;</span>Normal Distribution</a></span></li><li><span><a href="#Boxplot" data-toc-modified-id="Boxplot-3.1.2"><span class="toc-item-num">3.1.2&nbsp;&nbsp;</span>Boxplot</a></span></li><li><span><a href="#Standard-Deviation" data-toc-modified-id="Standard-Deviation-3.1.3"><span class="toc-item-num">3.1.3&nbsp;&nbsp;</span>Standard Deviation</a></span></li><li><span><a href="#Mean-Rating-over-time" data-toc-modified-id="Mean-Rating-over-time-3.1.4"><span class="toc-item-num">3.1.4&nbsp;&nbsp;</span>Mean Rating over time</a></span></li></ul></li><li><span><a href="#Q:-Which-country-and-country-makes-the-best-chcolate?" data-toc-modified-id="Q:-Which-country-and-country-makes-the-best-chcolate?-3.2"><span class="toc-item-num">3.2&nbsp;&nbsp;</span>Q: Which country and country makes the best chcolate?</a></span></li><li><span><a href="#Q:-Which-company-makes-the-best-choclate-?" data-toc-modified-id="Q:-Which-company-makes-the-best-choclate-?-3.3"><span class="toc-item-num">3.3&nbsp;&nbsp;</span>Q: Which company makes the best choclate ?</a></span></li><li><span><a href="#Q:-Does-origin-of-bean-affects-rating?" data-toc-modified-id="Q:-Does-origin-of-bean-affects-rating?-3.4"><span class="toc-item-num">3.4&nbsp;&nbsp;</span>Q: Does origin of bean affects rating?</a></span></li><li><span><a href="#Q:-Recipie-of-which-company-should-we-follow-?" data-toc-modified-id="Q:-Recipie-of-which-company-should-we-follow-?-3.5"><span class="toc-item-num">3.5&nbsp;&nbsp;</span>Q: Recipie of which company should we follow ?</a></span></li><li><span><a href="#Master-Choclatier?" data-toc-modified-id="Master-Choclatier?-3.6"><span class="toc-item-num">3.6&nbsp;&nbsp;</span>Master Choclatier?</a></span></li></ul></li><li><span><a href="#4.-Final-Dashboard" data-toc-modified-id="4.-Final-Dashboard-4"><span class="toc-item-num">4&nbsp;&nbsp;</span>4. Final Dashboard</a></span><ul class="toc-item"><li><ul class="toc-item"><li><span><a href="#Three-Componenets-Of-DASH" data-toc-modified-id="Three-Componenets-Of-DASH-4.0.1"><span class="toc-item-num">4.0.1&nbsp;&nbsp;</span>Three Componenets Of DASH</a></span></li></ul></li></ul></li><li><span><a href="#Summary:" data-toc-modified-id="Summary:-5"><span class="toc-item-num">5&nbsp;&nbsp;</span>Summary:</a></span></li></ul></div>
"""
"""
1. So much data, do not know what to do 90%
2. Knows about data, but not good questions 9%  
3. Knows exact question and exact answers 1%   (Social Media platforms, Shopping platform, Advance Daignostic Transport Platform)

Data: http://flavorsofcacao.com/index.html
"""
"""
## Loading Libraries
"""
!pip install chart_studio
import pandas as pd
import numpy as np


from chart_studio.plotly import plot, iplot as py

import plotly.graph_objects as go
from plotly.offline import iplot, init_notebook_mode

import cufflinks
cufflinks.go_offline(connected=True)
init_notebook_mode(connected=True)

from plotly.subplots import make_subplots
import plotly.express as px

import plotly.io as pio
pio.templates
!ls ../input/cocolate/chocolate.csv

"""
# Descriptive Analysis
"""
"""
## Trailer of data
"""
df=pd.read_csv("../input/cocolate/chocolate.csv", index_col="serial")
df.head()
"""
### Shape
"""
df.shape
"""
### Column Names
"""
for col in df.columns:
    print(col)
"""
### Converting haves and haves_not
"""
col_to_boolean= ["beans","cocoa_butter","vanilla","lecithin","salt","sugar","sweetener_without_sugar"]

def have_not_have_to_bool(x):
    if "have_not" in x:
        y=0
    else:
        y=1
    return y

for col in col_to_boolean:
    df[col]=df[col].apply(have_not_have_to_bool)
df.head()
"""
### General Stats
"""
df.describe()
"""
## KDE - Normal 
## Histogram

"""
col_for_normal=["cocoa_percent","rating","counts_of_ingredients"]

fig = make_subplots(rows=1, cols=3)

for col in col_for_normal:
    
    i=col_for_normal.index(col)
    
    
    fig.add_trace(
        go.Histogram(x=df[col],
                     histnorm='probability',
                     name=col,),
        
        row=1,
        col=col_for_normal.index(col)+1
    )


fig.update_layout(height=500, width=1000, title_text="Histogram")
fig.show()
"""
## Box PLot
"""
col_for_normal=["cocoa_percent","rating","counts_of_ingredients"]

fig = make_subplots(rows=1, cols=3)

for col in col_for_normal:
    
    i=col_for_normal.index(col)
    
    
    fig.add_trace(
        go.Box(y=df[col], name=col),
    
        row=1,
        col=col_for_normal.index(col)+1
    )


fig.update_layout(height=500, width=1000, title_text="Histogram")
fig.show()
"""
## Scatter Plot
"""
col_for_normal=["cocoa_percent","rating","counts_of_ingredients"]

corr_dict=[]
for col in col_for_normal:
    corr_dict.append(
        dict(label=col,values=df[col]),
    )
    


fig = go.Figure(data=go.Splom(
                dimensions=corr_dict,
#                 diagonal_visible=False, # remove plots on diagonal
                ))


fig.update_layout(
    title="Scatter Plot",
    width=1000,
    height=1000,
)

fig.show()
"""
## Heat Map

"""
col_for_normal=["cocoa_percent","rating","counts_of_ingredients"]

df_cor=df[col_for_normal].corr()

print(df_cor)
import plotly.express as px

fig = px.imshow(df_cor)

fig.update_layout(
    title='Heat Map',
    width=500,
    height=500,
)

fig.show()
"""
# Predcitive


"""
"""
## OLS v1 adding only nominal variables
"""
import statsmodels.api as sm

col_for_normal=["cocoa_percent","rating"]
X=df[["cocoa_percent","counts_of_ingredients"]]
y=df[["rating"]]
X = sm.add_constant(X)

model = sm.OLS(y,X)

results = model.fit()
results.summary()
df.head()
"""
* Numerical regression= OLS
* Nominal regression = probit 
* Ordinal regression = logit
"""
"""
## OLS v2 adding all variables
"""
X=df[["cocoa_percent","counts_of_ingredients","beans","cocoa_butter","lecithin","salt","vanilla","sugar","sweetener_without_sugar"]]
y=df[["rating"]]

model = sm.OLS(y, X)

results = model.fit()

results.summary()
"""
* If the p-value is less than 0.05, 

    - we reject the null hypothesis that there's no difference between the means and conclude that a significant difference does exist. If the p-value is larger than 0.05, we cannot conclude that a significant difference exists.03-Dec-2015

* H0:that there's no difference between the means
* H1:difference exist
"""
"""
## Stepwise OLS
"""
X=df[["cocoa_butter","sugar","vanilla","sweetener_without_sugar"]]
     
y=df[["rating"]]

model = sm.OLS(y, X)
results = model.fit()

results.summary()
results.params
"""
## PCA
"""
from sklearn.decomposition import PCA

X=df[["cocoa_butter","sugar","vanilla","sweetener_without_sugar"]]
# X=df[["cocoa_percent","counts_of_ingredients","beans","cocoa_butter","lecithin","salt","vanilla","sugar","sweetener_without_sugar"]]


pca = PCA(n_components=2)
principalComponents = pca.fit_transform(X)

pca_df= pd.DataFrame(data = principalComponents
             , columns = ['pca1', 'pca2',], index=df.index)

pca_df["rating"]=df["rating"].round(1).astype(float)
pca_df["rating"]*=10
pca_df["rating"]=pca_df["rating"].astype(int)
display(pca_df.head())


fig = go.Figure(data=go.Scatter(
                x=pca_df["pca1"], y=pca_df["pca2"],
                mode='markers',
                hovertext=pca_df["rating"],
                marker=dict(
                    size=16,
                    color=pca_df["rating"],
                    colorscale='aggrnyl',
#                     colorscale="blues",
                    showscale=True)
                ))


fig.update_layout(title='Using PCA to reduce dimension',width=600,height=600)

fig.show()
"""
## Clustering
"""
from sklearn.cluster import KMeans

kmeans = KMeans(n_clusters=6, random_state=0).fit(pca_df)
kmeans.labels_

pca_df["label"]=kmeans.labels_
display(pca_df.head())



fig = px.scatter(pca_df, x="pca1", y="pca2", color="rating",  size="rating",hover_data=["rating"], )
fig.update_layout( title="Rating", width=500, height=400 ,)
fig.show()


fig = px.scatter(pca_df, x="pca1", y="pca2", color="label",size="rating", hover_data=["rating","label"])
fig.update_layout( title="Cluster", width=500, height=400 )
fig.show()
"""
## Verifying clusters
"""
kmean_df=pca_df[["rating","label"]].groupby("label").mean()
kmean_df["std"]=pca_df[["rating","label"]].groupby("label").std()["rating"]
display(kmean_df)
good_df=df[pca_df["label"]==3]

my_order = good_df.groupby(by=["company"])["rating"].mean().iloc[::-1].index
my_order=my_order[:20]
my_order

good_df=good_df[good_df["company"].isin(my_order)]
display(good_df.head())

good_df.groupby(["company","review_date"])["rating"].mean()
"""
## Wordcloud
"""
from wordcloud import WordCloud
from matplotlib import pyplot as plt


col_to_combi=["first_taste","second_taste","third_taste","fourth_taste"]

text=[]
for col in col_to_combi:
    t=good_df[col].tolist()
    text.extend(t)
clean_text=""
for t in text:
    if not pd.isna(t):
        clean_text+= (str(t) +",")
    else:
        pass

word_cloud = WordCloud(collocations = False, background_color = 'white').generate(clean_text)
plt.imshow(word_cloud, interpolation='bilinear')
plt.axis("off")
plt.show()
"""
# Prescriptive


"""
import matplotlib.pyplot as plt
import seaborn as sns
"""
## Q: Does Rating change evry year ?
### Normal Distribution
"""
year_list=sorted(set(df["review_date"]))

fig = plt.figure(figsize=(20,20))

a=3
b=5
c=1

p=sns.color_palette("Paired")
p.extend(p)


for year in year_list:
    plt.subplot(a,b,c)
    plt.title(year)
    sns.kdeplot(data=df[df["review_date"]==year], x="rating", fill=True,color=p[c-1])
    c+=1
    
plt.show()
"""
### Boxplot
"""
fig = plt.figure(figsize=(20,20))
sns.boxplot(data=df,x="review_date",y="rating").set_title("Rating vs Years Box Plot")
"""
### Standard Deviation

"""
fig = plt.figure(figsize=(5,5))

std_df=df[["review_date","rating"]].groupby(["review_date"]).std().reset_index()
sns.lineplot(x=std_df["review_date"],y=std_df["rating"]).set_title("Standard Deviation vs Time")
"""
### Mean Rating over time 
"""
fig = plt.figure(figsize=(5,5))

mean_df=df[["review_date","rating"]].groupby(["review_date"]).mean().reset_index()
sns.lineplot(x=mean_df["review_date"],y=mean_df["rating"]).set_title("Rating vs Time")
"""
## Q: Which country and country makes the best chcolate?
"""
fig = plt.figure(figsize=(10,20))
my_order = df.groupby(by=["company_location"])["rating"].mean().iloc[::-1].index
sns.boxplot(data=df,x="rating",y="company_location", orient="h", order=my_order)
plt.show()
"""
## Q: Which company makes the best choclate ?
"""
fig = plt.figure(figsize=(20,10))

my_order = df.groupby(by=["company"])["rating"].median().iloc[::-1].index
my_order_20=my_order[:20]

comp_df=df[df["company"].isin(my_order_20)]

sns.boxplot(data=comp_df,x="company",y="rating", orient="v", order=my_order_20)
plt.xticks(rotation=80, fontsize=15)


plt.show()
"""
## Q: Does origin of bean affects rating?
"""
fig = plt.figure(figsize=(20,10))

my_order = df.groupby(by=["country_of_bean_origin"])["rating"].mean().iloc[::-1].index
my_order_20=my_order[:20]

comp_df=df[df["country_of_bean_origin"].isin(my_order_20)]

sns.boxplot(data=comp_df,x="country_of_bean_origin",y="rating", orient="v", order=my_order_20)
plt.xticks(rotation=60, fontsize=15)

plt.show()
"""
## Q: Recipie of which company should we follow ?  

"""
fig = plt.figure(figsize=(20,30))

a=4
b=5
c=1


my_order = df.groupby(by=["company"])["rating"].median().iloc[::-1].index
my_order_20=my_order[:20]

comp_df=df[df["company"].isin(my_order_20)]


for company in my_order_20:
    plt.subplot(a,b,c)
    plt.title(company)
    sns.lineplot(data=comp_df[comp_df["company"]==company], x="review_date",y="rating")
    c+=1
    
plt.show()
"""
## Master Choclatier?
"""
# twenty-four-blackbirds, Zokoko

master_df=df[df["company"]=="twenty-four blackbirds"]

display(master_df.head())

master_df.groupby(["rating","country_of_bean_origin","first_taste","second_taste"]).sum()
"""
# 4. Final Dashboard
"""
"""
### Three Componenets Of DASH

1. Components ->Slider, Checkbox
2. Graph -> Line char, Scatter plot
3. Callback -> Function
"""
!pip install jupyter_dash dash_bootstrap_components
import plotly.express as px
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
df.head()
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']


app = JupyterDash(__name__, external_stylesheets=external_stylesheets)


app.layout = html.Div([

    html.H1("Web Application Dashboards with Dash", style={'text-align': 'center'}),
        
    
    
    dcc.Slider(
        id='year-slider',
        min=df['review_date'].min(),
        max=df['review_date'].max(),
        value=df['review_date'].min(),
        marks={str(year): str(year) for year in df['review_date'].unique()},
        step=None
    ),
   
    dcc.Dropdown(
        id='graph-dropdown',
        
        options=[
            {'label': 'Scatter', 'value': 'scatter'},
            {'label': 'Box Plot', 'value': 'boxplot'},
            {'label': 'Bar Plot', 'value': 'barplot'},
            
        ],
        value='scatter'
    ),
            
    dcc.Graph(id='graph-with-slider'),
    
    
#     html.Label('Dropdown'),
#     dcc.Dropdown(
#         options=[
#             {'label': 'New York City', 'value': 'NYC'},
#             {'label': u'Montréal', 'value': 'MTL'},
#             {'label': 'San Francisco', 'value': 'SF'}
#         ],
#         value='MTL'
#     ),

#     html.Label('Multi-Select Dropdown'),
#     dcc.Dropdown(
#         options=[
#             {'label': 'New York City', 'value': 'NYC'},
#             {'label': u'Montréal', 'value': 'MTL'},
#             {'label': 'San Francisco', 'value': 'SF'}
#         ],
#         value=['MTL', 'SF'],
#         multi=True
#     ),

#     html.Label('Radio Items'),
#     dcc.RadioItems(
#         options=[
#             {'label': 'New York City', 'value': 'NYC'},
#             {'label': u'Montréal', 'value': 'MTL'},
#             {'label': 'San Francisco', 'value': 'SF'}
#         ],
#         value='MTL'
#     ),

#     html.Label('Checkboxes'),
#     dcc.Checklist(
#         options=[
#             {'label': 'New York City', 'value': 'NYC'},
#             {'label': u'Montréal', 'value': 'MTL'},
#             {'label': 'San Francisco', 'value': 'SF'}
#         ],
#         value=['MTL', 'SF']
#     ),

#     html.Label('Text Input'),
#     dcc.Input(id='my-input', value='initial value', type='text'),

#     html.Label('Text Output'),
#     html.Div(id='my-output'),
    
    
    
#     html.Label('Slider'),
#     dcc.Slider(
#         min=0,
#         max=9,
#         marks={i: 'Label {}'.format(i) if i == 1 else str(i) for i in range(1, 6)},
#         value=5,
#     ),

    
    
])

@app.callback(
    Output('graph-with-slider', 'figure'),
    Input('year-slider', 'value'),
    Input('graph-dropdown', 'value')
)




def update_figure(selected_year,graph_type):
    filtered_df = df[df.review_date == selected_year]

    
    if graph_type=="boxplot":
        
        fig = px.box(filtered_df, x='company_location', y='rating',
                    hover_name="company",
                    color="company_location")
 
        fig.update_layout(transition_duration=500)

    elif graph_type=="barplot":
        
        fig = px.bar(filtered_df, x='company_location', y='rating',
                    color="rating", hover_name="company",)
 
        fig.update_layout(transition_duration=500)

    elif graph_type=="scatter":
        fig = px.scatter(filtered_df, x="company_location", y="rating",
                         color="rating", hover_name="company",
                         size="counts_of_ingredients",
    #                      log_x=True, 
                         size_max=55
                        )

        fig.update_layout(transition_duration=500)

    return fig

# ------------------------------------------------------------------------------
if __name__ == '__main__':
    app.run_server(mode='inline',debug=True)
"""
# Summary:
1. Methods are always more important than tools
2. Do not memorize code (Google question or find in Docs
3. Prepare questions for data
4. Simple Roadmap 
    1. Describe
    2. Predict: Ols->StepOls ->Clustering
    2. Prescribe: Questions ->more Question->Answer->Dashbaord

"""