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

# ### "Drink because you are happy, but never because you are miserable" 

#
#

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn as sk
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error
from IPython.core.display import HTML

display(HTML("<img src='https://mygenes.co.il/wp-content/uploads/2020/09/shutterstock_1376048102-1.jpg'/>"))

# #### The Dataset:
# The dataset contains information on countries that rank their level of happiness through various data such as HDI, gross domestic product (GDP) and types of alcohol.
# [link to the data source](https://www.kaggle.com/marcospessotto/happiness-and-alcohol-consumption)
#
# We read the data from a github repository:

alcohol_df = pd.read_csv('https://raw.githubusercontent.com/shahafmalka13/DS_PROJECT/main/HappinessAlcoholConsumption.csv.csv')
alcohol_df

# ## 1. Wrangling the data:
#
# - Treat data types (if needed)
# - Treat missing values (if needed)
# - Treat column names (if needed)
#
# - Treat any other weird thing your data might have
#
#

# we check the data types-

alcohol_df.dtypes

# Check if there are missing values-

alcohol_df.isnull().sum().sort_values()

# ## 2. Understanding the data
#

alcohol_df.plot(subplots = True , layout =(3,3) , kind ='box', figsize = (14,16), patch_artist = False)
plt.subplots_adjust(wspace=0.5)

# GDP- It can be clearly seen that many data are not in the relevant field and therefore we have chosen to present in another way.

plt.title('GDP_PerCapita:')
alcohol_df['GDP_PerCapita'].hist();

# - It can be noted that there are several anomalies in the data but these anomalies are not problematic because it is indeed possible that the gross national product in one country is significantly higher than in the other countries, as well as the consumption of wine or beer in one country is significantly different from another.
#   According to state production capacity / demand / supply

alcohol_df.pivot_table(['Beer_PerCapita','Spirit_PerCapita','Wine_PerCapita' ],'Region') 

# #### findings:
# - It seems that in the provinces characterized by developing or Muslim countries the level of alcohol consumption is relatively low compared to the developed countries

plt.figure(figsize=(15,6))
plt.title('Presentation of the sample of countries:')
plot1 = sns.countplot(x="Region", data=alcohol_df);
plot1.set_xticklabels(plot1.get_xticklabels(), rotation=40, ha="right");


# - It can be seen that the sample presented in the data reliably represents the distribution and division of countries into regions in the world.

alcohol_df["Total_alcohol"] = alcohol_df['Beer_PerCapita'] + alcohol_df['Spirit_PerCapita'] + alcohol_df['Wine_PerCapita']
alcohol_df.head()

alcohol_groupd = alcohol_df.groupby('Country')[['HappinessScore', 'Total_alcohol']].max()
groupH = alcohol_groupd.sort_values('HappinessScore',ascending = False).reset_index()
groupH

alcohol_groupd = alcohol_df.groupby('Country')[['HappinessScore', 'Total_alcohol']].max()
groupT = alcohol_groupd.sort_values('Total_alcohol',ascending = False).reset_index()
groupT

countries = groupT['Country']

diff=[]
for country in countries:
    c1 = int(groupT[groupT['Country'] == country].index[0])
    c2 = int(groupH[groupH['Country'] == country].index[0])
    diff+=[abs(c1-c2)]
mean = np.mean(diff)
mean

# ##### findings:
# - It can be seen that the happiest country is not necessarily the country with the highest alcohol consumption.
# - It can be seen that there is indeed a connection between the level of alcohol and happiness.
# - With the help of the function we built it can be seen that the sorting is not completely random.

corr_prep = alcohol_df[['HappinessScore','Total_alcohol']]
corr_prep.corr(method = 'spearman')

# We chose to use Spearman's correlation because the other methods give a lower correlation. (unit 5)

sns.heatmap(corr_prep.corr(), vmin=0.0 , vmax = 1,cmap='Reds' , annot=True);

