因此,我有一个样本数据集,从中提取了相关矩阵,并且针对每一列,我计算了平均相关性,现在希望将两者进行比较。为此,我用Python编写了以下代码。该代码将同时查看两组数据(相关矩阵和平均值),并消除所有高于平均值的值,然后在其余值之间绘制图形。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import pylab as pl
import networkx as nx
from time import time
filesrc = "F:\Sumit2\PR\PTD.csv"
Data = pd.read_csv(filesrc)
#selecting the number of independent variables
n = 9
mylist = list(Data.columns)
limit_list = mylist[0:n]
correlation_matrix = Data.corr(method = 'pearson')
print(correlation_matrix)
cmean = correlation_matrix.mean()
Reduced_corrmat = correlation_matrix.iloc[0:n,0:n]
Reduced_cmean = cmean.iloc[0:n,]
G = nx.DiGraph()
G.add_nodes_from([1,n])
i = 0
N = len(Reduced_corrmat.axes[1])
for k in limit_list:
for j in Reduced_corrmat.axes[1]:
if Reduced_corrmat.iloc[k][j] < Reduced_cmean[k]:
G.add_edges_from([(k,j)])
nx.draw(G, with_labels = True)
运行此命令时,出现错误,无法使用的这些索引器[环境]进行位置索引。关于错误是什么以及如何解决它的任何想法将不胜感激。