You should be able to start up on this exercise after Lecture 1.
This exercise must be a group effort. That means everyone must participate in the assignment.
In this assignment you will solve a data science problem end-to-end, pretending to be recently hired data scientists in a company. To help you get started, we've prepared a checklist to guide you through the project. Here are the main steps that you will go through:
In each step we list a set of questions that one should have in mind when undertaking a data science project. The list is not meant to be exhaustive, but does contain a selection of the most important questions to ask. We will be available to provide assistance with each of the steps, and will allocate some part of each lesson towards working on the projects.
Your group must submit a single Jupyter notebook, structured in terms of the first 6 sections listed above (the seventh will be a video uploaded to some streaming platform, e.g. YouTube, Vimeo, etc.).
There are two datasets we will use. One has the hapiness score, and data that usually are used to explain why a country's happiness is higher, like economy and health. The other database has the alcohol per capita consumption by country. Both of the datasets results were created in 2016. The datasets url: https://www.kaggle.com/datasets/cssouza91/hapiness?select=2016.csv
https://www.kaggle.com/datasets/marcospessotto/happiness-and-alcohol-consumption
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
import seaborn
from scipy import stats
from sklearn.preprocessing import scale
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression, Ridge, RidgeCV, Lasso, LassoCV
from sklearn.linear_model import ElasticNet, ElasticNetCV
from sklearn.metrics import mean_squared_error
import plotly.graph_objs as go
from plotly.offline import iplot
from sklearn.preprocessing import MinMaxScaler, Normalizer, StandardScaler
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import AdaBoostRegressor
happiness = pd.read_csv("2016.csv")
happiness.head()
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Denmark | Western Europe | 1 | 7.526 | 7.460 | 7.592 | 1.44178 | 1.16374 | 0.79504 | 0.57941 | 0.44453 | 0.36171 | 2.73939 |
| 1 | Switzerland | Western Europe | 2 | 7.509 | 7.428 | 7.590 | 1.52733 | 1.14524 | 0.86303 | 0.58557 | 0.41203 | 0.28083 | 2.69463 |
| 2 | Iceland | Western Europe | 3 | 7.501 | 7.333 | 7.669 | 1.42666 | 1.18326 | 0.86733 | 0.56624 | 0.14975 | 0.47678 | 2.83137 |
| 3 | Norway | Western Europe | 4 | 7.498 | 7.421 | 7.575 | 1.57744 | 1.12690 | 0.79579 | 0.59609 | 0.35776 | 0.37895 | 2.66465 |
| 4 | Finland | Western Europe | 5 | 7.413 | 7.351 | 7.475 | 1.40598 | 1.13464 | 0.81091 | 0.57104 | 0.41004 | 0.25492 | 2.82596 |
print("It has {} rows and {} columns.".format(happiness.shape[0], happiness.shape[1]))
happiness.dtypes
It has 157 rows and 13 columns.
Country object Region object Happiness Rank int64 Happiness Score float64 Lower Confidence Interval float64 Upper Confidence Interval float64 Economy (GDP per Capita) float64 Family float64 Health (Life Expectancy) float64 Freedom float64 Trust (Government Corruption) float64 Generosity float64 Dystopia Residual float64 dtype: object
The dataset has data from 2016 like the happiness dataset. There are a few duplicate columns, which we are not interested in keeping, so we drop them before merging.
alcoholCSV = pd.read_csv("HappinessAlcoholConsumption.csv")
alcoholCSV = alcoholCSV.drop(["Region", "Hemisphere", "HappinessScore", "HDI", "GDP_PerCapita"], axis=1)
alcoholCSV.head()
| Country | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|
| 0 | Denmark | 224 | 81 | 278 |
| 1 | Switzerland | 185 | 100 | 280 |
| 2 | Iceland | 233 | 61 | 78 |
| 3 | Norway | 169 | 71 | 129 |
| 4 | Finland | 263 | 133 | 97 |
print("It has {} rows and {} columns.".format(alcoholCSV.shape[0], alcoholCSV.shape[1]))
alcoholCSV.dtypes
It has 122 rows and 4 columns.
Country object Beer_PerCapita int64 Spirit_PerCapita int64 Wine_PerCapita int64 dtype: object
combinedDatasets = happiness.merge(alcoholCSV, left_on="Country", right_on="Country")
combinedDatasets.head()
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Denmark | Western Europe | 1 | 7.526 | 7.460 | 7.592 | 1.44178 | 1.16374 | 0.79504 | 0.57941 | 0.44453 | 0.36171 | 2.73939 | 224 | 81 | 278 |
| 1 | Switzerland | Western Europe | 2 | 7.509 | 7.428 | 7.590 | 1.52733 | 1.14524 | 0.86303 | 0.58557 | 0.41203 | 0.28083 | 2.69463 | 185 | 100 | 280 |
| 2 | Iceland | Western Europe | 3 | 7.501 | 7.333 | 7.669 | 1.42666 | 1.18326 | 0.86733 | 0.56624 | 0.14975 | 0.47678 | 2.83137 | 233 | 61 | 78 |
| 3 | Norway | Western Europe | 4 | 7.498 | 7.421 | 7.575 | 1.57744 | 1.12690 | 0.79579 | 0.59609 | 0.35776 | 0.37895 | 2.66465 | 169 | 71 | 129 |
| 4 | Finland | Western Europe | 5 | 7.413 | 7.351 | 7.475 | 1.40598 | 1.13464 | 0.81091 | 0.57104 | 0.41004 | 0.25492 | 2.82596 | 263 | 133 | 97 |
dataset = combinedDatasets.copy()
print("It has {} rows and {} columns.".format(dataset.shape[0], dataset.shape[1]))
dataset.dtypes
It has 118 rows and 16 columns.
Country object Region object Happiness Rank int64 Happiness Score float64 Lower Confidence Interval float64 Upper Confidence Interval float64 Economy (GDP per Capita) float64 Family float64 Health (Life Expectancy) float64 Freedom float64 Trust (Government Corruption) float64 Generosity float64 Dystopia Residual float64 Beer_PerCapita int64 Spirit_PerCapita int64 Wine_PerCapita int64 dtype: object
Feature description
Data taken from https://worldhappiness.report/ed/2022/happiness-benevolence-and-trust-during-covid-19-and-beyond/
dataset.isnull().sum()
Country 0 Region 0 Happiness Rank 0 Happiness Score 0 Lower Confidence Interval 0 Upper Confidence Interval 0 Economy (GDP per Capita) 0 Family 0 Health (Life Expectancy) 0 Freedom 0 Trust (Government Corruption) 0 Generosity 0 Dystopia Residual 0 Beer_PerCapita 0 Spirit_PerCapita 0 Wine_PerCapita 0 dtype: int64
dataset.dtypes
Country object Region object Happiness Rank int64 Happiness Score float64 Lower Confidence Interval float64 Upper Confidence Interval float64 Economy (GDP per Capita) float64 Family float64 Health (Life Expectancy) float64 Freedom float64 Trust (Government Corruption) float64 Generosity float64 Dystopia Residual float64 Beer_PerCapita int64 Spirit_PerCapita int64 Wine_PerCapita int64 dtype: object
dataset.info()
<class 'pandas.core.frame.DataFrame'> Int64Index: 118 entries, 0 to 117 Data columns (total 16 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Country 118 non-null object 1 Region 118 non-null object 2 Happiness Rank 118 non-null int64 3 Happiness Score 118 non-null float64 4 Lower Confidence Interval 118 non-null float64 5 Upper Confidence Interval 118 non-null float64 6 Economy (GDP per Capita) 118 non-null float64 7 Family 118 non-null float64 8 Health (Life Expectancy) 118 non-null float64 9 Freedom 118 non-null float64 10 Trust (Government Corruption) 118 non-null float64 11 Generosity 118 non-null float64 12 Dystopia Residual 118 non-null float64 13 Beer_PerCapita 118 non-null int64 14 Spirit_PerCapita 118 non-null int64 15 Wine_PerCapita 118 non-null int64 dtypes: float64(10), int64(4), object(2) memory usage: 15.7+ KB
dataset.iloc[:, 3:].describe()
| Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| count | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 | 118.000000 |
| mean | 5.557195 | 5.459975 | 5.654415 | 1.021216 | 0.840081 | 0.594344 | 0.383215 | 0.134977 | 0.229399 | 2.353952 | 138.906780 | 97.067797 | 68.093220 |
| std | 1.145853 | 1.153111 | 1.139521 | 0.381773 | 0.251482 | 0.222809 | 0.145206 | 0.114400 | 0.125321 | 0.519255 | 105.090599 | 78.595658 | 89.066189 |
| min | 3.069000 | 2.936000 | 3.202000 | 0.087090 | 0.000000 | 0.000000 | 0.005890 | 0.000000 | 0.000000 | 0.817890 | 1.000000 | 1.000000 | 1.000000 |
| 25% | 4.640000 | 4.515250 | 4.767250 | 0.817762 | 0.708878 | 0.478178 | 0.275750 | 0.052440 | 0.140220 | 2.090750 | 42.000000 | 32.000000 | 5.000000 |
| 50% | 5.553000 | 5.469500 | 5.677500 | 1.088420 | 0.877970 | 0.643315 | 0.404725 | 0.098535 | 0.211335 | 2.300940 | 127.500000 | 85.500000 | 16.000000 |
| 75% | 6.480250 | 6.396750 | 6.570750 | 1.308817 | 1.041250 | 0.775190 | 0.502933 | 0.177445 | 0.303113 | 2.694330 | 224.750000 | 142.500000 | 115.250000 |
| max | 7.526000 | 7.460000 | 7.669000 | 1.824270 | 1.183260 | 0.947190 | 0.608480 | 0.480490 | 0.586960 | 3.559060 | 376.000000 | 373.000000 | 370.000000 |
dataset.iloc[:, :3].plot.box()
<AxesSubplot:>
dataset.iloc[:, 3:13].plot.box(figsize=(20,12))
<AxesSubplot:>
Family, Trust, Generosity and Dystopia Residual have outliers. We do not care about Dystopian Residual as it will not be used for our learning algorithms. However we will decide if to drop the outliers on the first three.
def outliers(column):
'''
Return outliers for a given column name
'''
Q1 = dataset[column].quantile(0.25)
Q3 = dataset[column].quantile(0.75)
IQR = Q3 - Q1 #IQR is interquartile range.
return dataset[(dataset[column] < Q1 - 1.5 * IQR) | (dataset[column] > Q3 + 1.5 *IQR)]
outliers("Family")
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 97 | Georgia | Central and Eastern Europe | 126 | 4.252 | 4.164 | 4.340 | 0.83792 | 0.19249 | 0.64035 | 0.32461 | 0.31880 | 0.06786 | 1.87031 | 52 | 100 | 149 |
| 101 | Malawi | Sub-Saharan Africa | 132 | 4.156 | 4.041 | 4.271 | 0.08709 | 0.14700 | 0.29364 | 0.41430 | 0.07564 | 0.30968 | 2.82859 | 8 | 11 | 1 |
| 115 | Benin | Sub-Saharan Africa | 153 | 3.484 | 3.404 | 3.564 | 0.39499 | 0.10419 | 0.21028 | 0.39747 | 0.06681 | 0.20180 | 2.10812 | 34 | 4 | 13 |
| 116 | Togo | Sub-Saharan Africa | 155 | 3.303 | 3.192 | 3.414 | 0.28123 | 0.00000 | 0.24811 | 0.34678 | 0.11587 | 0.17517 | 2.13540 | 36 | 2 | 19 |
| 117 | Syria | Middle East and Northern Africa | 156 | 3.069 | 2.936 | 3.202 | 0.74719 | 0.14866 | 0.62994 | 0.06912 | 0.17233 | 0.48397 | 0.81789 | 5 | 35 | 16 |
outliers("Trust (Government Corruption)")
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Denmark | Western Europe | 1 | 7.526 | 7.460 | 7.592 | 1.44178 | 1.16374 | 0.79504 | 0.57941 | 0.44453 | 0.36171 | 2.73939 | 224 | 81 | 278 |
| 1 | Switzerland | Western Europe | 2 | 7.509 | 7.428 | 7.590 | 1.52733 | 1.14524 | 0.86303 | 0.58557 | 0.41203 | 0.28083 | 2.69463 | 185 | 100 | 280 |
| 4 | Finland | Western Europe | 5 | 7.413 | 7.351 | 7.475 | 1.40598 | 1.13464 | 0.81091 | 0.57104 | 0.41004 | 0.25492 | 2.82596 | 263 | 133 | 97 |
| 7 | New Zealand | Australia and New Zealand | 8 | 7.334 | 7.264 | 7.404 | 1.36066 | 1.17278 | 0.83096 | 0.58147 | 0.41904 | 0.49401 | 2.47553 | 203 | 79 | 175 |
| 9 | Sweden | Western Europe | 10 | 7.291 | 7.227 | 7.355 | 1.45181 | 1.08764 | 0.83121 | 0.58218 | 0.40867 | 0.38254 | 2.54734 | 152 | 60 | 186 |
| 20 | Singapore | Southeastern Asia | 22 | 6.739 | 6.674 | 6.804 | 1.64555 | 0.86758 | 0.94719 | 0.48770 | 0.46987 | 0.32706 | 1.99375 | 60 | 12 | 11 |
| 32 | Qatar | Middle East and Northern Africa | 36 | 6.375 | 6.178 | 6.572 | 1.82427 | 0.87964 | 0.71723 | 0.56679 | 0.48049 | 0.32388 | 1.58224 | 1 | 42 | 7 |
outliers("Generosity")
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 28 | Malta | Western Europe | 30 | 6.488 | 6.409 | 6.567 | 1.30782 | 1.09879 | 0.80315 | 0.54994 | 0.17554 | 0.56237 | 1.99032 | 149 | 100 | 120 |
| 31 | Thailand | Southeastern Asia | 33 | 6.474 | 6.396 | 6.552 | 1.08930 | 1.04477 | 0.64915 | 0.49553 | 0.02833 | 0.58696 | 2.57960 | 99 | 258 | 1 |
We can plot the data too see how similar the countries' alcohol consumption is and look for outlisers. There are a few outliers, especially for wine consumtion, where 50% of the data is very low, indicating that many countries drink little to no wine.
dataset.iloc[:, 13:].plot.box(figsize=(20,12))
<AxesSubplot:>
Below is the happiness score, because it does not fit on the above scale. There are no outliers.
outliers('Spirit_PerCapita')
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 53 | Belarus | Central and Eastern Europe | 61 | 5.802 | 5.723 | 5.881 | 1.13062 | 1.04993 | 0.63104 | 0.29091 | 0.17457 | 0.13942 | 2.38582 | 142 | 373 | 42 |
| 104 | Haiti | Latin America and Caribbean | 136 | 4.028 | 3.893 | 4.163 | 0.34097 | 0.29561 | 0.27494 | 0.12072 | 0.14476 | 0.47958 | 2.37116 | 1 | 326 | 1 |
The outliers below are for Wine. We are not nessecarily interested in removing the outliers for wine because the data was so split.
outliers('Wine_PerCapita')
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 30 | France | Western Europe | 32 | 6.478 | 6.397 | 6.559 | 1.39488 | 1.00508 | 0.83795 | 0.46562 | 0.17808 | 0.12160 | 2.47440 | 127 | 151 | 370 |
| 78 | Portugal | Western Europe | 94 | 5.123 | 5.030 | 5.216 | 1.27607 | 0.94367 | 0.79363 | 0.44727 | 0.01521 | 0.11691 | 1.53015 | 194 | 67 | 339 |
Below we can see the lowest wine consumption with less than 3l wine.
dataset[(dataset['Wine_PerCapita'] < 3)]
| Country | Region | Happiness Rank | Happiness Score | Lower Confidence Interval | Upper Confidence Interval | Economy (GDP per Capita) | Family | Health (Life Expectancy) | Freedom | Trust (Government Corruption) | Generosity | Dystopia Residual | Beer_PerCapita | Spirit_PerCapita | Wine_PerCapita | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 31 | Thailand | Southeastern Asia | 33 | 6.474 | 6.396 | 6.552 | 1.08930 | 1.04477 | 0.64915 | 0.49553 | 0.02833 | 0.58696 | 2.57960 | 99 | 258 | 1 |
| 34 | Guatemala | Latin America and Caribbean | 39 | 6.324 | 6.213 | 6.435 | 0.83454 | 0.87119 | 0.54039 | 0.50379 | 0.08701 | 0.28808 | 3.19863 | 53 | 69 | 2 |
| 40 | El Salvador | Latin America and Caribbean | 46 | 6.068 | 5.967 | 6.169 | 0.87370 | 0.80975 | 0.59600 | 0.37269 | 0.10613 | 0.08877 | 3.22134 | 52 | 69 | 2 |
| 41 | Nicaragua | Latin America and Caribbean | 48 | 5.992 | 5.877 | 6.107 | 0.69384 | 0.89521 | 0.65213 | 0.46582 | 0.16292 | 0.29773 | 2.82428 | 78 | 118 | 1 |
| 66 | Jordan | Middle East and Northern Africa | 80 | 5.303 | 5.187 | 5.419 | 0.99673 | 0.86216 | 0.60712 | 0.36023 | 0.13297 | 0.14262 | 2.20142 | 6 | 21 | 1 |
| 68 | Philippines | Southeastern Asia | 82 | 5.279 | 5.160 | 5.398 | 0.81217 | 0.87877 | 0.47036 | 0.54854 | 0.11757 | 0.21674 | 2.23484 | 71 | 186 | 1 |
| 80 | Vietnam | Southeastern Asia | 96 | 5.061 | 4.991 | 5.131 | 0.74037 | 0.79117 | 0.66157 | 0.55954 | 0.11556 | 0.25075 | 1.94180 | 111 | 2 | 1 |
| 84 | Nigeria | Sub-Saharan Africa | 103 | 4.875 | 4.750 | 5.000 | 0.75216 | 0.64498 | 0.05108 | 0.27854 | 0.03050 | 0.23219 | 2.88586 | 42 | 5 | 2 |
| 85 | Honduras | Latin America and Caribbean | 104 | 4.871 | 4.750 | 4.992 | 0.69429 | 0.75596 | 0.58383 | 0.26755 | 0.06906 | 0.20440 | 2.29551 | 69 | 98 | 2 |
| 88 | Sierra Leone | Sub-Saharan Africa | 111 | 4.635 | 4.505 | 4.765 | 0.36485 | 0.62800 | 0.00000 | 0.30685 | 0.08196 | 0.23897 | 3.01402 | 25 | 3 | 2 |
| 89 | Namibia | Sub-Saharan Africa | 113 | 4.574 | 4.374 | 4.774 | 0.93287 | 0.70362 | 0.34745 | 0.48614 | 0.10398 | 0.07795 | 1.92198 | 376 | 3 | 1 |
| 92 | Egypt | Middle East and Northern Africa | 120 | 4.362 | 4.259 | 4.465 | 0.95395 | 0.49813 | 0.52116 | 0.18847 | 0.10393 | 0.12706 | 1.96895 | 6 | 4 | 1 |
| 94 | Kenya | Sub-Saharan Africa | 122 | 4.356 | 4.259 | 4.453 | 0.52267 | 0.76240 | 0.30147 | 0.40576 | 0.06686 | 0.41328 | 1.88326 | 58 | 22 | 2 |
| 101 | Malawi | Sub-Saharan Africa | 132 | 4.156 | 4.041 | 4.271 | 0.08709 | 0.14700 | 0.29364 | 0.41430 | 0.07564 | 0.30968 | 2.82859 | 8 | 11 | 1 |
| 103 | Mali | Sub-Saharan Africa | 135 | 4.073 | 3.988 | 4.158 | 0.31292 | 0.86333 | 0.16347 | 0.27544 | 0.13647 | 0.21064 | 2.11087 | 5 | 1 | 1 |
| 104 | Haiti | Latin America and Caribbean | 136 | 4.028 | 3.893 | 4.163 | 0.34097 | 0.29561 | 0.27494 | 0.12072 | 0.14476 | 0.47958 | 2.37116 | 1 | 326 | 1 |
| 106 | Comoros | Sub-Saharan Africa | 138 | 3.956 | 3.860 | 4.052 | 0.27509 | 0.60323 | 0.29981 | 0.15412 | 0.18437 | 0.18270 | 2.25632 | 1 | 3 | 1 |
| 107 | Cambodia | Southeastern Asia | 140 | 3.907 | 3.798 | 4.016 | 0.55604 | 0.53750 | 0.42494 | 0.58852 | 0.08092 | 0.40339 | 1.31573 | 57 | 65 | 1 |
| 109 | Niger | Sub-Saharan Africa | 142 | 3.856 | 3.781 | 3.931 | 0.13270 | 0.60530 | 0.26162 | 0.38041 | 0.17176 | 0.20970 | 2.09469 | 3 | 2 | 1 |
| 110 | Chad | Sub-Saharan Africa | 144 | 3.763 | 3.672 | 3.854 | 0.42214 | 0.63178 | 0.03824 | 0.12807 | 0.04952 | 0.18667 | 2.30637 | 15 | 1 | 1 |
| 113 | Tanzania | Sub-Saharan Africa | 149 | 3.666 | 3.561 | 3.771 | 0.47155 | 0.77623 | 0.35700 | 0.31760 | 0.05099 | 0.31472 | 1.37769 | 36 | 6 | 1 |
| 114 | Liberia | Sub-Saharan Africa | 150 | 3.622 | 3.463 | 3.781 | 0.10706 | 0.50353 | 0.23165 | 0.25748 | 0.04852 | 0.24063 | 2.23284 | 19 | 152 | 2 |
The target is happiness score. We will take the columns, Economy, Family, Health, Freedom, Trust and Generosity to see if there is any association between those values and happiness. From the second dataset the target is happiness score and we will try to find association with the columns: Beer_PerCapita, Beer_PerCapita, Wine_PerCapita.
dataset.plot(subplots=True, figsize=(8, 8));
#https://levelup.gitconnected.com/plotting-choropleth-maps-in-python-b74c53b8d0a6
def featureWorldMap(feature, title):
'''
Returns a world heatmap of a given feature
'''
return go.Figure(
data = {
'type':'choropleth',
'locations':dataset['Country'],
'locationmode':'country names',
'colorscale':["darkred","red","lightcoral","white", "palegreen","green","darkgreen"],
'z':dataset[feature],
'colorbar':{'title': title},
'marker': {
'line': {
'color':'rgb(255,255,255)',
'width':2
}
}
},
layout = {
'geo':{
'scope':'world',
}
}
)
featureWorldMap("Happiness Score", "Happiness Score")
TODO Do we need this link? I just used the data from the alcohol dataset from the top
featureWorldMap("Beer_PerCapita", "Beer consumption")
featureWorldMap("Spirit_PerCapita", "Spirit consumption")
featureWorldMap("Wine_PerCapita", "Wine consumption")
data_corr = dataset.iloc[:,[3,6,7,8,9,10,11,13,14,15]]
# pairplot
seaborn.pairplot(data_corr)
# to show
plt.show()
print("happiness and Economy\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,6])))
print("happiness and Family\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,7])))
print("happiness and Health\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,8])))
print("happiness and Freedom\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,9])))
print("happiness and Trust\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,10])))
print("happiness and Generosity correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,11])))
print("happiness and Beer\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,13])))
print("happiness and Spirit\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,14])))
print("happiness and Wine\t correlation is {}.".format(dataset.iloc[:,3].corr(dataset.iloc[:,15])))
happiness and Economy correlation is 0.7896671254192654. happiness and Family correlation is 0.7509180595792792. happiness and Health correlation is 0.7470862678123842. happiness and Freedom correlation is 0.6183532344146925. happiness and Trust correlation is 0.5161251180353214. happiness and Generosity correlation is 0.3067203229436911. happiness and Beer correlation is 0.4807494310940025. happiness and Spirit correlation is 0.23063681111279222. happiness and Wine correlation is 0.44042535523016324.
We can also see the correlation in a heatmap.
labels = data_corr.columns
seaborn.heatmap(data_corr.corr(),
mask=np.triu(np.ones_like(data_corr.corr())),
annot=True,
xticklabels=labels,
yticklabels=labels)\
.set_title('Correlation between happiness and features')
plt.xticks(rotation=45)
(array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]), [Text(0.5, 0, 'Happiness Score'), Text(1.5, 0, 'Economy (GDP per Capita)'), Text(2.5, 0, 'Family'), Text(3.5, 0, 'Health (Life Expectancy)'), Text(4.5, 0, 'Freedom'), Text(5.5, 0, 'Trust (Government Corruption)'), Text(6.5, 0, 'Generosity'), Text(7.5, 0, 'Beer_PerCapita'), Text(8.5, 0, 'Spirit_PerCapita'), Text(9.5, 0, 'Wine_PerCapita')])