0
这里是我当前的代码在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个值。正如您所看到的,我计算了数据列之间的皮尔逊r值并将其分配给r,我可以在其中通过简单地为每个矩阵打印 r[i][1][0] for i in range(90)
来检索每个2×2矩阵的相关值。我知道为对角线表写一个上对角表的代码应该不是问题,但是我遇到的问题与此类似,表的第一行应包含以下值:r [90] [1] [0] r [89] [1] [0] r [87] [1] [0] r [84] [1] [0] etc 。那么我怎么用循环来写这些数值,从90开始,从增加正整数的速度减少到90?
让我知道如果你想将数据与代码玩
指数阵列用x [i,j,k]代替[i] [j] ...这样更容易阅读。还要确定阵列的形状和dtype。 – hpaulj
看看np.tri ...函数族;和np.diag ... – hpaulj
谢谢,我会试试这些方法 – user5896534