Upload eda_363.py
Browse files- eda_363.py +98 -0
eda_363.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""eda.363
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1LWUvpKSaZSgOHW-h4GzL9_0NM3RRIxTx
|
8 |
+
"""
|
9 |
+
|
10 |
+
import numpy as np
|
11 |
+
import pandas as pd
|
12 |
+
import matplotlib.pyplot as plt
|
13 |
+
import seaborn as sns
|
14 |
+
|
15 |
+
df = pd.read_csv("/content/World-happiness-report-updated_2024.csv", encoding='latin-1')
|
16 |
+
df.head(5)
|
17 |
+
|
18 |
+
df.describe()
|
19 |
+
|
20 |
+
df.info()
|
21 |
+
|
22 |
+
df.isnull().sum()
|
23 |
+
|
24 |
+
numeric_cols = df.select_dtypes(include=np.number).columns
|
25 |
+
df[numeric_cols] = df[numeric_cols].fillna(df[numeric_cols].mean())
|
26 |
+
|
27 |
+
df2024 = pd.read_csv("/content/World-happiness-report-2024.csv", encoding='latin-1')
|
28 |
+
df2024.head(5)
|
29 |
+
|
30 |
+
df2024.describe()
|
31 |
+
|
32 |
+
df2024.info()
|
33 |
+
|
34 |
+
df2024.isnull().sum()
|
35 |
+
|
36 |
+
numeric_cols = df2024.select_dtypes(include=np.number).columns
|
37 |
+
df2024[numeric_cols] = df2024[numeric_cols].fillna(df2024[numeric_cols].mean())
|
38 |
+
|
39 |
+
df2024['Country name'].unique()
|
40 |
+
|
41 |
+
sns.countplot(x = 'Regional indicator', data = df2024)
|
42 |
+
plt.xticks(rotation = 60)
|
43 |
+
plt.show()
|
44 |
+
|
45 |
+
list_features = ['Social support', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption']
|
46 |
+
sns.boxplot(data=df2024.loc[:,list_features],orient='h',palette = 'Set3')
|
47 |
+
plt.show()
|
48 |
+
|
49 |
+
list_features = ['Ladder score', 'Log GDP per capita']
|
50 |
+
sns.boxplot(data=df2024.loc[:,list_features],orient='h',palette = 'Set3')
|
51 |
+
plt.show()
|
52 |
+
|
53 |
+
df2024_happiest_unhappiest = df2024[(df2024.loc[:,'Ladder score']>7.4) | (df2024.loc[:,'Ladder score']<3.5)]
|
54 |
+
sns.barplot(x = 'Ladder score', y= 'Country name', data = df2024_happiest_unhappiest, palette = 'coolwarm')
|
55 |
+
plt.title('Happiest and Unhappiest Countries in 2024')
|
56 |
+
plt.show()
|
57 |
+
|
58 |
+
plt.figure(figsize=(15,8))
|
59 |
+
sns.kdeplot(x=df2024['Ladder score'], hue = df2024['Regional indicator'], fill = True, linewidth = 2)
|
60 |
+
plt.axvline(df2024['Ladder score'].mean(),c= 'black')
|
61 |
+
plt.title('Ladder Score Distribution by Regional Indicator')
|
62 |
+
plt.show()
|
63 |
+
|
64 |
+
import plotly.express as px
|
65 |
+
fig = px.choropleth(df.sort_values('year'),
|
66 |
+
locations='Country name',
|
67 |
+
color='Life Ladder',
|
68 |
+
locationmode = 'country names',
|
69 |
+
animation_frame = 'year')
|
70 |
+
fig.update_layout(title = 'Life Ladder Comparison by Countires')
|
71 |
+
|
72 |
+
df2024_generosity = df2024[(df2024.loc[:,'Generosity']>0.6)|(df2024.loc[:,'Generosity']<0.05)]
|
73 |
+
sns.barplot(x = 'Generosity', y = 'Country name', data = df2024_generosity, palette= 'coolwarm')
|
74 |
+
plt.title('Most Generous and Most Ungenerous Countries in 2024')
|
75 |
+
plt.show()
|
76 |
+
|
77 |
+
fig = px.choropleth(df.sort_values('year'),
|
78 |
+
locations = 'Country name',
|
79 |
+
color = 'Generosity',
|
80 |
+
locationmode = 'country names',
|
81 |
+
animation_frame = 'year')
|
82 |
+
fig.update_layout(title = 'Generosity Comparison by Countries')
|
83 |
+
fig.show()
|
84 |
+
|
85 |
+
sns.swarmplot(x = "Regional indicator", y = "Generosity", data = df2024, palette="Set1")
|
86 |
+
plt.xticks(rotation = 90)
|
87 |
+
plt.title("Generous Distribution by Regional Indicator in 2021")
|
88 |
+
plt.show()
|
89 |
+
|
90 |
+
non_numeric_columns = df.select_dtypes(exclude=['float64', 'int64']).columns
|
91 |
+
df_numeric = df.drop(columns=non_numeric_columns)
|
92 |
+
correlation_matrix = df_numeric.corr()
|
93 |
+
sns.heatmap(correlation_matrix, annot = True, fmt ='.2f', linewidth = .7)
|
94 |
+
plt.title('Relationship Between Features')
|
95 |
+
plt.show()
|
96 |
+
|
97 |
+
sns.clustermap(correlation_matrix, center = 0, cmap = 'vlag', dendrogram_ratio = (0.1,0.2), annot = True, linewidth = .7, figsize=(10,10))
|
98 |
+
plt.show()
|