<jupyter_start><jupyter_text>A2C model<jupyter_code>LENGTH_TRIAL = 500
EPISODES = 1
Generate_DATA = Generate_data(device)
Liste_activation,Final_score,Liste_position_agent,Liste_position_alien,Liste_action_agent= Generate_DATA.run_episodes(structure_network = "a2c",length_trial = LENGTH_TRIAL,number_episode = EPISODES)<jupyter_output>A.L.E: Arcade Learning Environment (version +978d2ce)
[Powered by Stella]<jupyter_text>Preprocessing<jupyter_code>Names_hook = ["Conv_1","Conv_2","Conv_3","fc1"]
analysis = Manifold_analysis(LENGTH_TRIAL)
Prepro_length = True
activity_layer = [[[[] for i in range(len(Names_hook))] for j in range(6)] for episode in range(EPISODES)]

for agent_number in range(6):
    print("agent_number",agent_number)
    for layer in range(len(Names_hook)):
        for episode in range(EPISODES):
            activity_layer[episode][agent_number][layer] = analysis.prepro(Liste_activation[episode][agent_number][layer],Prepro_length)<jupyter_output>agent_number 0
agent_number 1
agent_number 2
agent_number 3
agent_number 4
agent_number 5<jupyter_text>PCA<jupyter_code>#fig = plt.figure(figsize=(20, 10))

L_threshold = [[[] for i in range(len(Names_hook))] for j in range(6)] 


for agent_number in range(6):    
    for episode in range(EPISODES):
        for i in range(len(Names_hook)):

            X = np.transpose(activity_layer[episode][agent_number][i])
            pca = PCA(n_components = 50) #we have 20 features
            pca.fit(X)
            variance = pca.explained_variance_ratio_ #calculate variance ratios
            var=np.cumsum(np.round(pca.explained_variance_ratio_, decimals=3)*100)
            #var #cumulative sum of variance explained with [n] features
            threshold = [i for i in range(len(var)) if var[i]>90]
            L_threshold[agent_number][i].append(threshold[0])<jupyter_output><empty_output><jupyter_text>Etude de l'influence de l'entrainement sur la dimension du manifold<jupyter_code>table = [["Init"],["1000"],["2000"],["3000"],["4000"],["5000"]]

for agent_number in range(6):
    for i in range(len(Names_hook)):
        table[agent_number].append(np.mean(L_threshold[agent_number][i]))

        
print(tabulate(table, headers=["# Training Epoch","Conv_1","Conv_2","Conv_3","fc1"],tablefmt ="fancy_grid"))<jupyter_output>╒════════════════════╤══════════╤══════════╤══════════╤═══════╕
│ # Training Epoch   │   Conv_1 │   Conv_2 │   Conv_3 │   fc1 │
╞════════════════════╪══════════╪══════════╪══════════╪═══════╡
│ Init               │        8 │        4 │        0 │     0 │
├────────────────────┼──────────┼──────────┼──────────┼───────┤
│ 1000               │        7 │        3 │        0 │     0 │
├────────────────────┼──────────┼──────────┼──────────┼───────┤
│ 2000               │        5 │        2 │        2 │     0 │
├────────────────────┼──────────┼──────────┼──────────┼───────┤
│ 3000               │        7 │        5 │        3 │     2 │
├────────────────────┼──────────┼──────────┼──────────┼───────┤
│ 4000               │        7 │        5 │        2 │     1 │
├────────────────────┼──────────┼──────────┼──────────┼───────┤
│ 5000               │        5 │        4 │        3 │     1 │
╘════════════════════╧══════════╧══════════╧══════════╧═══════╛<jupyter_text>Canonical Covariance Anaysis<jupyter_code>j=1
fig = plt.figure(figsize=(30,25))
for agent_number in range(6):
    for i in range(len(Names_hook)):
        X = np.transpose(activity_layer[0][agent_number][i])
        pca = PCA(n_components = 10) #we have 20 features
        pca.fit(X)
        ax = fig.add_subplot(6,len(Names_hook),j)
        ax.scatter(pca.components_[0],pca.components_[1])
        ax.set_title("(agent %s) %s" % (agent_number, Names_hook[layer]))           # plt.suptitle(table[agent_number][0], fontsize=20)


        
        if i ==0:
            ax.set_xlabel('PC1')
            ax.set_ylabel('PC2')
            
        j+=1
        


