форкнуто от main/python-labs
main
Родитель
9b6ab0fe10
Сommit
3155b61c4c
@ -1,25 +1,15 @@
|
|||||||
|
import statistics
|
||||||
|
|
||||||
def mod2(list1, list2):
|
def mod2(list1, list2):
|
||||||
n = min(len(list1), len(list2))
|
n = min(len(list1), len(list2))
|
||||||
if n == 0:
|
if n == 0:
|
||||||
return 0.0
|
return 0.0
|
||||||
|
x = list1[:n]
|
||||||
|
y = list2[:n]
|
||||||
|
|
||||||
avg1 = sum(list1[:n]) / n
|
try:
|
||||||
avg2 = sum(list2[:n]) / n
|
correlation = statistics.correlation(x, y)
|
||||||
|
except statistics.StatisticsError:
|
||||||
numerator = 0
|
correlation = 0.0
|
||||||
denom1 = 0
|
|
||||||
denom2 = 0
|
|
||||||
|
|
||||||
for i in range(n):
|
|
||||||
diff1 = list1[i] - avg1
|
|
||||||
diff2 = list2[i] - avg2
|
|
||||||
numerator += diff1 * diff2
|
|
||||||
denom1 += diff1 * diff1
|
|
||||||
denom2 += diff2 * diff2
|
|
||||||
|
|
||||||
if denom1 == 0 or denom2 == 0:
|
|
||||||
return 0.0
|
|
||||||
|
|
||||||
correlation = numerator / ((denom1 * denom2) ** 0.5)
|
|
||||||
|
|
||||||
return correlation
|
return correlation
|
||||||
Загрузка…
Ссылка в новой задаче