# Import libraries require
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set(color_codes = True)
# Load the data into data frame according to the file where you save it
happiness = pd.read_csv("c://2019.csv")
# Let data frame to display all the data
pd.set_option("display.max_rows", None)
# show the data
happiness
Overall rank | Country or region | Score | GDP per capita | Social support | Healthy life expectancy | Freedom to make life choices | Generosity | Perceptions of corruption | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | Finland | 7.769 | 1.340 | 1.587 | 0.986 | 0.596 | 0.153 | 0.393 |
1 | 2 | Denmark | 7.600 | 1.383 | 1.573 | 0.996 | 0.592 | 0.252 | 0.410 |
2 | 3 | Norway | 7.554 | 1.488 | 1.582 | 1.028 | 0.603 | 0.271 | 0.341 |
3 | 4 | Iceland | 7.494 | 1.380 | 1.624 | 1.026 | 0.591 | 0.354 | 0.118 |
4 | 5 | Netherlands | 7.488 | 1.396 | 1.522 | 0.999 | 0.557 | 0.322 | 0.298 |
5 | 6 | Switzerland | 7.480 | 1.452 | 1.526 | 1.052 | 0.572 | 0.263 | 0.343 |
6 | 7 | Sweden | 7.343 | 1.387 | 1.487 | 1.009 | 0.574 | 0.267 | 0.373 |
7 | 8 | New Zealand | 7.307 | 1.303 | 1.557 | 1.026 | 0.585 | 0.330 | 0.380 |
8 | 9 | Canada | 7.278 | 1.365 | 1.505 | 1.039 | 0.584 | 0.285 | 0.308 |
9 | 10 | Austria | 7.246 | 1.376 | 1.475 | 1.016 | 0.532 | 0.244 | 0.226 |
10 | 11 | Australia | 7.228 | 1.372 | 1.548 | 1.036 | 0.557 | 0.332 | 0.290 |
11 | 12 | Costa Rica | 7.167 | 1.034 | 1.441 | 0.963 | 0.558 | 0.144 | 0.093 |
12 | 13 | Israel | 7.139 | 1.276 | 1.455 | 1.029 | 0.371 | 0.261 | 0.082 |
13 | 14 | Luxembourg | 7.090 | 1.609 | 1.479 | 1.012 | 0.526 | 0.194 | 0.316 |
14 | 15 | United Kingdom | 7.054 | 1.333 | 1.538 | 0.996 | 0.450 | 0.348 | 0.278 |
15 | 16 | Ireland | 7.021 | 1.499 | 1.553 | 0.999 | 0.516 | 0.298 | 0.310 |
16 | 17 | Germany | 6.985 | 1.373 | 1.454 | 0.987 | 0.495 | 0.261 | 0.265 |
17 | 18 | Belgium | 6.923 | 1.356 | 1.504 | 0.986 | 0.473 | 0.160 | 0.210 |
18 | 19 | United States | 6.892 | 1.433 | 1.457 | 0.874 | 0.454 | 0.280 | 0.128 |
19 | 20 | Czech Republic | 6.852 | 1.269 | 1.487 | 0.920 | 0.457 | 0.046 | 0.036 |
20 | 21 | United Arab Emirates | 6.825 | 1.503 | 1.310 | 0.825 | 0.598 | 0.262 | 0.182 |
21 | 22 | Malta | 6.726 | 1.300 | 1.520 | 0.999 | 0.564 | 0.375 | 0.151 |
22 | 23 | Mexico | 6.595 | 1.070 | 1.323 | 0.861 | 0.433 | 0.074 | 0.073 |
23 | 24 | France | 6.592 | 1.324 | 1.472 | 1.045 | 0.436 | 0.111 | 0.183 |
24 | 25 | Taiwan | 6.446 | 1.368 | 1.430 | 0.914 | 0.351 | 0.242 | 0.097 |
25 | 26 | Chile | 6.444 | 1.159 | 1.369 | 0.920 | 0.357 | 0.187 | 0.056 |
26 | 27 | Guatemala | 6.436 | 0.800 | 1.269 | 0.746 | 0.535 | 0.175 | 0.078 |
27 | 28 | Saudi Arabia | 6.375 | 1.403 | 1.357 | 0.795 | 0.439 | 0.080 | 0.132 |
28 | 29 | Qatar | 6.374 | 1.684 | 1.313 | 0.871 | 0.555 | 0.220 | 0.167 |
29 | 30 | Spain | 6.354 | 1.286 | 1.484 | 1.062 | 0.362 | 0.153 | 0.079 |
30 | 31 | Panama | 6.321 | 1.149 | 1.442 | 0.910 | 0.516 | 0.109 | 0.054 |
31 | 32 | Brazil | 6.300 | 1.004 | 1.439 | 0.802 | 0.390 | 0.099 | 0.086 |
32 | 33 | Uruguay | 6.293 | 1.124 | 1.465 | 0.891 | 0.523 | 0.127 | 0.150 |
33 | 34 | Singapore | 6.262 | 1.572 | 1.463 | 1.141 | 0.556 | 0.271 | 0.453 |
34 | 35 | El Salvador | 6.253 | 0.794 | 1.242 | 0.789 | 0.430 | 0.093 | 0.074 |
35 | 36 | Italy | 6.223 | 1.294 | 1.488 | 1.039 | 0.231 | 0.158 | 0.030 |
36 | 37 | Bahrain | 6.199 | 1.362 | 1.368 | 0.871 | 0.536 | 0.255 | 0.110 |
37 | 38 | Slovakia | 6.198 | 1.246 | 1.504 | 0.881 | 0.334 | 0.121 | 0.014 |
38 | 39 | Trinidad & Tobago | 6.192 | 1.231 | 1.477 | 0.713 | 0.489 | 0.185 | 0.016 |
39 | 40 | Poland | 6.182 | 1.206 | 1.438 | 0.884 | 0.483 | 0.117 | 0.050 |
40 | 41 | Uzbekistan | 6.174 | 0.745 | 1.529 | 0.756 | 0.631 | 0.322 | 0.240 |
41 | 42 | Lithuania | 6.149 | 1.238 | 1.515 | 0.818 | 0.291 | 0.043 | 0.042 |
42 | 43 | Colombia | 6.125 | 0.985 | 1.410 | 0.841 | 0.470 | 0.099 | 0.034 |
43 | 44 | Slovenia | 6.118 | 1.258 | 1.523 | 0.953 | 0.564 | 0.144 | 0.057 |
44 | 45 | Nicaragua | 6.105 | 0.694 | 1.325 | 0.835 | 0.435 | 0.200 | 0.127 |
45 | 46 | Kosovo | 6.100 | 0.882 | 1.232 | 0.758 | 0.489 | 0.262 | 0.006 |
46 | 47 | Argentina | 6.086 | 1.092 | 1.432 | 0.881 | 0.471 | 0.066 | 0.050 |
47 | 48 | Romania | 6.070 | 1.162 | 1.232 | 0.825 | 0.462 | 0.083 | 0.005 |
48 | 49 | Cyprus | 6.046 | 1.263 | 1.223 | 1.042 | 0.406 | 0.190 | 0.041 |
49 | 50 | Ecuador | 6.028 | 0.912 | 1.312 | 0.868 | 0.498 | 0.126 | 0.087 |
50 | 51 | Kuwait | 6.021 | 1.500 | 1.319 | 0.808 | 0.493 | 0.142 | 0.097 |
51 | 52 | Thailand | 6.008 | 1.050 | 1.409 | 0.828 | 0.557 | 0.359 | 0.028 |
52 | 53 | Latvia | 5.940 | 1.187 | 1.465 | 0.812 | 0.264 | 0.075 | 0.064 |
53 | 54 | South Korea | 5.895 | 1.301 | 1.219 | 1.036 | 0.159 | 0.175 | 0.056 |
54 | 55 | Estonia | 5.893 | 1.237 | 1.528 | 0.874 | 0.495 | 0.103 | 0.161 |
55 | 56 | Jamaica | 5.890 | 0.831 | 1.478 | 0.831 | 0.490 | 0.107 | 0.028 |
56 | 57 | Mauritius | 5.888 | 1.120 | 1.402 | 0.798 | 0.498 | 0.215 | 0.060 |
57 | 58 | Japan | 5.886 | 1.327 | 1.419 | 1.088 | 0.445 | 0.069 | 0.140 |
58 | 59 | Honduras | 5.860 | 0.642 | 1.236 | 0.828 | 0.507 | 0.246 | 0.078 |
59 | 60 | Kazakhstan | 5.809 | 1.173 | 1.508 | 0.729 | 0.410 | 0.146 | 0.096 |
60 | 61 | Bolivia | 5.779 | 0.776 | 1.209 | 0.706 | 0.511 | 0.137 | 0.064 |
61 | 62 | Hungary | 5.758 | 1.201 | 1.410 | 0.828 | 0.199 | 0.081 | 0.020 |
62 | 63 | Paraguay | 5.743 | 0.855 | 1.475 | 0.777 | 0.514 | 0.184 | 0.080 |
63 | 64 | Northern Cyprus | 5.718 | 1.263 | 1.252 | 1.042 | 0.417 | 0.191 | 0.162 |
64 | 65 | Peru | 5.697 | 0.960 | 1.274 | 0.854 | 0.455 | 0.083 | 0.027 |
65 | 66 | Portugal | 5.693 | 1.221 | 1.431 | 0.999 | 0.508 | 0.047 | 0.025 |
66 | 67 | Pakistan | 5.653 | 0.677 | 0.886 | 0.535 | 0.313 | 0.220 | 0.098 |
67 | 68 | Russia | 5.648 | 1.183 | 1.452 | 0.726 | 0.334 | 0.082 | 0.031 |
68 | 69 | Philippines | 5.631 | 0.807 | 1.293 | 0.657 | 0.558 | 0.117 | 0.107 |
69 | 70 | Serbia | 5.603 | 1.004 | 1.383 | 0.854 | 0.282 | 0.137 | 0.039 |
70 | 71 | Moldova | 5.529 | 0.685 | 1.328 | 0.739 | 0.245 | 0.181 | 0.000 |
71 | 72 | Libya | 5.525 | 1.044 | 1.303 | 0.673 | 0.416 | 0.133 | 0.152 |
72 | 73 | Montenegro | 5.523 | 1.051 | 1.361 | 0.871 | 0.197 | 0.142 | 0.080 |
73 | 74 | Tajikistan | 5.467 | 0.493 | 1.098 | 0.718 | 0.389 | 0.230 | 0.144 |
74 | 75 | Croatia | 5.432 | 1.155 | 1.266 | 0.914 | 0.296 | 0.119 | 0.022 |
75 | 76 | Hong Kong | 5.430 | 1.438 | 1.277 | 1.122 | 0.440 | 0.258 | 0.287 |
76 | 77 | Dominican Republic | 5.425 | 1.015 | 1.401 | 0.779 | 0.497 | 0.113 | 0.101 |
77 | 78 | Bosnia and Herzegovina | 5.386 | 0.945 | 1.212 | 0.845 | 0.212 | 0.263 | 0.006 |
78 | 79 | Turkey | 5.373 | 1.183 | 1.360 | 0.808 | 0.195 | 0.083 | 0.106 |
79 | 80 | Malaysia | 5.339 | 1.221 | 1.171 | 0.828 | 0.508 | 0.260 | 0.024 |
80 | 81 | Belarus | 5.323 | 1.067 | 1.465 | 0.789 | 0.235 | 0.094 | 0.142 |
81 | 82 | Greece | 5.287 | 1.181 | 1.156 | 0.999 | 0.067 | 0.000 | 0.034 |
82 | 83 | Mongolia | 5.285 | 0.948 | 1.531 | 0.667 | 0.317 | 0.235 | 0.038 |
83 | 84 | North Macedonia | 5.274 | 0.983 | 1.294 | 0.838 | 0.345 | 0.185 | 0.034 |
84 | 85 | Nigeria | 5.265 | 0.696 | 1.111 | 0.245 | 0.426 | 0.215 | 0.041 |
85 | 86 | Kyrgyzstan | 5.261 | 0.551 | 1.438 | 0.723 | 0.508 | 0.300 | 0.023 |
86 | 87 | Turkmenistan | 5.247 | 1.052 | 1.538 | 0.657 | 0.394 | 0.244 | 0.028 |
87 | 88 | Algeria | 5.211 | 1.002 | 1.160 | 0.785 | 0.086 | 0.073 | 0.114 |
88 | 89 | Morocco | 5.208 | 0.801 | 0.782 | 0.782 | 0.418 | 0.036 | 0.076 |
89 | 90 | Azerbaijan | 5.208 | 1.043 | 1.147 | 0.769 | 0.351 | 0.035 | 0.182 |
90 | 91 | Lebanon | 5.197 | 0.987 | 1.224 | 0.815 | 0.216 | 0.166 | 0.027 |
91 | 92 | Indonesia | 5.192 | 0.931 | 1.203 | 0.660 | 0.491 | 0.498 | 0.028 |
92 | 93 | China | 5.191 | 1.029 | 1.125 | 0.893 | 0.521 | 0.058 | 0.100 |
93 | 94 | Vietnam | 5.175 | 0.741 | 1.346 | 0.851 | 0.543 | 0.147 | 0.073 |
94 | 95 | Bhutan | 5.082 | 0.813 | 1.321 | 0.604 | 0.457 | 0.370 | 0.167 |
95 | 96 | Cameroon | 5.044 | 0.549 | 0.910 | 0.331 | 0.381 | 0.187 | 0.037 |
96 | 97 | Bulgaria | 5.011 | 1.092 | 1.513 | 0.815 | 0.311 | 0.081 | 0.004 |
97 | 98 | Ghana | 4.996 | 0.611 | 0.868 | 0.486 | 0.381 | 0.245 | 0.040 |
98 | 99 | Ivory Coast | 4.944 | 0.569 | 0.808 | 0.232 | 0.352 | 0.154 | 0.090 |
99 | 100 | Nepal | 4.913 | 0.446 | 1.226 | 0.677 | 0.439 | 0.285 | 0.089 |
100 | 101 | Jordan | 4.906 | 0.837 | 1.225 | 0.815 | 0.383 | 0.110 | 0.130 |
101 | 102 | Benin | 4.883 | 0.393 | 0.437 | 0.397 | 0.349 | 0.175 | 0.082 |
102 | 103 | Congo (Brazzaville) | 4.812 | 0.673 | 0.799 | 0.508 | 0.372 | 0.105 | 0.093 |
103 | 104 | Gabon | 4.799 | 1.057 | 1.183 | 0.571 | 0.295 | 0.043 | 0.055 |
104 | 105 | Laos | 4.796 | 0.764 | 1.030 | 0.551 | 0.547 | 0.266 | 0.164 |
105 | 106 | South Africa | 4.722 | 0.960 | 1.351 | 0.469 | 0.389 | 0.130 | 0.055 |
106 | 107 | Albania | 4.719 | 0.947 | 0.848 | 0.874 | 0.383 | 0.178 | 0.027 |
107 | 108 | Venezuela | 4.707 | 0.960 | 1.427 | 0.805 | 0.154 | 0.064 | 0.047 |
108 | 109 | Cambodia | 4.700 | 0.574 | 1.122 | 0.637 | 0.609 | 0.232 | 0.062 |
109 | 110 | Palestinian Territories | 4.696 | 0.657 | 1.247 | 0.672 | 0.225 | 0.103 | 0.066 |
110 | 111 | Senegal | 4.681 | 0.450 | 1.134 | 0.571 | 0.292 | 0.153 | 0.072 |
111 | 112 | Somalia | 4.668 | 0.000 | 0.698 | 0.268 | 0.559 | 0.243 | 0.270 |
112 | 113 | Namibia | 4.639 | 0.879 | 1.313 | 0.477 | 0.401 | 0.070 | 0.056 |
113 | 114 | Niger | 4.628 | 0.138 | 0.774 | 0.366 | 0.318 | 0.188 | 0.102 |
114 | 115 | Burkina Faso | 4.587 | 0.331 | 1.056 | 0.380 | 0.255 | 0.177 | 0.113 |
115 | 116 | Armenia | 4.559 | 0.850 | 1.055 | 0.815 | 0.283 | 0.095 | 0.064 |
116 | 117 | Iran | 4.548 | 1.100 | 0.842 | 0.785 | 0.305 | 0.270 | 0.125 |
117 | 118 | Guinea | 4.534 | 0.380 | 0.829 | 0.375 | 0.332 | 0.207 | 0.086 |
118 | 119 | Georgia | 4.519 | 0.886 | 0.666 | 0.752 | 0.346 | 0.043 | 0.164 |
119 | 120 | Gambia | 4.516 | 0.308 | 0.939 | 0.428 | 0.382 | 0.269 | 0.167 |
120 | 121 | Kenya | 4.509 | 0.512 | 0.983 | 0.581 | 0.431 | 0.372 | 0.053 |
121 | 122 | Mauritania | 4.490 | 0.570 | 1.167 | 0.489 | 0.066 | 0.106 | 0.088 |
122 | 123 | Mozambique | 4.466 | 0.204 | 0.986 | 0.390 | 0.494 | 0.197 | 0.138 |
123 | 124 | Tunisia | 4.461 | 0.921 | 1.000 | 0.815 | 0.167 | 0.059 | 0.055 |
124 | 125 | Bangladesh | 4.456 | 0.562 | 0.928 | 0.723 | 0.527 | 0.166 | 0.143 |
125 | 126 | Iraq | 4.437 | 1.043 | 0.980 | 0.574 | 0.241 | 0.148 | 0.089 |
126 | 127 | Congo (Kinshasa) | 4.418 | 0.094 | 1.125 | 0.357 | 0.269 | 0.212 | 0.053 |
127 | 128 | Mali | 4.390 | 0.385 | 1.105 | 0.308 | 0.327 | 0.153 | 0.052 |
128 | 129 | Sierra Leone | 4.374 | 0.268 | 0.841 | 0.242 | 0.309 | 0.252 | 0.045 |
129 | 130 | Sri Lanka | 4.366 | 0.949 | 1.265 | 0.831 | 0.470 | 0.244 | 0.047 |
130 | 131 | Myanmar | 4.360 | 0.710 | 1.181 | 0.555 | 0.525 | 0.566 | 0.172 |
131 | 132 | Chad | 4.350 | 0.350 | 0.766 | 0.192 | 0.174 | 0.198 | 0.078 |
132 | 133 | Ukraine | 4.332 | 0.820 | 1.390 | 0.739 | 0.178 | 0.187 | 0.010 |
133 | 134 | Ethiopia | 4.286 | 0.336 | 1.033 | 0.532 | 0.344 | 0.209 | 0.100 |
134 | 135 | Swaziland | 4.212 | 0.811 | 1.149 | 0.000 | 0.313 | 0.074 | 0.135 |
135 | 136 | Uganda | 4.189 | 0.332 | 1.069 | 0.443 | 0.356 | 0.252 | 0.060 |
136 | 137 | Egypt | 4.166 | 0.913 | 1.039 | 0.644 | 0.241 | 0.076 | 0.067 |
137 | 138 | Zambia | 4.107 | 0.578 | 1.058 | 0.426 | 0.431 | 0.247 | 0.087 |
138 | 139 | Togo | 4.085 | 0.275 | 0.572 | 0.410 | 0.293 | 0.177 | 0.085 |
139 | 140 | India | 4.015 | 0.755 | 0.765 | 0.588 | 0.498 | 0.200 | 0.085 |
140 | 141 | Liberia | 3.975 | 0.073 | 0.922 | 0.443 | 0.370 | 0.233 | 0.033 |
141 | 142 | Comoros | 3.973 | 0.274 | 0.757 | 0.505 | 0.142 | 0.275 | 0.078 |
142 | 143 | Madagascar | 3.933 | 0.274 | 0.916 | 0.555 | 0.148 | 0.169 | 0.041 |
143 | 144 | Lesotho | 3.802 | 0.489 | 1.169 | 0.168 | 0.359 | 0.107 | 0.093 |
144 | 145 | Burundi | 3.775 | 0.046 | 0.447 | 0.380 | 0.220 | 0.176 | 0.180 |
145 | 146 | Zimbabwe | 3.663 | 0.366 | 1.114 | 0.433 | 0.361 | 0.151 | 0.089 |
146 | 147 | Haiti | 3.597 | 0.323 | 0.688 | 0.449 | 0.026 | 0.419 | 0.110 |
147 | 148 | Botswana | 3.488 | 1.041 | 1.145 | 0.538 | 0.455 | 0.025 | 0.100 |
148 | 149 | Syria | 3.462 | 0.619 | 0.378 | 0.440 | 0.013 | 0.331 | 0.141 |
149 | 150 | Malawi | 3.410 | 0.191 | 0.560 | 0.495 | 0.443 | 0.218 | 0.089 |
150 | 151 | Yemen | 3.380 | 0.287 | 1.163 | 0.463 | 0.143 | 0.108 | 0.077 |
151 | 152 | Rwanda | 3.334 | 0.359 | 0.711 | 0.614 | 0.555 | 0.217 | 0.411 |
152 | 153 | Tanzania | 3.231 | 0.476 | 0.885 | 0.499 | 0.417 | 0.276 | 0.147 |
153 | 154 | Afghanistan | 3.203 | 0.350 | 0.517 | 0.361 | 0.000 | 0.158 | 0.025 |
154 | 155 | Central African Republic | 3.083 | 0.026 | 0.000 | 0.105 | 0.225 | 0.235 | 0.035 |
155 | 156 | South Sudan | 2.853 | 0.306 | 0.575 | 0.295 | 0.010 | 0.202 | 0.091 |
# Rename the columns to make it more easy to understand
happiness = happiness.rename(columns = {'Overall rank': 'Rank', 'Country or region': 'Country',
'GDP per capita': 'Economy (GDP per Capita)', 'Social support': 'Social Support',
'Healthy life expectancy': 'Health (Life Expectancy)',
'Freedom to make life choices': 'Freedom',
'Perceptions of corruption': 'Perception of Corruption'})
#cleaning null value and inconsistent data
happiness.drop(index=86,axis=0 , inplace=True)
happiness.drop(index=96,axis=0 , inplace=True)
happiness.drop(index=98,axis=0 , inplace=True)
happiness.drop(index=111,axis=0 , inplace=True)
happiness.drop(index=117,axis=0 , inplace=True)
happiness.drop(index=119,axis=0 , inplace=True)
happiness.drop(index=125,axis=0 , inplace=True)
happiness.drop(index=134,axis=0 , inplace=True)
happiness.drop(index=147,axis=0 , inplace=True)
# Dropping the unimportant columns
happiness.drop(['Rank'], axis = 1)
Country | Score | Economy (GDP per Capita) | Social Support | Health (Life Expectancy) | Freedom | Generosity | Perception of Corruption | |
---|---|---|---|---|---|---|---|---|
0 | Finland | 7.769 | 1.340 | 1.587 | 0.986 | 0.596 | 0.153 | 0.393 |
1 | Denmark | 7.600 | 1.383 | 1.573 | 0.996 | 0.592 | 0.252 | 0.410 |
2 | Norway | 7.554 | 1.488 | 1.582 | 1.028 | 0.603 | 0.271 | 0.341 |
3 | Iceland | 7.494 | 1.380 | 1.624 | 1.026 | 0.591 | 0.354 | 0.118 |
4 | Netherlands | 7.488 | 1.396 | 1.522 | 0.999 | 0.557 | 0.322 | 0.298 |
5 | Switzerland | 7.480 | 1.452 | 1.526 | 1.052 | 0.572 | 0.263 | 0.343 |
6 | Sweden | 7.343 | 1.387 | 1.487 | 1.009 | 0.574 | 0.267 | 0.373 |
7 | New Zealand | 7.307 | 1.303 | 1.557 | 1.026 | 0.585 | 0.330 | 0.380 |
8 | Canada | 7.278 | 1.365 | 1.505 | 1.039 | 0.584 | 0.285 | 0.308 |
9 | Austria | 7.246 | 1.376 | 1.475 | 1.016 | 0.532 | 0.244 | 0.226 |
10 | Australia | 7.228 | 1.372 | 1.548 | 1.036 | 0.557 | 0.332 | 0.290 |
11 | Costa Rica | 7.167 | 1.034 | 1.441 | 0.963 | 0.558 | 0.144 | 0.093 |
12 | Israel | 7.139 | 1.276 | 1.455 | 1.029 | 0.371 | 0.261 | 0.082 |
13 | Luxembourg | 7.090 | 1.609 | 1.479 | 1.012 | 0.526 | 0.194 | 0.316 |
14 | United Kingdom | 7.054 | 1.333 | 1.538 | 0.996 | 0.450 | 0.348 | 0.278 |
15 | Ireland | 7.021 | 1.499 | 1.553 | 0.999 | 0.516 | 0.298 | 0.310 |
16 | Germany | 6.985 | 1.373 | 1.454 | 0.987 | 0.495 | 0.261 | 0.265 |
17 | Belgium | 6.923 | 1.356 | 1.504 | 0.986 | 0.473 | 0.160 | 0.210 |
18 | United States | 6.892 | 1.433 | 1.457 | 0.874 | 0.454 | 0.280 | 0.128 |
19 | Czech Republic | 6.852 | 1.269 | 1.487 | 0.920 | 0.457 | 0.046 | 0.036 |
20 | United Arab Emirates | 6.825 | 1.503 | 1.310 | 0.825 | 0.598 | 0.262 | 0.182 |
21 | Malta | 6.726 | 1.300 | 1.520 | 0.999 | 0.564 | 0.375 | 0.151 |
22 | Mexico | 6.595 | 1.070 | 1.323 | 0.861 | 0.433 | 0.074 | 0.073 |
23 | France | 6.592 | 1.324 | 1.472 | 1.045 | 0.436 | 0.111 | 0.183 |
24 | Taiwan | 6.446 | 1.368 | 1.430 | 0.914 | 0.351 | 0.242 | 0.097 |
25 | Chile | 6.444 | 1.159 | 1.369 | 0.920 | 0.357 | 0.187 | 0.056 |
26 | Guatemala | 6.436 | 0.800 | 1.269 | 0.746 | 0.535 | 0.175 | 0.078 |
27 | Saudi Arabia | 6.375 | 1.403 | 1.357 | 0.795 | 0.439 | 0.080 | 0.132 |
28 | Qatar | 6.374 | 1.684 | 1.313 | 0.871 | 0.555 | 0.220 | 0.167 |
29 | Spain | 6.354 | 1.286 | 1.484 | 1.062 | 0.362 | 0.153 | 0.079 |
30 | Panama | 6.321 | 1.149 | 1.442 | 0.910 | 0.516 | 0.109 | 0.054 |
31 | Brazil | 6.300 | 1.004 | 1.439 | 0.802 | 0.390 | 0.099 | 0.086 |
32 | Uruguay | 6.293 | 1.124 | 1.465 | 0.891 | 0.523 | 0.127 | 0.150 |
33 | Singapore | 6.262 | 1.572 | 1.463 | 1.141 | 0.556 | 0.271 | 0.453 |
34 | El Salvador | 6.253 | 0.794 | 1.242 | 0.789 | 0.430 | 0.093 | 0.074 |
35 | Italy | 6.223 | 1.294 | 1.488 | 1.039 | 0.231 | 0.158 | 0.030 |
36 | Bahrain | 6.199 | 1.362 | 1.368 | 0.871 | 0.536 | 0.255 | 0.110 |
37 | Slovakia | 6.198 | 1.246 | 1.504 | 0.881 | 0.334 | 0.121 | 0.014 |
38 | Trinidad & Tobago | 6.192 | 1.231 | 1.477 | 0.713 | 0.489 | 0.185 | 0.016 |
39 | Poland | 6.182 | 1.206 | 1.438 | 0.884 | 0.483 | 0.117 | 0.050 |
40 | Uzbekistan | 6.174 | 0.745 | 1.529 | 0.756 | 0.631 | 0.322 | 0.240 |
41 | Lithuania | 6.149 | 1.238 | 1.515 | 0.818 | 0.291 | 0.043 | 0.042 |
42 | Colombia | 6.125 | 0.985 | 1.410 | 0.841 | 0.470 | 0.099 | 0.034 |
43 | Slovenia | 6.118 | 1.258 | 1.523 | 0.953 | 0.564 | 0.144 | 0.057 |
44 | Nicaragua | 6.105 | 0.694 | 1.325 | 0.835 | 0.435 | 0.200 | 0.127 |
45 | Kosovo | 6.100 | 0.882 | 1.232 | 0.758 | 0.489 | 0.262 | 0.006 |
46 | Argentina | 6.086 | 1.092 | 1.432 | 0.881 | 0.471 | 0.066 | 0.050 |
47 | Romania | 6.070 | 1.162 | 1.232 | 0.825 | 0.462 | 0.083 | 0.005 |
48 | Cyprus | 6.046 | 1.263 | 1.223 | 1.042 | 0.406 | 0.190 | 0.041 |
49 | Ecuador | 6.028 | 0.912 | 1.312 | 0.868 | 0.498 | 0.126 | 0.087 |
50 | Kuwait | 6.021 | 1.500 | 1.319 | 0.808 | 0.493 | 0.142 | 0.097 |
51 | Thailand | 6.008 | 1.050 | 1.409 | 0.828 | 0.557 | 0.359 | 0.028 |
52 | Latvia | 5.940 | 1.187 | 1.465 | 0.812 | 0.264 | 0.075 | 0.064 |
53 | South Korea | 5.895 | 1.301 | 1.219 | 1.036 | 0.159 | 0.175 | 0.056 |
54 | Estonia | 5.893 | 1.237 | 1.528 | 0.874 | 0.495 | 0.103 | 0.161 |
55 | Jamaica | 5.890 | 0.831 | 1.478 | 0.831 | 0.490 | 0.107 | 0.028 |
56 | Mauritius | 5.888 | 1.120 | 1.402 | 0.798 | 0.498 | 0.215 | 0.060 |
57 | Japan | 5.886 | 1.327 | 1.419 | 1.088 | 0.445 | 0.069 | 0.140 |
58 | Honduras | 5.860 | 0.642 | 1.236 | 0.828 | 0.507 | 0.246 | 0.078 |
59 | Kazakhstan | 5.809 | 1.173 | 1.508 | 0.729 | 0.410 | 0.146 | 0.096 |
60 | Bolivia | 5.779 | 0.776 | 1.209 | 0.706 | 0.511 | 0.137 | 0.064 |
61 | Hungary | 5.758 | 1.201 | 1.410 | 0.828 | 0.199 | 0.081 | 0.020 |
62 | Paraguay | 5.743 | 0.855 | 1.475 | 0.777 | 0.514 | 0.184 | 0.080 |
63 | Northern Cyprus | 5.718 | 1.263 | 1.252 | 1.042 | 0.417 | 0.191 | 0.162 |
64 | Peru | 5.697 | 0.960 | 1.274 | 0.854 | 0.455 | 0.083 | 0.027 |
65 | Portugal | 5.693 | 1.221 | 1.431 | 0.999 | 0.508 | 0.047 | 0.025 |
66 | Pakistan | 5.653 | 0.677 | 0.886 | 0.535 | 0.313 | 0.220 | 0.098 |
67 | Russia | 5.648 | 1.183 | 1.452 | 0.726 | 0.334 | 0.082 | 0.031 |
68 | Philippines | 5.631 | 0.807 | 1.293 | 0.657 | 0.558 | 0.117 | 0.107 |
69 | Serbia | 5.603 | 1.004 | 1.383 | 0.854 | 0.282 | 0.137 | 0.039 |
70 | Moldova | 5.529 | 0.685 | 1.328 | 0.739 | 0.245 | 0.181 | 0.000 |
71 | Libya | 5.525 | 1.044 | 1.303 | 0.673 | 0.416 | 0.133 | 0.152 |
72 | Montenegro | 5.523 | 1.051 | 1.361 | 0.871 | 0.197 | 0.142 | 0.080 |
73 | Tajikistan | 5.467 | 0.493 | 1.098 | 0.718 | 0.389 | 0.230 | 0.144 |
74 | Croatia | 5.432 | 1.155 | 1.266 | 0.914 | 0.296 | 0.119 | 0.022 |
75 | Hong Kong | 5.430 | 1.438 | 1.277 | 1.122 | 0.440 | 0.258 | 0.287 |
76 | Dominican Republic | 5.425 | 1.015 | 1.401 | 0.779 | 0.497 | 0.113 | 0.101 |
77 | Bosnia and Herzegovina | 5.386 | 0.945 | 1.212 | 0.845 | 0.212 | 0.263 | 0.006 |
78 | Turkey | 5.373 | 1.183 | 1.360 | 0.808 | 0.195 | 0.083 | 0.106 |
79 | Malaysia | 5.339 | 1.221 | 1.171 | 0.828 | 0.508 | 0.260 | 0.024 |
80 | Belarus | 5.323 | 1.067 | 1.465 | 0.789 | 0.235 | 0.094 | 0.142 |
81 | Greece | 5.287 | 1.181 | 1.156 | 0.999 | 0.067 | 0.000 | 0.034 |
82 | Mongolia | 5.285 | 0.948 | 1.531 | 0.667 | 0.317 | 0.235 | 0.038 |
83 | North Macedonia | 5.274 | 0.983 | 1.294 | 0.838 | 0.345 | 0.185 | 0.034 |
84 | Nigeria | 5.265 | 0.696 | 1.111 | 0.245 | 0.426 | 0.215 | 0.041 |
85 | Kyrgyzstan | 5.261 | 0.551 | 1.438 | 0.723 | 0.508 | 0.300 | 0.023 |
87 | Algeria | 5.211 | 1.002 | 1.160 | 0.785 | 0.086 | 0.073 | 0.114 |
88 | Morocco | 5.208 | 0.801 | 0.782 | 0.782 | 0.418 | 0.036 | 0.076 |
89 | Azerbaijan | 5.208 | 1.043 | 1.147 | 0.769 | 0.351 | 0.035 | 0.182 |
90 | Lebanon | 5.197 | 0.987 | 1.224 | 0.815 | 0.216 | 0.166 | 0.027 |
91 | Indonesia | 5.192 | 0.931 | 1.203 | 0.660 | 0.491 | 0.498 | 0.028 |
92 | China | 5.191 | 1.029 | 1.125 | 0.893 | 0.521 | 0.058 | 0.100 |
93 | Vietnam | 5.175 | 0.741 | 1.346 | 0.851 | 0.543 | 0.147 | 0.073 |
94 | Bhutan | 5.082 | 0.813 | 1.321 | 0.604 | 0.457 | 0.370 | 0.167 |
95 | Cameroon | 5.044 | 0.549 | 0.910 | 0.331 | 0.381 | 0.187 | 0.037 |
97 | Ghana | 4.996 | 0.611 | 0.868 | 0.486 | 0.381 | 0.245 | 0.040 |
99 | Nepal | 4.913 | 0.446 | 1.226 | 0.677 | 0.439 | 0.285 | 0.089 |
100 | Jordan | 4.906 | 0.837 | 1.225 | 0.815 | 0.383 | 0.110 | 0.130 |
101 | Benin | 4.883 | 0.393 | 0.437 | 0.397 | 0.349 | 0.175 | 0.082 |
102 | Congo (Brazzaville) | 4.812 | 0.673 | 0.799 | 0.508 | 0.372 | 0.105 | 0.093 |
103 | Gabon | 4.799 | 1.057 | 1.183 | 0.571 | 0.295 | 0.043 | 0.055 |
104 | Laos | 4.796 | 0.764 | 1.030 | 0.551 | 0.547 | 0.266 | 0.164 |
105 | South Africa | 4.722 | 0.960 | 1.351 | 0.469 | 0.389 | 0.130 | 0.055 |
106 | Albania | 4.719 | 0.947 | 0.848 | 0.874 | 0.383 | 0.178 | 0.027 |
107 | Venezuela | 4.707 | 0.960 | 1.427 | 0.805 | 0.154 | 0.064 | 0.047 |
108 | Cambodia | 4.700 | 0.574 | 1.122 | 0.637 | 0.609 | 0.232 | 0.062 |
109 | Palestinian Territories | 4.696 | 0.657 | 1.247 | 0.672 | 0.225 | 0.103 | 0.066 |
110 | Senegal | 4.681 | 0.450 | 1.134 | 0.571 | 0.292 | 0.153 | 0.072 |
112 | Namibia | 4.639 | 0.879 | 1.313 | 0.477 | 0.401 | 0.070 | 0.056 |
113 | Niger | 4.628 | 0.138 | 0.774 | 0.366 | 0.318 | 0.188 | 0.102 |
114 | Burkina Faso | 4.587 | 0.331 | 1.056 | 0.380 | 0.255 | 0.177 | 0.113 |
115 | Armenia | 4.559 | 0.850 | 1.055 | 0.815 | 0.283 | 0.095 | 0.064 |
116 | Iran | 4.548 | 1.100 | 0.842 | 0.785 | 0.305 | 0.270 | 0.125 |
118 | Georgia | 4.519 | 0.886 | 0.666 | 0.752 | 0.346 | 0.043 | 0.164 |
120 | Kenya | 4.509 | 0.512 | 0.983 | 0.581 | 0.431 | 0.372 | 0.053 |
121 | Mauritania | 4.490 | 0.570 | 1.167 | 0.489 | 0.066 | 0.106 | 0.088 |
122 | Mozambique | 4.466 | 0.204 | 0.986 | 0.390 | 0.494 | 0.197 | 0.138 |
123 | Tunisia | 4.461 | 0.921 | 1.000 | 0.815 | 0.167 | 0.059 | 0.055 |
124 | Bangladesh | 4.456 | 0.562 | 0.928 | 0.723 | 0.527 | 0.166 | 0.143 |
126 | Congo (Kinshasa) | 4.418 | 0.094 | 1.125 | 0.357 | 0.269 | 0.212 | 0.053 |
127 | Mali | 4.390 | 0.385 | 1.105 | 0.308 | 0.327 | 0.153 | 0.052 |
128 | Sierra Leone | 4.374 | 0.268 | 0.841 | 0.242 | 0.309 | 0.252 | 0.045 |
129 | Sri Lanka | 4.366 | 0.949 | 1.265 | 0.831 | 0.470 | 0.244 | 0.047 |
130 | Myanmar | 4.360 | 0.710 | 1.181 | 0.555 | 0.525 | 0.566 | 0.172 |
131 | Chad | 4.350 | 0.350 | 0.766 | 0.192 | 0.174 | 0.198 | 0.078 |
132 | Ukraine | 4.332 | 0.820 | 1.390 | 0.739 | 0.178 | 0.187 | 0.010 |
133 | Ethiopia | 4.286 | 0.336 | 1.033 | 0.532 | 0.344 | 0.209 | 0.100 |
135 | Uganda | 4.189 | 0.332 | 1.069 | 0.443 | 0.356 | 0.252 | 0.060 |
136 | Egypt | 4.166 | 0.913 | 1.039 | 0.644 | 0.241 | 0.076 | 0.067 |
137 | Zambia | 4.107 | 0.578 | 1.058 | 0.426 | 0.431 | 0.247 | 0.087 |
138 | Togo | 4.085 | 0.275 | 0.572 | 0.410 | 0.293 | 0.177 | 0.085 |
139 | India | 4.015 | 0.755 | 0.765 | 0.588 | 0.498 | 0.200 | 0.085 |
140 | Liberia | 3.975 | 0.073 | 0.922 | 0.443 | 0.370 | 0.233 | 0.033 |
141 | Comoros | 3.973 | 0.274 | 0.757 | 0.505 | 0.142 | 0.275 | 0.078 |
142 | Madagascar | 3.933 | 0.274 | 0.916 | 0.555 | 0.148 | 0.169 | 0.041 |
143 | Lesotho | 3.802 | 0.489 | 1.169 | 0.168 | 0.359 | 0.107 | 0.093 |
144 | Burundi | 3.775 | 0.046 | 0.447 | 0.380 | 0.220 | 0.176 | 0.180 |
145 | Zimbabwe | 3.663 | 0.366 | 1.114 | 0.433 | 0.361 | 0.151 | 0.089 |
146 | Haiti | 3.597 | 0.323 | 0.688 | 0.449 | 0.026 | 0.419 | 0.110 |
148 | Syria | 3.462 | 0.619 | 0.378 | 0.440 | 0.013 | 0.331 | 0.141 |
149 | Malawi | 3.410 | 0.191 | 0.560 | 0.495 | 0.443 | 0.218 | 0.089 |
150 | Yemen | 3.380 | 0.287 | 1.163 | 0.463 | 0.143 | 0.108 | 0.077 |
151 | Rwanda | 3.334 | 0.359 | 0.711 | 0.614 | 0.555 | 0.217 | 0.411 |
152 | Tanzania | 3.231 | 0.476 | 0.885 | 0.499 | 0.417 | 0.276 | 0.147 |
153 | Afghanistan | 3.203 | 0.350 | 0.517 | 0.361 | 0.000 | 0.158 | 0.025 |
154 | Central African Republic | 3.083 | 0.026 | 0.000 | 0.105 | 0.225 | 0.235 | 0.035 |
155 | South Sudan | 2.853 | 0.306 | 0.575 | 0.295 | 0.010 | 0.202 | 0.091 |
# Checking if the rows containing any duplicate data or not
duplicate_rows_happiness = happiness[happiness.duplicated()]
print("number of dyplicate rows: ", duplicate_rows_happiness.shape)
number of dyplicate rows: (0, 9)
# Find the null values.
print(happiness.isnull().sum())
Rank 0
Country 0
Score 0
Economy (GDP per Capita) 0
Social Support 0
Health (Life Expectancy) 0
Freedom 0
Generosity 0
Perception of Corruption 0
dtype: int64
# Drop the missing values.
happiness = happiness.dropna()
happiness.count()
Rank 147 Country 147 Score 147 Economy (GDP per Capita) 147 Social Support 147 Health (Life Expectancy) 147 Freedom 147 Generosity 147 Perception of Corruption 147 dtype: int64
# After dropping the values
print(happiness.isnull().sum())
Rank 0
Country 0
Score 0
Economy (GDP per Capita) 0
Social Support 0
Health (Life Expectancy) 0
Freedom 0
Generosity 0
Perception of Corruption 0
dtype: int64
# Decision Tree Regression
x = pd.DataFrame(np.c_[happiness['Economy (GDP per Capita)'], happiness['Social Support'], happiness['Health (Life Expectancy)']],
columns = ['Economy (GDP per Capita)','Social Support','Health (Life Expectancy)'])
y = happiness['Score']
# split the data using train_test_split
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state=42)
# build the model
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor()
# Fitting Decision Tree Regression into dataset
regressor.fit(x_train, y_train)
y_pred = regressor.predict(x_test)
# test the accuracy/performance measurement using R2
print('R2: ', regressor.score(x_test, y_test))
R2: 0.5694117211173655
df = pd.DataFrame({'Real Values':y_test, 'Predicted Values':y_pred})
df
Real Values | Predicted Values | |
---|---|---|
132 | 4.332 | 5.261 |
51 | 6.008 | 5.523 |
146 | 3.597 | 4.085 |
19 | 6.852 | 6.118 |
107 | 4.707 | 6.125 |
12 | 7.139 | 6.223 |
76 | 5.425 | 5.523 |
31 | 6.300 | 6.125 |
81 | 5.287 | 5.339 |
9 | 7.246 | 6.985 |
26 | 6.436 | 6.253 |
99 | 4.913 | 5.779 |
152 | 3.231 | 4.996 |
67 | 5.648 | 5.888 |
142 | 3.933 | 4.286 |
66 | 5.653 | 4.996 |
18 | 6.892 | 6.825 |
69 | 5.603 | 6.125 |
131 | 4.350 | 4.374 |
30 | 6.321 | 6.293 |
29 | 6.354 | 6.223 |
108 | 4.700 | 4.166 |
36 | 6.199 | 6.446 |
124 | 4.456 | 5.208 |
55 | 5.890 | 5.743 |
22 | 6.595 | 5.523 |
64 | 5.697 | 5.386 |
141 | 3.973 | 4.286 |
82 | 5.285 | 4.696 |
11 | 7.167 | 5.323 |
# Visualising the Decision Tree Regression Results
X_grid = np.arange(0, 10)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(y_pred, y_test)
plt.title('Decision Tree Regression')
plt.xlabel('Predicted Score')
plt.ylabel('Score')
plt.show()
from sklearn import metrics
mse = metrics.mean_squared_error(y_test, y_pred)
mse
0.5222810333333335
# k-fold cross validation of decision tree regression
from sklearn.model_selection import cross_val_score
regressor = DecisionTreeRegressor()
first_cv_scores = cross_val_score(regressor, x, y, cv=5, scoring='neg_mean_squared_error')
second_cv_scores = cross_val_score(regressor, x, y, cv=10, scoring='neg_mean_squared_error')
print('mean in first_cv_scores is {0:.2f} and in second_cv_scores is {1:.2f}'.format(np.mean
(first_cv_scores),
np.mean
(second_cv_scores)))
# neg_mean_squared_error is better when close to 0.0
mean in first_cv_scores is -0.87 and in second_cv_scores is -0.68