在python中,如何创建没有对角线的upperdiagonal表?

2024-09-28 19:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我现在的密码

import numpy as np
with open("wine.txt","r") as f:
    stuff=f.readlines()
z=[]
for hello in stuff:
    firstbook=hello.strip().split(",")
    x=[float(xy) for xy in firstbook]
    z.append(x)
u=np.array(z)
gridList = []
for item in range(14):
    row=[]
    for nlist in u:
        row.append(nlist[item])
    gridList.append(row)
column=13
r=[]
while column >= 0:
    recess=column-1
    while recess >= 0:
        r.append(np.corrcoef(gridList[recess],gridList[column]))
        recess=recess - 1
    column=column-1
print len(r),r[90][1][0],r[0][1][0]
print "column -->      1    2    3     4    5    6    7     8    9   10    11   12    13   14"
print 87*"-"
chucky=0
while chucky<1:
    print "column=", chucky, "|",
    princess = 90
    while princess >=77:
        print round((r[princess][1][0]),2),
        princess-=1
    chucky+=1

我正在讨论是否要用一种很难的方法来完成这个任务,写一堆while循环,但是我知道有一种更简单的方法来编写这个代码。我需要创建一个没有对角线的上对角线表,总共需要输入91个值。如您所见,我计算了数据列之间的pearson r值,并将其赋给r,在r中,我可以通过简单地打印来检索每个2x2矩阵的相关值 r[i][1][0] for i in range(90)对于每个矩阵。我知道为不带对角线的上对角线表编写代码应该不是问题,但是我遇到的问题是这样的,表的第一行应该包含以下值r[90][1][0]r[89][1][0]r[87][1][0]r[84][1][0]r[80][1][0]等等。那么如何使用循环来写入这些值呢90,谁从90以正整数递增的速度递减?你知道吗

如果你想让数据和代码一起玩,请告诉我


Tags: 代码inforasnpcolumnrowprint