форкнуто от main/python-labs
Родитель
a952e19f72
Сommit
67b160433f
@ -0,0 +1,7 @@
|
|||||||
|
def readList(filename):
|
||||||
|
numList = []
|
||||||
|
with open(filename, "r") as file:
|
||||||
|
for line in file:
|
||||||
|
for num in line.strip().split():
|
||||||
|
numList.append(float(num))
|
||||||
|
return numList
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def calculateCorrelation(list1, list2):
|
||||||
|
if not list1 or not list2:
|
||||||
|
print("Ошибка: список не может быть пустым")
|
||||||
|
return None
|
||||||
|
|
||||||
|
n = min(len(list1), len(list2))
|
||||||
|
mean1 = sum(list1[:n]) / n
|
||||||
|
mean2 = sum(list2[:n]) / n
|
||||||
|
|
||||||
|
numerator = 0
|
||||||
|
sum1 = 0
|
||||||
|
sum2 = 0
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
d1 = list1[i] - mean1
|
||||||
|
d2 = list2[i] - mean2
|
||||||
|
numerator += d1 * d2
|
||||||
|
sum1 += d1 ** 2
|
||||||
|
sum2 += d2 ** 2
|
||||||
|
|
||||||
|
denominator = math.sqrt(sum1 * sum2)
|
||||||
|
if denominator == 0:
|
||||||
|
print("Ошибка: деление на ноль")
|
||||||
|
return None
|
||||||
|
|
||||||
|
return numerator / denominator
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
import os
|
||||||
|
import Module1
|
||||||
|
import Module2
|
||||||
|
|
||||||
|
for i in range(1, 3):
|
||||||
|
while True:
|
||||||
|
filename = os.path.abspath(input(f"Введите имя {i}-го файла: "))
|
||||||
|
|
||||||
|
if not os.path.isfile(filename):
|
||||||
|
print("Ошибка: введено неверное имя файла")
|
||||||
|
continue
|
||||||
|
|
||||||
|
newList = Module1.readList(filename)
|
||||||
|
|
||||||
|
if not newList:
|
||||||
|
print("Ошибка: в данном файле содержится пустой список значений")
|
||||||
|
continue
|
||||||
|
|
||||||
|
globals()[f"list{i}"] = newList
|
||||||
|
break
|
||||||
|
|
||||||
|
correlation = Module2.calculateCorrelation(list1, list2)
|
||||||
|
if correlation is not None:
|
||||||
|
print(f"Коэффициент корреляции равен: {correlation:.3f}")
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
1 2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
7 5
|
||||||
@ -0,0 +1 @@
|
|||||||
|
6 7 8 9 10
|
||||||
Загрузка…
Ссылка в новой задаче