image_output_5.png image_output_4.png text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png image_output_3.png image_output_2.png image_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error, r2_score

from sklearn.metrics import mean_squared_error, r2_score

from sklearn.model_selection import train_test_split

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import os

import pandas as pd

import pandas as pd

import seaborn as sns

import seaborn as sns

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        if filename == '2018.csv':
            data_path = os.path.join(dirname, filename)
data = pd.read_csv(data_path)
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
X = data[['GDP per capita', 'Social support', 'Healthy life expectancy', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption']]
y = data['Score']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
coeff_df = pd.DataFrame(model.coef_, X.columns, columns=['Coefficient'])

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        if filename == 'report_2018-2019.csv':
            data_path = os.path.join(dirname, filename)
data = pd.read_csv(data_path)
X = data.drop(['Overall rank', 'Country or region', 'Score', 'Year'], axis=1)
y = data['Score']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
reg = LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
coefficients = pd.DataFrame(reg.coef_, X.columns, columns=['Coefficient'])

plt.figure(figsize=(8, 6))
sns.histplot(data=data, x='Score', kde=True)
plt.title('Distribution of Happiness Scores')
plt.xlabel('Happiness Score')
plt.ylabel('Frequency')
plt.show()
yearly_data = data.groupby('Year').mean()
yearly_data.reset_index(level=0, inplace=True)
yearly_data.plot(x='Year', y=['Score', 'GDP per capita', 'Social support', 'Healthy life expectancy', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption'], kind='bar', subplots=True, layout=(4, 2), figsize=(15, 15), legend=False)
plt.tight_layout()
plt.show()
top_10_2019 = data[data['Year'] == 2019].sort_values('Score', ascending=False).head(10)
print('Top 10 countries by happiness score in 2019:\n', top_10_2019['Country or region'])
bottom_10_2019 = data[data['Year'] == 2019].sort_values('Score', ascending=True).head(10)
print('Bottom 10 countries by happiness score in 2019:\n', bottom_10_2019['Country or region'])
top_gdp_2019 = data[data['Year'] == 2019].sort_values('GDP per capita', ascending=False).head(5)
top_social_2019 = data[data['Year'] == 2019].sort_values('Social support', ascending=False).head(5)
top_health_2019 = data[data['Year'] == 2019].sort_values('Healthy life expectancy', ascending=False).head(5)
print('Top 5 countries by GDP per capita in 2019:\n', top_gdp_2019['Country or region'])
print('Top 5 countries by Social support in 2019:\n', top_social_2019['Country or region'])
print('Top 5 countries by Healthy life expectancy in 2019:\n', top_health_2019['Country or region'])
plt.figure(figsize=(8, 6))
sns.scatterplot(data=data, x='GDP per capita', y='Score', hue='Year', style='Year')
plt.title('GDP per Capita vs Happiness Score')
plt.xlabel('GDP per Capita')
plt.ylabel('Happiness Score')
plt.show()
plt.figure(figsize=(8, 6))
sns.scatterplot(data=data, x='Social support', y='Score', hue='Year', style='Year')
plt.title('Social Support vs Happiness Score')
plt.xlabel('Social Support')
plt.ylabel('Happiness Score')
plt.show()
plt.figure(figsize=(8, 6))
sns.scatterplot(data=data, x='Healthy life expectancy', y='Score', hue='Year', style='Year')
plt.title('Healthy Life Expectancy vs Happiness Score')
plt.xlabel('Healthy Life Expectancy')
plt.ylabel('Happiness Score')
plt.show()