Python | Pandas dataframe.corr ()

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas dataframe.corr() is used to find the pairwise correlation of all columns in the dataframe. Any na values are automatically excluded. For any non-numeric data type columns in the dataframe it is ignored.

Syntax: DataFrame.corr(self, method=’pearson’, min_periods=1)

Parameters:
method :
pearson : standard correlation coefficient
kendall : Kendall Tau correlation coefficient
spearman : Spearman rank correlation
min_periods : Minimum number of observations required per pair of columns to have a valid result. Currently only available for pearson and spearman correlation

Returns: count :y : DataFrame

Примечание: корреляция переменной с самой собой равна 1.

Для ссылки на CSV-файл, используемый в коде, щелкните здесь

Example #1: Use corr() function to find the correlation among the columns in the dataframe using ‘Pearson’ method.

# importing pandas as pd
import pandas as pd
  
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
  
# Printing the first 10 rows of the data frame for visualization
df[:10]

Now use corr() function to find the correlation among the columns. We are only having four numeric columns in the dataframe.

# To find the correlation among
# the columns using pearson method
df.corr(method ="pearson")

Выход :

The output dataframe can be interpreted as for any cell, row variable correlation with the column variable is the value of the cell. As mentioned earlier, that the correlation of a variable with itself is 1. For that reason all the diagonal values are 1.00
 
Example #2: Use corr() function to find the correlation among the columns in the dataframe using ‘kendall’ method.

# importing pandas as pd
import pandas as pd
  
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
  
# To find the correlation among
# the columns using kendall method
df.corr(method ="kendall")

Выход :

Выходной кадр данных можно интерпретировать как любую ячейку, корреляция переменной строки с переменной столбца является значением ячейки. Как упоминалось ранее, корреляция переменной с самой собой равна 1. По этой причине все диагональные значения равны 1,00.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.