Чтобы вывести корреляцию между функциями в Python, вы можете использовать различные методы. Вот несколько часто используемых:
-
Использование библиотеки NumPy и функции
corrcoef
:import numpy as np # Create an example dataset feature1 = [1, 2, 3, 4, 5] feature2 = [5, 4, 3, 2, 1] # Calculate the correlation coefficient correlation_matrix = np.corrcoef(feature1, feature2) correlation = correlation_matrix[0, 1] # Print the correlation coefficient print("Correlation coefficient:", correlation)
-
Использование библиотеки Pandas и метода
corr
:import pandas as pd # Create a DataFrame with example data data = {'Feature1': [1, 2, 3, 4, 5], 'Feature2': [5, 4, 3, 2, 1]} df = pd.DataFrame(data) # Calculate the correlation matrix correlation_matrix = df.corr() # Print the correlation between two features print("Correlation coefficient:", correlation_matrix['Feature1']['Feature2'])
-
Использование библиотеки SciPy и функции
pearsonr
:from scipy.stats import pearsonr # Create an example dataset feature1 = [1, 2, 3, 4, 5] feature2 = [5, 4, 3, 2, 1] # Calculate the correlation coefficient correlation, _ = pearsonr(feature1, feature2) # Print the correlation coefficient print("Correlation coefficient:", correlation)
Обратите внимание, что приведенные примеры предполагают, что у вас установлены необходимые библиотеки.