plt.title('Amount of Alcohol versus happiness level:')
sns.regplot(x='Total_alcohol', y='HappinessScore', data=corr_prep);

# ##### findings:
#
# - The correlation between happiness level and overall alcohol consumption is moderate. This medium relationship can be explained by a number of explanations:
# - It is natural to think that in countries where people drink more the population is happier (celebrating more) but it is possible that in countries where people drink more it is the population that is obligated to alcohol addictions, ie a population with bigger problems.

# checking the correlation between types of alcohol with happines- 

spc_corr = alcohol_df[['HappinessScore','Beer_PerCapita','Spirit_PerCapita','Wine_PerCapita']].corr(method = 'spearman')
spc_corr

spc_corr.style.background_gradient(cmap='Blues')

plt.title('The connection between drinking Wine and drinking Beer:')
sns.regplot(x='Wine_PerCapita', y='Beer_PerCapita', data=alcohol_df);

# ##### findings:
#
# - we can see that the relationship between beer and happiness is the strongest between all of them. 
# - From the heat map it can be seen that there is a strong connection between drinking beer and drinking wine.

hdi_groupd = alcohol_df.groupby('Region')[['Total_alcohol','HDI']].sum()
hdi_groupd.sort_values('HDI',ascending = False)

# - There is a direct relationship between alcohol consumption and the level of development of the country

# ## 3. Building a model from the data

# We will try to predict the level of happiness using the HDI, GDP, total alcohol index. In order to predict we will use tree in the regression method because happiness is measured in a continuous range. Using the size of the error we will try to understand if there is a relationship between these features and the level of happiness. 

features = ['Total_alcohol','HDI','GDP_PerCapita']
X = alcohol_df[features]
y = alcohol_df['HappinessScore']
X_train, X_test, y_train, y_test = sk.model_selection.train_test_split(X, y, test_size=0.3, random_state=42)
X.head()

Alcohol_model = sk.tree.DecisionTreeRegressor(random_state=42)
Alcohol_model.fit(X_train, y_train)

#def mse(a,b):
    #return np.sqrt(np.square(a-b).mean())


def eval(x_test,y_test,Alcohol_model):
    pred = Alcohol_model.predict(x_test)
    print("MSE: {:.3f}".format(mean_squared_error(pred,y_test,squared=False)))


eval(X_test,y_test,Alcohol_model)

# +
print("Making predictions:")
print(y_test)

print("The predictions are:")
test_pred = Alcohol_model.predict(X_test)
print(test_pred)

print("MSE: {:.3f}".format(mse(y_test.values,test_pred)))
# -

# We will check the depth of the tree and try to change it to see if there is a positive change in the size of the error.

print("depth:",Alcohol_model.get_depth())

import sklearn.tree as tree
def plot_tree(tree_model,feat,size=(15,10)):
    fig = plt.figure(figsize=size)
    tree.plot_tree(tree_model, 
                   feature_names = feat, 
                   filled=True,
                  fontsize=15)  
    plt.show()


# +
model = DecisionTreeRegressor(max_depth=3,random_state=42)
model.fit(X_train,y_train)

eval(X_test,y_test,model)
plot_tree(model,X_test.columns,size=(30,20))
# -

# Because the mse is very low it can be concluded that the features we have chosen reliably predict the level of happiness measured in each country and that there is indeed a direct relationship between these features and the level of happiness. 

display(HTML("<img src='https://www.rt-ltd.com/wp-content/uploads/2017/06/didyouknow-246x300.png'/>"))    

# ##### And a little for general knowledge, let's see where we're located 

groupH[groupH['Country']=='Israel']

groupT[groupT['Country']=='Israel']

# And what makes Israelis rank so high in the global happiness rankings?
# The reasons can be seen in the next article
# [link to the article](https://www.ynet.co.il/articles/0,7340,L-4956142,00.html)

StarcoderdataJupyterScriptsDedupFiltered