以下是我当前的代码
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)来检索每个2x2矩阵的相关值。我知道写一个没有对角线的上对角线表的代码应该不是问题,但我遇到的问题是,表的第一行应该包含以下值,r90 r89 r87 r84 r80等。那么我如何使用循环来写入这些值,从90开始,以正整数的速度从90开始递减?
如果你想让数据使用代码,请让我知道
发布于 2016-10-29 13:04:50
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
https://stackoverflow.com/questions/40316072
复制相似问题