episode_s = 0
agent_s = 0
position_agent_s = Liste_position_agent[episode_s][agent_s]
position_alien_s = Liste_position_alien[episode_s][agent_s]
Liste_action_agent_s = Liste_action_agent[episode_s][agent_s]
print(len(position_agent_s[0:500]))<jupyter_output>500<jupyter_text>Creata DataFrame<jupyter_code>import pandas as pd  
  
# assign data of lists.  
data = {'PC1':pca.components_[0],'PC2':pca.components_[1],'PC3':pca.components_[2],'PC4':pca.components_[3],'PC5':pca.components_[4], 'Agent_Position':position_agent_s[0:500],'Alien_Position':position_alien_s[0:500],'Liste_action_agent':Liste_action_agent_s[0:500]}  


#data = {'PC1':pca.components_[0],'PC2':pca.components_[1],'PC3':pca.components_[2], 'Alien_Position':[np.random.randint(100) for i in range(500)],'Agent_Position':[np.random.randint(100) for i in range(500)],'Liste_action_agent':Liste_action_agent_s[0:500]}  
  
# Creates pandas DataFrame.  
df = pd.DataFrame(data, index =[str(i) for i in range(500)])  
  
# print the data<jupyter_output><empty_output><jupyter_text>Standardization<jupyter_code>X = df[['PC1','PC2','PC3','PC4','PC5']]
Y = df[["Agent_Position","Alien_Position"]]

X_mc = (X-X.mean())/(X.std())
Y_mc = (Y-Y.mean())/(Y.std())
ca = CCA()
ca.fit(X_mc, Y_mc)
X_c, Y_c = ca.transform(X_mc, Y_mc)
print(X_c.shape)
print(Y_c.shape)
cc_res = pd.DataFrame({"CCX_1":X_c[:, 0],
                       "CCY_1":Y_c[:, 0],
                       "CCX_2":X_c[:, 1],
                       "CCY_2":Y_c[:, 1],
                       "Actions":df.Liste_action_agent.tolist(),
                      })
np.corrcoef(X_c[:, 1], Y_c[:, 1])
np.corrcoef(X_c[:, 0], Y_c[:, 0])
sns.set_context("talk", font_scale=1.2)
plt.figure(figsize=(10,8))
sns.scatterplot(x="CCX_1",
                y="CCY_1", 
                data=cc_res)
plt.title('Comp. 1, corr = %.2f' %
         np.corrcoef(X_c[:, 0], Y_c[:, 0])[0, 1])
sns.set_context("talk", font_scale=1.2)
plt.figure(figsize=(10,8))
sns.scatterplot(x="CCX_2",
                y="CCY_2", 
                data=cc_res)
plt.title('Comp. 1, corr = %.2f' %
         np.corrcoef(X_c[:, 1], Y_c[:, 1])[0, 1])
plt.figure(figsize=(10,8))
sns.scatterplot(x="CCX_1",
                y="CCY_1", 
                hue="Actions", data=cc_res)
plt.title('First Pair of Canonical Covariate, corr = %.2f' %
         np.corrcoef(X_c[:, 0], Y_c[:, 0])[0, 1])
ccY_df = pd.DataFrame({"CCY_1":Y_c[:, 0],
                       "CCY_2":Y_c[:, 1],
                       "Actions":df.Liste_action_agent.astype('category').cat.codes,
                       "Agent_Position":Y_mc.Agent_Position,
                      "Alien_Position":Y_mc.Alien_Position})
