代碼
import math as mt
def pearson(rating1, rating2):
sum_xy = 0
sum_x = 0
sum_y = 0
sum_x2 = 0
sum_y2 = 0
n = 0
l=len(rating1)
for key in range(1,l):
x = rating1[key]
y = rating2[key]
sum_xy += x * y
sum_x += x
sum_y += y
sum_x2 += mt.pow(x, 2)
sum_y2 += mt.pow(y, 2)
n += 1
# now compute denominator
denominator = mt.sqrt(sum_x2 - mt.pow(sum_x, 2) / n) * mt.sqrt(sum_y2 - mt.pow(sum_y, 2) / n)
if denominator == 0:
return 0
else:
return (sum_xy - (sum_x * sum_y) / n) / denominator
def transformMatrix(m):
rt = [[] for i in m[0]] # m[0] 有幾個元素琐簇,說明原矩陣有多少列蒿秦。此處創(chuàng)建轉置矩陣的行
for ele in m:
for i in range(len(ele)):
# rt[i] 代表新矩陣的第 i 行
# ele[i] 代表原矩陣當前行的第 i 列
rt[i].append(ele[i])
return rt
main():
gene=['5.18C','5.20C','5.22C','5.24C','5.26C','5.20CR','5.22CR','5.24CR','5.26CR','5.20IF','5.22IF','5.24IF','5.26IF','5.28IF','5,30IF']
with open('pcc1.txt','r')as file:
data=[]
for lines in file:
lines=lines.split()
lines=list(map(float,lines))#map函數(shù)將整個列表的字符串改為數(shù)值
data.append(lines)
data=transformMatrix(data)
for i in range(len(data)):
for n in range(i+1,len(data)):
result=pearson(data[i],data[n])
print(gene[i]+' '+gene[n]+' '+str(result))
main()
結果
5.18C 5.20C 0.9871012835402054
5.18C 5.22C 0.9539391509078203
5.18C 5.24C 0.9440122769056143
5.18C 5.26C 0.9207233646483903
5.18C 5.20CR 0.986711759773038
5.18C 5.22CR 0.958232055117914
5.18C 5.24CR 0.9467990025945423
5.18C 5.26CR 0.930003579027746
5.18C 5.20IF 0.9803204119636252
5.18C 5.22IF 0.9679535116878819
5.18C 5.24IF 0.9554002211751581
5.18C 5.26IF 0.9406467568988299
5.18C 5.28IF 0.927492736000826
5.18C 5,30IF 0.9231483557836282
5.20C 5.22C 0.9733785319114773
5.20C 5.24C 0.9637653880340846
5.20C 5.26C 0.9363821030901199
5.20C 5.20CR 0.993947234421875
5.20C 5.22CR 0.976266301928793
5.20C 5.24CR 0.9660875620803956
5.20C 5.26CR 0.9483749949409074
5.20C 5.20IF 0.9885143857594585
5.20C 5.22IF 0.9831894091374788
5.20C 5.24IF 0.9769998231491283
5.20C 5.26IF 0.9555250622607507
5.20C 5.28IF 0.9426427743346459
5.20C 5,30IF 0.9363767045005105
5.22C 5.24C 0.9927286472800367
5.22C 5.26C 0.963659638876761
5.22C 5.20CR 0.9640978077920334
5.22C 5.22CR 0.9905739624976019
5.22C 5.24CR 0.9896445100754759
5.22C 5.26CR 0.9779389386895804
5.22C 5.20IF 0.9569141104951323
5.22C 5.22IF 0.9887565640901403
5.22C 5.24IF 0.9875052947174031
5.22C 5.26IF 0.9760811273321829
5.22C 5.28IF 0.9529885507000276
5.22C 5,30IF 0.9498049955629376
5.24C 5.26C 0.9823469109882319
5.24C 5.20CR 0.9516417575655933
5.24C 5.22CR 0.9805301253796285
5.24C 5.24CR 0.9940626511172791
5.24C 5.26CR 0.9896343766884068
5.24C 5.20IF 0.9460234732765005
5.24C 5.22IF 0.976305631206706
5.24C 5.24IF 0.9840932186444508
5.24C 5.26IF 0.9883160087453777
5.24C 5.28IF 0.9707509122979096
5.24C 5,30IF 0.9696718884207947
5.26C 5.20CR 0.9217129268064191
5.26C 5.22CR 0.9444114714681782
5.26C 5.24CR 0.9768245417083046
5.26C 5.26CR 0.989178177300411
5.26C 5.20IF 0.9183802492037744
5.26C 5.22IF 0.9379542836151004
5.26C 5.24IF 0.9562404088552118
5.26C 5.26IF 0.994498211383029
5.26C 5.28IF 0.9891437347952052
5.26C 5,30IF 0.9902973553214138
5.20CR 5.22CR 0.9739738170945773
5.20CR 5.24CR 0.958694839983264
5.20CR 5.26CR 0.93898816197368
5.20CR 5.20IF 0.9942783171880107
5.20CR 5.22IF 0.9784444335228751
5.20CR 5.24IF 0.973993379519237
5.20CR 5.26IF 0.9429546182379347
5.20CR 5.28IF 0.9324721716400979
5.20CR 5,30IF 0.9236007837816115
5.22CR 5.24CR 0.9852690489829382
5.22CR 5.26CR 0.9668856388486581
5.22CR 5.20IF 0.9687560104037619
5.22CR 5.22IF 0.9920918327648084
5.22CR 5.24IF 0.9914194822162129
5.22CR 5.26IF 0.9609387193038533
5.22CR 5.28IF 0.9400761511428076
5.22CR 5,30IF 0.933403475693742
5.24CR 5.26CR 0.9890825272477216
5.24CR 5.20IF 0.9550609459930318
5.24CR 5.22IF 0.9774727435408652
5.24CR 5.24IF 0.9897825257393855
5.24CR 5.26IF 0.9850261491581499
5.24CR 5.28IF 0.9706027961419257
5.24CR 5,30IF 0.9670275495541026
5.26CR 5.20IF 0.9373124377983526
5.26CR 5.22IF 0.9590275941362891
5.26CR 5.24IF 0.9766579003207781
5.26CR 5.26IF 0.9901626744066809
5.26CR 5.28IF 0.983449844845842
5.26CR 5,30IF 0.9812949733729065
5.20IF 5.22IF 0.972246395713007
5.20IF 5.24IF 0.9730044202038579
5.20IF 5.26IF 0.9380807151112099
5.20IF 5.28IF 0.9338276135937662
5.20IF 5,30IF 0.9230220326580256
5.22IF 5.24IF 0.9859027483021913
5.22IF 5.26IF 0.9567823424516605
5.22IF 5.28IF 0.935308654297277
5.22IF 5,30IF 0.9306435208823552
5.24IF 5.26IF 0.9701011523511289
5.24IF 5.28IF 0.9571942210250748
5.24IF 5,30IF 0.9493918051499619
5.26IF 5.28IF 0.9866700821126035
5.26IF 5,30IF 0.9861027338926087
5.28IF 5,30IF 0.995733574369238