<jupyter_start><jupyter_text>https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data<jupyter_code>import numpy as np
import pandas as pd
from sklearn.metrics import pairwise_distances
import matplotlib.pyplot as plt
from sklearn.preprocessing import RobustScaler

np.random.seed(0)<jupyter_output><empty_output><jupyter_text>1. Read Data<jupyter_code>df = pd.read_pickle('climate_change/df.pkl')
avg_temp_df = pd.read_pickle('climate_change/avg_temp_df.pkl')
std_temp_df = pd.read_pickle('climate_change/std_temp_df.pkl')
month_df = pd.read_pickle('climate_change/month_df.pkl')
sel_cities = pd.read_pickle('climate_change/sel_cities.pkl')<jupyter_output><empty_output><jupyter_text>2. Generate Graphs  2.1. Spatial Graph<jupyter_code>df['Latitude'] = df['Latitude'].str[:-1].astype(float)
df['Longitude'] = df['Longitude'].str[:-1].astype(float)
geo_df = df.groupby('City')[['Latitude', 'Longitude']].mean().loc[sel_cities]
alpha = 0.1
A_cities = np.exp(-alpha*pairwise_distances(geo_df))
A_cities[range(A_cities.shape[0]), range(A_cities.shape[0])] = 0
A_cities = pd.DataFrame(A_cities, index=geo_df.index, columns=geo_df.index)
# PLOTTING FUNCITONS
import networkx as nx

# cols = [f'C{c}' for c in A_cities.columns]
# A_cities.columns, A_cities.index = cols, cols

G = nx.from_pandas_adjacency(A_cities, create_using=nx.Graph)
edges = G.edges()
weights = [G[u][v]['weight']**8 *1 for u,v in edges]

plt.figure(figsize=(7, 7))
nx.draw_circular(G, edges=edges, with_labels=False, 
                 node_size=100, node_color='DARKORANGE', 
                 font_color='white', font_weight=1000, font_size=20,
                 width=weights, arrowsize=30)
                 #edge_color=weights, edge_cmap=plt.get_cmap("cool"), width=3)

plt.savefig('A_s.png', dpi=300)<jupyter_output><empty_output><jupyter_text>2.2. Time Graph<jupyter_code># Adjacency matrix as a constant variable (N x N)
lbw = 24
damping_constant = 0.9
A_t = np.zeros((lbw, lbw))
for row_idx in range(A_t.shape[0]):
    for col_idx in range(A_t.shape[1]):
        if col_idx > row_idx:
            A_t[row_idx, col_idx] = damping_constant**(col_idx-row_idx)

A_t = A_t.T
A_t = pd.DataFrame(A_t)

A_t.head(5)
# PLOTTING FUNCITONS
import networkx as nx

cols = [f'T{c}' for c in A_t.columns]
A_t.columns, A_t.index = cols, cols

G = nx.from_pandas_adjacency(A_t.T, create_using=nx.DiGraph)
edges = G.edges()
weights = [G[u][v]['weight']**3 *5 for u,v in edges]

plt.figure(figsize=(7, 7))
nx.draw_circular(G, edges=edges, with_labels=False, 
                 node_size=1000, node_color='GREEN', 
                 font_color='white', font_weight=1000, font_size=20,
                 width=weights, arrowsize=20)
                 #edge_color=weights, edge_cmap=plt.get_cmap("cool"), width=3)

plt.savefig('A_t.png', dpi=300)<jupyter_output><empty_output><jupyter_text>3. Generate Tensor Data<jupyter_code>X, Y = [], []
for t in avg_temp_df.index[lbw:]:
    X_sample = []
    X_sample.append(avg_temp_df.loc[:t].iloc[-lbw-1:-1].values)
    X_sample.append(std_temp_df.loc[:t].iloc[-lbw-1:-1].values)
    X_sample.append(month_df.loc[:t].iloc[-lbw-1:-1].values)
    X.append(np.array(X_sample))
    Y.append(avg_temp_df.loc[t].values)
    
X, Y = np.array(X), np.array(Y)
X = X.transpose((0, 2, 3, 1))
print(X.shape, Y.shape)
# Train-test split
split_idx = int(0.7*X.shape[0])
X_tr, Y_tr = X[:split_idx], Y[:split_idx]
X_te, Y_te = X[split_idx:], Y[split_idx:]<jupyter_output><empty_output><jupyter_text>4. Modelling- check the transpose of the time graph - use the learnable (I + \alpha * A) filter  4.1. MGTN<jupyter_code>import tensorflow as tf
from GraphTensorNetworks import SpecialMultiGraphTensorNetwork as SMGTN
from GraphTensorNetworks import TensorTrainLayer as TTL

tf.random.set_seed(0)
graph_list = [A_t.values, A_cities.values]
tt_ips = [X.shape[1], X.shape[2], 32]
tt_ops = [2,2,2]
tt_ranks = [1,2,2,1]
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape=X.shape[1:]))