corr_Y_df= ccY_df.corr(method='pearson') 
corr_Y_df.head()
plt.figure(figsize=(10,8))
Y_df_lt = corr_Y_df.where(np.tril(np.ones(corr_Y_df.shape)).astype(np.bool))

sns.heatmap(Y_df_lt,cmap="coolwarm",annot=True,fmt='.1g')
plt.tight_layout()
plt.savefig("Heatmap_Canonical_Correlates_from_Y_and_data.jpg",
                    format='jpeg',
                    dpi=100)
ccX_df = pd.DataFrame({"CCX_1":X_c[:, 0],
                       "CCX_2":X_c[:, 1],
                       "Actions":df.Liste_action_agent.astype('category').cat.codes,
                       "PC1":X_mc.PC1,
                      "PC2":X_mc.PC2,
                      "PC3":X_mc.PC3,
                      "PC4":X_mc.PC4,
                      "PC5":X_mc.PC5,
                      "Agent_Position":Y_mc.Agent_Position,
                      "Alien_Position":Y_mc.Alien_Position})
corr_X_df= ccX_df.corr(method='pearson') 
corr_X_df.head()
plt.figure(figsize=(20,16))
X_df_lt = corr_X_df.where(np.tril(np.ones(corr_X_df.shape)).astype(np.bool))
sns.heatmap(X_df_lt,cmap="coolwarm",annot=True,fmt='.1g')
plt.tight_layout()
plt.savefig("Heatmap_Canonical_Correlates_from_X_and_data.jpg",
                    format='jpeg',
                    dpi=100)

for k in range(4,5):
    print(k)
SBP = [120,109,130,121,135,140]
DBP = [76,80,82,78,85,87]
Height = [165,180,170,185,180,187]
Weight = [60,80,70,85,90,87]
data = {'SBP':SBP,'DBP':DBP,'Height':Height,'Weight':Weight}  

# Creates pandas DataFrame.  
df = pd.DataFrame(data, index =[str(i) for i in range(len(SBP))])  
X = df[['SBP','DBP']]
Y = df[['Height','Weight']]

#X_mc = (X-X.mean())/(X.std())
#Y_mc = (Y-Y.mean())/(Y.std())
ca = CCA(n_components=2,scale=False)
ca.fit(X, Y)
X_c, Y_c = ca.transform(X, Y)
print("X_c_1",X_c[:, 0])
print("X_c_2",X_c[:, 1])
print(ca.coef_)
valeur = 120*-1.29 + 75*-0.75
print(valeur)
print(ca.x_loadings_)
print(ca.feature_names_in_)
print(ca.y_loadings_)
cc_res = pd.DataFrame({"CCX_1":X_c[:, 0],
                       "CCY_1":Y_c[:, 0],
                       "CCX_2":X_c[:, 1],
                       "CCY_2":Y_c[:, 1]
                      })
print("Y_c_1",Y_c[:, 0])
print("Y_c_2",Y_c[:, 1])<jupyter_output>Y_c_1 [-11.42472374   0.2837574   -4.50589459   2.6785834    9.33176373
   3.6365138 ]
Y_c_2 [ 1.70881758 -2.0847964   3.7374866  -4.9959379   7.79482464 -6.1603945 ]
{'fraction_non_alphanumeric': 0.211978465679677, 'fraction_numerical': 0.05260206370569762, 'mean_word_length': 3.3035714285714284, 'pattern_counts': {'":': 24, '<': 24, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '438319', 'license': 'MIT', 'n_tokens_mistral': 4001, 'n_tokens_neox': 3510, 'n_words': 521, 'path': 'cgames/02_space_invader/Experiments/Experiments_notebooks/Predictive_learning_paper/Canonical Correlation Analaysis General .ipynb', 'repo_name': 'BeylierMPG/Reinforcement-Learning', 'size': '8916'}