форкнуто от main/python-labs
Родитель
3ba84c6b16
Сommit
936097eb41
@ -0,0 +1,8 @@
|
||||
def read_number_list(filename):
|
||||
number_list = []
|
||||
with open(filename, 'r') as file:
|
||||
for line in file:
|
||||
numbers = line.strip().split()
|
||||
for num in numbers:
|
||||
number_list.append(float(num))
|
||||
return number_list
|
||||
@ -0,0 +1,22 @@
|
||||
import math
|
||||
|
||||
def calculate_correlation(list1, list2):
|
||||
if not list1 or not list2:
|
||||
print("Ошибка: Один или оба списка пусты.")
|
||||
return None
|
||||
|
||||
n = min(len(list1), len(list2)) # Используем меньшую длину
|
||||
sum_x = sum(list1[:n])
|
||||
sum_y = sum(list2[:n])
|
||||
sum_x_squared = sum(x**2 for x in list1[:n])
|
||||
sum_y_squared = sum(y**2 for y in list2[:n])
|
||||
sum_xy = sum(list1[i] * list2[i] for i in range(n))
|
||||
|
||||
numerator = n * sum_xy - sum_x * sum_y
|
||||
denominator = math.sqrt((n * sum_x_squared - sum_x**2) * (n * sum_y_squared - sum_y**2))
|
||||
|
||||
if denominator == 0:
|
||||
print("Предупреждение: Деление на ноль при вычислении корреляции.")
|
||||
return None
|
||||
|
||||
return numerator / denominator
|
||||
@ -0,0 +1,14 @@
|
||||
import Mod11
|
||||
import Mod22
|
||||
|
||||
if __name__ == "__main__":
|
||||
file1_name = input("Введите имя первого файла: ")
|
||||
file2_name = input("Введите имя второго файла: ")
|
||||
|
||||
list1 = Mod11.read_number_list(file1_name)
|
||||
list2 = Mod11.read_number_list(file2_name)
|
||||
|
||||
if list1 is not None and list2 is not None:
|
||||
correlation = Mod22.calculate_correlation(list1, list2)
|
||||
if correlation is not None:
|
||||
print(f"Коэффициент корреляции: {correlation:.3f}")
|
||||
Загрузка…
Ссылка в новой задаче