model.add(SMGTN(tt_ips[-1], graph_list, bias_bool=False))
model.add(tf.keras.layers.Activation('tanh'))

model.add(TTL(tt_ips, tt_ops, tt_ranks))
model.add(tf.keras.layers.Activation('tanh'))
model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(Y.shape[1]))

model.compile(loss='mse', optimizer=tf.keras.optimizers.RMSprop(lr=0.01))

model.summary()
model.fit(X_tr, Y_tr, validation_split=0.2, epochs=30, shuffle=False, verbose=False)
from sklearn.metrics import mean_squared_error
preds = model.predict(X_tr)
mean_squared_error(preds.reshape(-1), Y_tr.reshape(-1))
preds = model.predict(X_te)
mean_squared_error(preds.reshape(-1), Y_te.reshape(-1))<jupyter_output><empty_output><jupyter_text>4.2. TTNN<jupyter_code>X_tr.shape
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape=X.shape[1:]))

model.add(TTL([24, 92, 3], [4, 4, 2], tt_ranks))
model.add(tf.keras.layers.Activation('tanh'))

model.add(TTL([4, 4, 2], tt_ops, tt_ranks))
model.add(tf.keras.layers.Activation('tanh'))
model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(Y.shape[1]))

model.compile(loss='mse', optimizer=tf.keras.optimizers.RMSprop(lr=0.01))

model.summary()
model.fit(X_tr, Y_tr, validation_split=0.2, epochs=30, shuffle=False, verbose=False)
preds = model.predict(X_tr)
mean_squared_error(preds.reshape(-1), Y_tr.reshape(-1))
preds = model.predict(X_te)
mean_squared_error(preds.reshape(-1), Y_te.reshape(-1))<jupyter_output><empty_output><jupyter_text>4.3. GRU Model<jupyter_code>X_tr_gru = X_tr.reshape(X_tr.shape[0], lbw, -1)
X_te_gru = X_te.reshape(X_te.shape[0], lbw, -1)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape=X_tr_gru.shape[1:]))

model.add(tf.keras.layers.GRU(32, return_sequences=True))
model.add(tf.keras.layers.Activation('tanh'))
model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(2**3))
model.add(tf.keras.layers.Activation('tanh'))

model.add(tf.keras.layers.Dense(Y.shape[1]))

model.compile(loss='mse', optimizer=tf.keras.optimizers.RMSprop(lr=0.01))

model.summary()
model.fit(X_tr_gru, Y_tr, validation_split=0.2, epochs=30, shuffle=False, verbose=False)
preds = model.predict(X_tr_gru)
mean_squared_error(preds.reshape(-1), Y_tr.reshape(-1))
preds = model.predict(X_te_gru)
mean_squared_error(preds.reshape(-1), Y_te.reshape(-1))<jupyter_output><empty_output><jupyter_text>4.4. GCN<jupyter_code>X_tr_gcn = X_tr.transpose(0, 2, 1, 3).reshape(X_tr.shape[0], A_cities.shape[0], -1)
X_te_gcn = X_te.transpose(0, 2, 1, 3).reshape(X_te.shape[0], A_cities.shape[0], -1)
from GraphTensorNetworks import GraphCNN
g_filter = tf.keras.backend.constant(A_cities.values + np.eye(A_cities.shape[0]))

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape=(A_cities.shape[0], X_tr_gcn.shape[-1])))

model.add(GraphCNN(output_dim=32, num_filters=1, graph_conv_filters=g_filter, input_shape=X_tr_gcn.shape[1:]))
model.add(tf.keras.layers.Activation('tanh'))

# model.add(GraphCNN(output_dim=8, num_filters=1, graph_conv_filters=g_filter, input_shape=X_tr_gcn.shape[1:]))
# model.add(tf.keras.layers.Activation('tanh'))
model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(3**3))
model.add(tf.keras.layers.Activation('tanh'))

model.add(tf.keras.layers.Dense(Y.shape[1]))

model.compile(loss='mse', optimizer=tf.keras.optimizers.RMSprop(lr=0.01))

model.summary()
model.fit(X_tr_gcn, Y_tr, validation_split=0.2, epochs=30, shuffle=False, verbose=False)
preds = model.predict(X_tr_gcn)
mean_squared_error(preds.reshape(-1), Y_tr.reshape(-1))
preds = model.predict(X_te_gcn)
mean_squared_error(preds.reshape(-1), Y_te.reshape(-1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17000520291363164, 'fraction_numerical': 0.02497398543184183, 'mean_word_length': 6.073597056117755, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '276247', 'license': 'MIT', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3230, 'n_words': 425, 'path': 'exp4_temperature/(DONE) Climate Change.ipynb', 'repo_name': 'gylx/mgtn', 'size': '7688'}