这是我目前的代码
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,我可以通过简单的打印来检索每个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减少?
如果您希望数据与代码一起播放,请告诉我
答案 0 :(得分:0)
numpy.triu获取上三角形,1是取出对角线
import numpy as np
np.triu([[1,2,3],[4,5,6],[7,8,9]], 1)
https://docs.scipy.org/doc/numpy/reference/generated/numpy.triu.html