-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkorelasyon hesaplama.py
44 lines (36 loc) · 1.36 KB
/
korelasyon hesaplama.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
import math
x = [1, 2, 5, 7, 8, 12, 15, 17, 18]
y = [4, 8, 9, 13, 16, 19, 21, 25, 27]
# r() = korelasyon, E() = toplam, n = grupların eleman sayısı, sqrt() = karekök
# r(x, y) = (E(x*y)-(E(x)*E(y)/n)) / sqrt((E(x^2)-E(x)^2/n)*(E(y^2)-E(y)^2/n))
# Listedeki elemanları toplama fonksiyonu
def E(liste):
toplam = 0
for i in range(len(liste)):
toplam += liste[i]
return toplam
# Listenin karesini alma fonksiyonu
def kare(liste):
a = liste.copy()
for i in range(len(liste)):
a[i] = liste[i] * liste[i]
return a
# İki listeyi çarpma fonksiyonu
def carpim(listebir, listeiki):
finalliste = []
for i in range(len(listebir)):
finalliste.append(listebir[i] * listeiki[i])
return finalliste
# Korelasyon fonksiyonu
#def r(listebir, listeiki):
# return (E(carpim(listebir, listeiki)) - (E(listebir)*E(listeiki)/len(listeiki)) / math.sqrt((E(kare(listebir))-E(listebir)*E(listebir)/len(listebir))*(E(kare(listeiki))-E(listeiki)*E(listeiki)/len(listeiki))))
def r(listebir, listeiki):
n = len(listebir)
nom = (n*E(carpim(listebir, listeiki)) - E(listebir)*E(listeiki))
denom = math.sqrt((n*E(kare(listebir)) - E(listebir)*E(listebir)) * (n*E(kare(listeiki)) - E(listeiki) * E(listeiki)))
return nom/denom
# MegaCrafter thank you for your contribution
# Çalıştır
print(r(x, y))
print(x)