<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
import pandas as pd
import pandas.util.testing as tm
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

print(f'Tensorflow version: {tf.__version__}')
happines_data = pd.read_csv('/content/drive/My Drive/Colab Notebooks/whr_all.csv', parse_dates=True, encoding = "cp1252")
happines_data.head()
X = happines_data[['GDP per capita',
                   'Social support', 
                   'Healthy life expectancy',
                   'Freedom to make life choices',
                   'Generosity',
                   'Perceptions of corruption']]

y = happines_data['Score']
# corr = happines_data.corr(method = "pearson")
corr = happines_data.corr(method = "spearman")
# corr = happines_data.corr(method = "kendall")

f, ax = plt.subplots(figsize=(10, 10))

sns.heatmap(corr, mask=np.zeros_like(corr, dtype=np.bool), cmap=sns.diverging_palette(220, 10, as_cmap=True), square=True, ax=ax, annot=True,)
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=0)
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(units=12, input_shape=(X_train.shape[1],), activation='relu'),
  tf.keras.layers.Dense(units=52, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(units=120, activation='exponential'),
  tf.keras.layers.Dense(units=34, activation='relu'),
  tf.keras.layers.Dense(units=14, activation='relu'),
  tf.keras.layers.Dense(1)
])

model.compile(optimizer='adam', loss='mse', metrics=['mae', 'mse'])
model.summary()
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=5)

reg = model.fit(X_train, y_train, validation_data=(X_test,y_test), epochs=15, callbacks=[callback])
fig, ax = plt.subplots(figsize=(25,8)) 

plt.plot(reg.history['mse'], label='mean_squared_error')
plt.plot(reg.history['val_mse'], label='val_mean_squared_error', linestyle='--')
plt.plot(reg.history['mae'], label='mean_absolute_error')
plt.plot(reg.history['val_mae'], label='val_mean_absolute_error', linestyle='--')
plt.legend()
correct=[]

for i in range(len(X)):
    a=happines_data.loc[i,"Score"]
    b=model.predict([[happines_data.loc[i,"GDP per capita"],
                      happines_data.loc[i,"Social support"],
                      happines_data.loc[i,"Healthy life expectancy"],
                      happines_data.loc[i,"Freedom to make life choices"],
                      happines_data.loc[i,"Generosity"],
                      happines_data.loc[i,"Perceptions of corruption"]]])[0]

    correct.append((abs(a-b)/a)[0])
    # print(f'{a:.4f} - {b[0]:.4f} - {(abs(a-b)/a)[0]:.4f}')

print(1-sum(correct) / len(correct))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1600951733514616, 'fraction_numerical': 0.015295717199184228, 'mean_word_length': 4.460111317254174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '549081', 'license': 'MIT', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1041, 'n_words': 173, 'path': 'Neural Networks/World Happiness Report.ipynb', 'repo_name': 'vislupus/machine-learning', 'size': '2942'}