import pandas as pd import numpy as np from itertools import combinations from…
- User
import pandas as pd import numpy as np from itertools import combinations from google.colab import files
Загружаем датасет
#uploaded = files.upload()
Предполагаем, что загружаемый вами файл имеет имя diseasesDataset-_-kopia.xlsx
df = pd.read_excel('diseasesDataset__kopia.xlsx', engine='openpyxl')
Проверьте, как выглядят ваши данные
print("Первые 5 строк вашего датасета:")
print(df.head())
1. Функциональная предобработка данных (попарные произведения)
def add_interaction_terms(df): factors = df.columns[1:] # Пропускаем целевую колонку for (f1, f2) in combinations(factors, 2): df[f"{f1}_{f2}"] = df[f1] * df[f2] return df
df = add_interaction_terms(df)
Корреляция между факторами
correlation_matrix = df.corr()
2. Определение групп по корреляции
def define_correlation_groups(corr_matrix, target_factor): strong_group = [] medium_group = [] weak_group = []
for factor in corr_matrix.columns: if factor != target_factor: corr_value = corr_matrix[target_factor][factor] if 0.7 < corr_value <= 1: strong_group.append(factor) elif 0.3 < corr_value <= 0.7: medium_group.append(factor) elif 0 <= corr_value <= 0.3: weak_group.append(factor) return strong_group, medium_group, weak_groupИспользуем целевую переменную
target_factor = '№2 ВРОЖДЕННЫЕ АНОМАЛИИ' strong, medium, weak = define_correlation_groups(correlation_matrix, target_factor)
print(f"Сильная корреляция: {strong}") print(f"Средняя корреляция: {medium}") print(f"Слабая корреляция: {weak}")
3. Вычисление коэффициентов корреляции для сильной группы
def calculate_coefficients(strong_group, target_series): coefficients = {} for factor in strong_group: r = correlation_matrix[target_series.name][factor] sigma_y = target_series.std() sigma_x = df[factor].std() b = r * (sigma_y / sigma_x) a = target_series.mean() - b * df[factor].mean() coefficients[factor] = (a, b)
return coefficientsПример использования
coefficients_strong = calculate_coefficients(strong, df[target_factor])
print("Коэффициенты для сильной группы:")
for factor, (a, b) in coefficients_strong.items():
print(f"{factor}: a = {a}, b = {b}")
4. Определение эмпирических и теоретических областей
def calculate_empirical_theoretical_bounds(factor_series): a = factor_series.min() b = factor_series.max() min_empirical = a - (b - a) * 0.05 max_empirical = b + (b - a) * 0.05 mean = factor_series.mean() std = factor_series.std() min_theoretical = mean - 3 * std max_theoretical = mean + 3 * std return (min_empirical, max_empirical), (min_theoretical, max_theoretical)
- Assistant
- User
Приведи полный код изменений
- Assistant