Hướng dẫn how do you display a correlation matrix in python? - làm thế nào để bạn hiển thị một ma trận tương quan trong python?

Ngạc nhiên khi thấy không ai đề cập đến các lựa chọn thay thế có khả năng, tương tác và dễ sử dụng hơn.

A) Bạn có thể sử dụng Plotly:

  1. Chỉ cần hai dòng và bạn nhận được:

  2. interactivity,

  3. quy mô trơn tru,

  4. Màu sắc dựa trên toàn bộ dữ liệu thay vì các cột riêng lẻ,

  5. Tên cột & chỉ số hàng trên trục,

  6. Phóng to,

  7. panning,

  8. Khả năng một cú nhấp chuột tích hợp để lưu nó dưới dạng định dạng PNG,

  9. auto-scaling,

  10. so sánh về di chuột,

  11. Bong bóng hiển thị các giá trị để HeatMap vẫn có vẻ tốt và bạn có thể thấy các giá trị bất cứ nơi nào bạn muốn:

import plotly.express as px
fig = px.imshow(df.corr())
fig.show()

Hướng dẫn how do you display a correlation matrix in python? - làm thế nào để bạn hiển thị một ma trận tương quan trong python?

B) Bạn cũng có thể sử dụng Bokeh:

Tất cả các chức năng giống nhau với một chút rắc rối. Nhưng vẫn đáng giá nếu bạn không muốn chọn tham gia vào âm mưu và vẫn muốn tất cả những điều này:

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)

Hướng dẫn how do you display a correlation matrix in python? - làm thế nào để bạn hiển thị một ma trận tương quan trong python?

Trong hướng dẫn này, bạn sẽ học cách tính toán một ma trận tương quan trong Python và cách vẽ nó dưới dạng bản đồ nhiệt. Bạn sẽ tìm hiểu một ma trận tương quan là gì và cách giải thích nó, cũng như đánh giá ngắn về hệ số tương quan là gì.you’ll learn how to calculate a correlation matrix in Python and how to plot it as a heat map. You’ll learn what a correlation matrix is and how to interpret it, as well as a short review of what the coefficient of correlation is.

Sau đó, bạn sẽ học cách tính toán một ma trận tương quan với thư viện Pandas. Sau đó, bạn sẽ học cách vẽ ma trận tương quan bản đồ nhiệt bằng cách sử dụng Seaborn. Cuối cùng, bạn sẽ học cách tùy chỉnh các bản đồ nhiệt này để bao gồm các giá trị nhất định.

Câu trả lời nhanh: Sử dụng gấu trúc

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
4 để tính toán ma trận tương quan trong Python

# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000

  • Ma trận tương quan là gì và làm thế nào để giải thích nó
  • Tính toán ma trận tương quan trong python với gấu trúc
  • Làm thế nào để vẽ một ma trận tương quan bản đồ nhiệt với Seaborn
  • Chỉ vẽ nửa dưới của một ma trận tương quan với Seaborn
  • Cách lưu ma trận tương quan vào một tệp trong Python
  • Chỉ chọn mối tương quan mạnh trong ma trận tương quan
  • Chỉ chọn tương quan tích cực / tiêu cực trong ma trận tương quan
  • Sự kết luận
  • Tài nguyên bổ sung

Ma trận tương quan là gì và làm thế nào để giải thích nó

Tính toán ma trận tương quan trong python với gấu trúc. It allows us to visualize how much (or how little) correlation exists between different variables.

Làm thế nào để vẽ một ma trận tương quan bản đồ nhiệt với Seaborn

Chỉ vẽ nửa dưới của một ma trận tương quan với Seaborndimensionality reduction and can be used to improve the runtime and effectiveness of our models.

Cách lưu ma trận tương quan vào một tệp trong Pythonlook like? A correlation matrix has the same number of rows and columns as our dataset has columns.

Chỉ chọn mối tương quan mạnh trong ma trận tương quan

Chỉ chọn tương quan tích cực / tiêu cực trong ma trận tương quan A coefficient of correlation is a value between

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
5 and
from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
6 that denotes both the strength and directionality of a relationship between two variables.

  • Sự kết luận
  • Ma trận tương quan là một công cụ phổ biến được sử dụng để so sánh các hệ số tương quan giữa các tính năng (hoặc thuộc tính) khác nhau) trong bộ dữ liệu. Nó cho phép chúng ta hình dung mức độ tương quan (hoặc ít) tồn tại giữa các biến khác nhau.

Đây là một bước quan trọng trong các đường ống học máy tiền xử lý trước. Vì ma trận tương quan cho phép chúng tôi xác định các biến có mức độ tương quan cao, chúng cho phép chúng tôi giảm số lượng các tính năng chúng tôi có thể có trong bộ dữ liệu.

Điều này thường được gọi là giảm kích thước và có thể được sử dụng để cải thiện thời gian chạy và hiệu quả của các mô hình của chúng tôi.

Hướng dẫn how do you display a correlation matrix in python? - làm thế nào để bạn hiển thị một ma trận tương quan trong python?
Đó là lý thuyết về ma trận tương quan của chúng tôi. Nhưng nó thực sự trông như thế nào? Một ma trận tương quan có cùng số lượng hàng và cột như tập dữ liệu của chúng tôi có các cột.

Điều này có nghĩa là nếu chúng ta có một bộ dữ liệu với 10 cột, thì ma trận của chúng ta sẽ có mười hàng và mười cột. Mỗi hàng và cột biểu thị một biến (hoặc cột) trong tập dữ liệu của chúng tôi và giá trị trong ma trận là hệ số tương quan giữa hàng và cột tương ứng.correlation coefficients between the pairs of features. We can see that we have a diagonal line of the values of 1. This is because these values represent the correlation between a column and itself. Because these values are, of course, always the same they will always be 1.

Nếu bạn có một con mắt sắc sảo, bạn sẽ nhận thấy rằng các giá trị ở trên cùng bên phải là hình ảnh được nhân đôi ở phía dưới bên trái của ma trận. Điều này là do mối quan hệ giữa hai biến trong các cặp cột hàng sẽ luôn giống nhau. Nó thực hành phổ biến để loại bỏ chúng khỏi ma trận bản đồ nhiệt để trực quan hóa dữ liệu tốt hơn. Đây là một cái gì đó bạn sẽ học được trong các phần sau của hướng dẫn.

Gấu trúc giúp tạo ra một ma trận tương quan cực kỳ dễ dàng bằng phương pháp DataFrame,

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
7. Phương pháp lấy một số tham số. Hãy cùng khám phá chúng trước khi lặn vào một ví dụ:

matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)

Theo mặc định, phương pháp

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
8 sẽ sử dụng hệ số tương quan Pearson, mặc dù bạn cũng có thể chọn các phương thức Kendall hoặc Spearman. Tương tự, bạn có thể giới hạn số lượng quan sát cần thiết để tạo ra kết quả.

Đang tải một mẫu dữ liệu gấu trúc mẫu

Bây giờ bạn có một sự hiểu biết về cách thức hoạt động của phương thức, hãy để tải một mẫu dữ liệu gấu trúc mẫu. Đối với điều này, chúng tôi sẽ sử dụng hàm Seaborn

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
9, cho phép chúng tôi tạo một số bộ dữ liệu dựa trên dữ liệu trong thế giới thực. Chúng tôi sẽ tải bộ dữ liệu
# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000
0. Seaborn cho phép chúng tôi tạo ra các trực quan python rất hữu ích, cung cấp một trình bao bọc cấp cao dễ sử dụng trên matplotlib.

# Loading a sample Pandas dataframe
import pandas as pd
import seaborn as sns

df = sns.load_dataset('penguins')

# We're renaming columns to make them print nicer
df.columns = ['species', 'island', 'b_len', 'b_dep', 'f_len', 'f_dep', 'sex']
print(df.head())

# Returns:
#   species     island  b_len  b_dep  f_len   f_dep     sex
# 0  Adelie  Torgersen   39.1   18.7  181.0  3750.0    Male
# 1  Adelie  Torgersen   39.5   17.4  186.0  3800.0  Female
# 2  Adelie  Torgersen   40.3   18.0  195.0  3250.0  Female
# 3  Adelie  Torgersen    NaN    NaN    NaN     NaN     NaN
# 4  Adelie  Torgersen   36.7   19.3  193.0  3450.0  Female

Hãy cùng phá vỡ những gì chúng tôi đã làm ở đây:

  • Chúng tôi đã tải thư viện gấu trúc bằng bí danh PD. Chúng tôi cũng tải thư viện Seaborn bằng bí danh SNS.
  • Sau đó, chúng tôi đã tạo một dataFrame,
    # Calculating a Correlation Matrix with Pandas
    import pandas as pd
    
    matrix = df.corr()
    print(matrix)
    
    # Returns:
    #           b_len     b_dep     f_len     f_dep
    # b_len  1.000000 -0.235053  0.656181  0.595110
    # b_dep -0.235053  1.000000 -0.583851 -0.471916
    # f_len  0.656181 -0.583851  1.000000  0.871202
    # f_dep  0.595110 -0.471916  0.871202  1.000000
    1, sử dụng hàm load_dataset và chuyển trong
    # Calculating a Correlation Matrix with Pandas
    import pandas as pd
    
    matrix = df.corr()
    print(matrix)
    
    # Returns:
    #           b_len     b_dep     f_len     f_dep
    # b_len  1.000000 -0.235053  0.656181  0.595110
    # b_dep -0.235053  1.000000 -0.583851 -0.471916
    # f_len  0.656181 -0.583851  1.000000  0.871202
    # f_dep  0.595110 -0.471916  0.871202  1.000000
    2 làm đối số.
  • Cuối cùng, chúng tôi đã in năm hàng đầu tiên của DataFrame bằng phương thức
    # Calculating a Correlation Matrix with Pandas
    import pandas as pd
    
    matrix = df.corr()
    print(matrix)
    
    # Returns:
    #           b_len     b_dep     f_len     f_dep
    # b_len  1.000000 -0.235053  0.656181  0.595110
    # b_dep -0.235053  1.000000 -0.583851 -0.471916
    # f_len  0.656181 -0.583851  1.000000  0.871202
    # f_dep  0.595110 -0.471916  0.871202  1.000000
    3

Chúng ta có thể thấy rằng DataFrame của chúng ta có 7 cột. Một số cột này là số và một số khác là chuỗi.

Tính toán một ma trận tương quan với gấu trúc

Bây giờ chúng tôi đã tải DataFrame của Pandas, hãy để sử dụng phương thức

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
8 để tính toán ma trận tương quan của chúng tôi. Chúng tôi chỉ cần áp dụng phương thức trực tiếp cho toàn bộ DataFrame:

# Calculating a Correlation Matrix with Pandas
matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000

Chúng ta có thể thấy rằng trong khi DataFrame gốc của chúng ta có bảy cột, Pandas chỉ tính toán ma trận bằng các cột số. Chúng ta có thể thấy rằng bốn cột của chúng ta đã được biến thành các cặp hàng cột, biểu thị mối quan hệ giữa hai cột.

Ví dụ, chúng ta có thể thấy rằng hệ số tương quan giữa các biến

# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000
5 và
# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000
6 là 0,87. Điều này chỉ ra rằng có một mối quan hệ tương đối mạnh mẽ, tích cực giữa hai biến.

Làm tròn các giá trị ma trận tương quan của chúng tôi với gấu trúc

Chúng ta có thể làm tròn các giá trị trong ma trận của chúng ta đến hai chữ số để làm cho chúng dễ đọc hơn. Ma trận mà trả về thực sự là một khung dữ liệu gấu trúc. Điều này có nghĩa là chúng ta thực sự có thể áp dụng các phương thức DataFrame khác nhau cho chính ma trận. Chúng ta có thể sử dụng phương pháp Pandas

# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000
7 để làm tròn các giá trị của chúng ta.

matrix = df.corr().round(2)
print(matrix)

# Returns:
#        b_len  b_dep  f_len  f_dep
# b_len   1.00  -0.24   0.66   0.60
# b_dep  -0.24   1.00  -0.58  -0.47
# f_len   0.66  -0.58   1.00   0.87
# f_dep   0.60  -0.47   0.87   1.00

Mặc dù chúng ta mất một chút độ chính xác khi làm điều này, nhưng nó làm cho các mối quan hệ dễ đọc hơn.

Trong phần tiếp theo, bạn sẽ học cách sử dụng thư viện Seaborn để vẽ bản đồ nhiệt dựa trên ma trận.

Làm thế nào để vẽ một ma trận tương quan bản đồ nhiệt với Seaborn

Trong nhiều trường hợp, bạn sẽ muốn hình dung một ma trận tương quan. Điều này dễ dàng được thực hiện ở định dạng bản đồ nhiệt nơi chúng ta có thể hiển thị các giá trị mà chúng ta có thể hiểu rõ hơn về mặt trực quan. Thư viện Seaborn làm cho việc tạo ra một bản đồ nhiệt rất dễ dàng, sử dụng chức năng

# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000
8.

Bây giờ, hãy cùng nhập pyplot từ matplotlib để trực quan hóa dữ liệu của chúng tôi. Mặc dù chúng tôi thực sự đang sử dụng Seaborn để trực quan hóa dữ liệu, Seaborn phụ thuộc rất nhiều vào matplotlib vì hình ảnh của nó.

# Visualizing a Pandas Correlation Matrix Using Seaborn
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('penguins')
matrix = df.corr().round(2)
sns.heatmap(matrix, annot=True)
plt.show()

Ở đây, chúng tôi đã nhập thư viện

# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000
9 dưới dạng
matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)
0, cho phép chúng tôi hiển thị dữ liệu của mình. Sau đó, chúng tôi đã sử dụng hàm
matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)
1, chuyển trong ma trận của chúng tôi và yêu cầu thư viện chú thích bản đồ nhiệt của chúng tôi bằng các giá trị bằng tham số
matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)
2. Điều này đã trả về biểu đồ sau:

Hướng dẫn how do you display a correlation matrix in python? - làm thế nào để bạn hiển thị một ma trận tương quan trong python?
Trực quan hóa một ma trận tương quan với các tham số hầu hết là mặc định

Chúng ta có thể thấy rằng một số điều kỳ lạ đã xảy ra ở đây. Thứ nhất, chúng ta biết rằng một hệ số tương quan có thể lấy các giá trị từ -1 đến +1. Biểu đồ của chúng tôi hiện chỉ hiển thị các giá trị từ khoảng -0,5 đến +1. Bởi vì điều này, trừ khi chúng tôi cẩn thận, chúng tôi có thể suy ra rằng các mối quan hệ tiêu cực mạnh mẽ hơn thực tế.we know that a correlation coefficient can take the values from -1 through +1. Our graph currently only shows values from roughly -0.5 through +1. Because of this, unless we’re careful, we may infer that negative relationships are strong than they actually are.

Hơn nữa, dữ liệu không được hiển thị theo cách khác nhau. Chúng tôi muốn màu sắc của chúng tôi mạnh mẽ khi các mối quan hệ trở nên mạnh mẽ. Thay vào đó, các màu yếu khi các giá trị đi gần +1.

Chúng tôi có thể sửa đổi một vài tham số bổ sung ở đây:

  1. matrix = df.corr(
        method = 'pearson',  # The method of correlation
        min_periods = 1      # Min number of observations required
    )
    3,
    matrix = df.corr(
        method = 'pearson',  # The method of correlation
        min_periods = 1      # Min number of observations required
    )
    4 được sử dụng để neo Colormap. Nếu không có giá trị nào, các giá trị được suy ra, dẫn đến các giá trị âm sẽ không vượt quá 0,5. Vì chúng ta biết rằng các hệ số hoặc mối tương quan nên được neo ở mức +1 và -1, chúng ta có thể vượt qua chúng.
  2. matrix = df.corr(
        method = 'pearson',  # The method of correlation
        min_periods = 1      # Min number of observations required
    )
    5 Các loài giá trị để tập trung colormap khi chúng ta vẽ dữ liệu phân kỳ. Vì chúng tôi muốn màu sắc phân kỳ từ 0, chúng tôi nên chỉ định 0 là đối số ở đây.
  3. CMAP = cho phép chúng tôi vượt qua trong một bản đồ màu khác. Bởi vì chúng tôi muốn màu sắc mạnh mẽ hơn ở hai đầu của sự khác biệt, chúng tôi có thể vượt qua VLAG khi đối số để hiển thị màu sắc chuyển từ màu xanh sang màu đỏ.

Hãy để thử lại điều này, vượt qua ba đối số mới này:

# Visualizing a Pandas Correlation Matrix Using Seaborn
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('penguins')
matrix = df.corr().round(2)
sns.heatmap(matrix, annot=True, vmax=1, vmin=-1, center=0, cmap='vlag')
plt.show()

Điều này trả về ma trận sau. Nó phân kỳ từ -1 đến +1 và màu sắc thuận tiện tối ở một trong hai cực.

Hướng dẫn how do you display a correlation matrix in python? - làm thế nào để bạn hiển thị một ma trận tương quan trong python?
Bản đồ nhiệt được định dạng đúng với màu sắc khác nhau

Trong phần này, bạn đã học cách định dạng một bản đồ nhiệt được tạo ra bằng cách sử dụng Seaborn để hình dung rõ hơn các mối quan hệ giữa các cột.

Chỉ vẽ nửa dưới của một ma trận tương quan với Seaborn

Một điều mà bạn sẽ nhận thấy là làm thế nào dự phòng khi hiển thị cả nửa trên và dưới của một ma trận tương quan. Tâm trí của chúng ta chỉ có thể diễn giải rất nhiều - vì điều này, có thể hữu ích khi chỉ hiển thị nửa dưới của hình dung của chúng ta. Tương tự, nó có thể có ý nghĩa để loại bỏ đường chéo 1S, vì điều này không có giá trị thực.

Để thực hiện điều này, chúng ta có thể sử dụng hàm

matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)
6 numpy, tạo ra một hình tam giác của một ma trận. Hãy bắt đầu bằng cách nhập Numpy và thêm biến
matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)
7 vào chức năng của chúng tôi. Sau đó, chúng ta có thể chuyển mặt nạ này vào chức năng Seaborn của chúng ta, yêu cầu bản đồ nhiệt chỉ che giấu các giá trị mà chúng ta muốn thấy:

# Showing only the bottom half of our correlation matrix
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = sns.load_dataset('penguins')
matrix = df.corr().round(2)
mask = np.triu(np.ones_like(matrix, dtype=bool))
sns.heatmap(matrix, annot=True, vmax=1, vmin=-1, center=0, cmap='vlag', mask=mask)
plt.show()

Điều này trả về hình ảnh sau:

Hướng dẫn how do you display a correlation matrix in python? - làm thế nào để bạn hiển thị một ma trận tương quan trong python?
Chỉ hiển thị nửa dưới của ma trận bằng mặt nạ Numpy

Chúng ta có thể thấy nó dễ dàng hơn bao nhiêu để hiểu được sức mạnh của các mối quan hệ dữ liệu của chúng ta ở đây. Bởi vì chúng tôi đã loại bỏ một lượng đáng kể sự lộn xộn thị giác (hơn một nửa!), Chúng tôi có thể giải thích tốt hơn ý nghĩa đằng sau hình ảnh.

Cách lưu ma trận tương quan vào một tệp trong Python

Có thể có những lúc bạn muốn thực sự lưu ma trận tương quan theo chương trình. Cho đến nay, chúng tôi đã sử dụng chức năng

matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)
8 để hiển thị biểu đồ của chúng tôi. Tất nhiên, bạn có thể lưu kết quả thủ công vào máy tính của bạn. Nhưng Matplotlib giúp dễ dàng lưu biểu đồ sử dụng chức năng
matrix = df.corr(
    method = 'pearson',  # The method of correlation
    min_periods = 1      # Min number of observations required
)
9 để lưu tệp của chúng tôi.

Tệp cho phép chúng tôi truyền trong một đường dẫn tệp để cho biết nơi chúng tôi muốn lưu tệp. Giả sử chúng tôi muốn lưu nó trong thư mục nơi tập lệnh đang chạy, chúng tôi có thể đi qua một đường dẫn tương đối như dưới đây:

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
0

Trong mã được hiển thị ở trên, chúng tôi sẽ lưu tệp dưới dạng tệp PNG với tên HeatMap. Tệp sẽ được lưu trong thư mục nơi tập lệnh đang chạy.

Chỉ chọn mối tương quan mạnh trong ma trận tương quan

Trong một số trường hợp, bạn chỉ có thể muốn chọn mối tương quan mạnh mẽ trong ma trận. Nói chung, một mối tương quan được coi là mạnh khi giá trị tuyệt đối lớn hơn hoặc bằng 0,7. Vì ma trận được trả về là một khung dữ liệu gấu trúc, chúng ta có thể sử dụng các phương thức lọc pandas để lọc dữ liệu của chúng ta.

Vì chúng tôi muốn chọn các mối quan hệ mạnh mẽ, chúng tôi cần có khả năng chọn các giá trị lớn hơn hoặc bằng 0,7 và nhỏ hơn hoặc bằng -0.7 Vì điều này sẽ làm cho câu lệnh Lựa chọn của chúng tôi trở nên phức tạp hơn, chúng tôi chỉ có thể lọc theo giá trị tuyệt đối của Hệ số tương quan.filter on the absolute value of our correlation coefficient.

Hãy cùng xem cách chúng ta có thể làm điều này:

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
1

Ở đây, trước tiên chúng tôi lấy ma trận của chúng tôi và áp dụng phương thức

# Loading a sample Pandas dataframe
import pandas as pd
import seaborn as sns

df = sns.load_dataset('penguins')

# We're renaming columns to make them print nicer
df.columns = ['species', 'island', 'b_len', 'b_dep', 'f_len', 'f_dep', 'sex']
print(df.head())

# Returns:
#   species     island  b_len  b_dep  f_len   f_dep     sex
# 0  Adelie  Torgersen   39.1   18.7  181.0  3750.0    Male
# 1  Adelie  Torgersen   39.5   17.4  186.0  3800.0  Female
# 2  Adelie  Torgersen   40.3   18.0  195.0  3250.0  Female
# 3  Adelie  Torgersen    NaN    NaN    NaN     NaN     NaN
# 4  Adelie  Torgersen   36.7   19.3  193.0  3450.0  Female
0, chuyển đổi ma trận thành một chuỗi các giá trị 1 chiều, với một số chỉ số. Điều này có nghĩa là mỗi chỉ mục biểu thị cả hàng và cột hoặc ma trận trước đó. Sau đó chúng ta có thể lọc chuỗi dựa trên giá trị tuyệt đối.

Chỉ chọn tương quan tích cực / tiêu cực trong ma trận tương quan

Trong một số trường hợp, bạn có thể chỉ muốn chọn các mối tương quan tích cực trong tập dữ liệu hoặc chỉ các mối tương quan tiêu cực. Một lần nữa, chúng ta có thể làm điều này bằng cách trước tiên giải phóng DataFrame và sau đó chỉ chọn các mối quan hệ tích cực hoặc tiêu cực.

Trước tiên, hãy xem cách chúng ta chỉ có thể chọn các mối quan hệ tích cực:

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
2

Chúng ta có thể thấy ở đây rằng quá trình này gần giống như chỉ chọn các mối quan hệ mạnh mẽ. Chúng tôi chỉ cần thay đổi bộ lọc của chúng tôi của chuỗi thành chỉ bao gồm các mối quan hệ trong đó hệ số lớn hơn 0.

Tương tự, nếu chúng ta muốn chọn các mối quan hệ tiêu cực, chúng ta chỉ cần thay đổi một ký tự. Chúng ta có thể thay đổi

# Loading a sample Pandas dataframe
import pandas as pd
import seaborn as sns

df = sns.load_dataset('penguins')

# We're renaming columns to make them print nicer
df.columns = ['species', 'island', 'b_len', 'b_dep', 'f_len', 'f_dep', 'sex']
print(df.head())

# Returns:
#   species     island  b_len  b_dep  f_len   f_dep     sex
# 0  Adelie  Torgersen   39.1   18.7  181.0  3750.0    Male
# 1  Adelie  Torgersen   39.5   17.4  186.0  3800.0  Female
# 2  Adelie  Torgersen   40.3   18.0  195.0  3250.0  Female
# 3  Adelie  Torgersen    NaN    NaN    NaN     NaN     NaN
# 4  Adelie  Torgersen   36.7   19.3  193.0  3450.0  Female
1 thành so sánh
# Loading a sample Pandas dataframe
import pandas as pd
import seaborn as sns

df = sns.load_dataset('penguins')

# We're renaming columns to make them print nicer
df.columns = ['species', 'island', 'b_len', 'b_dep', 'f_len', 'f_dep', 'sex']
print(df.head())

# Returns:
#   species     island  b_len  b_dep  f_len   f_dep     sex
# 0  Adelie  Torgersen   39.1   18.7  181.0  3750.0    Male
# 1  Adelie  Torgersen   39.5   17.4  186.0  3800.0  Female
# 2  Adelie  Torgersen   40.3   18.0  195.0  3250.0  Female
# 3  Adelie  Torgersen    NaN    NaN    NaN     NaN     NaN
# 4  Adelie  Torgersen   36.7   19.3  193.0  3450.0  Female
2:

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
3

Đây là một công cụ hữu ích, cho phép chúng ta xem mối quan hệ nào là một trong hai hướng. Chúng ta thậm chí có thể kết hợp những điều này và chỉ chọn các mối quan hệ tích cực mạnh mẽ hoặc các mối quan hệ tiêu cực mạnh mẽ.

Sự kết luận

Trong hướng dẫn này, bạn đã học cách sử dụng Python và Pandas để tính toán một ma trận tương quan. Bạn đã học, một thời gian ngắn, một ma trận tương quan là gì và làm thế nào để giải thích nó. Sau đó, bạn đã học được cách sử dụng phương pháp Pandas

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
8 để tính toán ma trận tương quan và cách lọc nó dựa trên các tiêu chí khác nhau. Bạn cũng đã học cách sử dụng thư viện Seaborn để trực quan hóa một ma trận bằng cách sử dụng hàm
# Calculating a Correlation Matrix with Pandas
import pandas as pd

matrix = df.corr()
print(matrix)

# Returns:
#           b_len     b_dep     f_len     f_dep
# b_len  1.000000 -0.235053  0.656181  0.595110
# b_dep -0.235053  1.000000 -0.583851 -0.471916
# f_len  0.656181 -0.583851  1.000000  0.871202
# f_dep  0.595110 -0.471916  0.871202  1.000000
8, cho phép bạn trực quan hóa và hiểu rõ hơn về dữ liệu trong nháy mắt.

Để tìm hiểu thêm về phương pháp DataFrame

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.transform import transform
output_notebook()
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
data = df.corr().stack().rename("value").reset_index()
p = figure(x_range=list(df.columns), y_range=list(df.index), tools=TOOLS, toolbar_location='below',
           tooltips=[('Row, Column', '@level_0 x @level_1'), ('value', '@value')], height = 500, width = 500)

p.rect(x="level_1", y="level_0", width=1, height=1,
       source=data,
       fill_color={'field': 'value', 'transform': LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max())},
       line_color=None)
color_bar = ColorBar(color_mapper=LinearColorMapper(palette=colors, low=data.value.min(), high=data.value.max()), major_label_text_font_size="7px",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     formatter=PrintfTickFormatter(format="%f"),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)
7, hãy xem tài liệu chính thức ở đây.

Tài nguyên bổ sung

Để tìm hiểu về các chủ đề liên quan, hãy xem các bài viết được liệt kê dưới đây:

  • Phương sai của gấu trúc: Tính phương sai của cột DataFrame Pandas
  • Pandas Mô tả: Thống kê mô tả về DataFrame của bạn
  • Pandas Quantile: Tính tỷ lệ phần trăm của DataFrame
  • Pandas có nghĩa là: Tính trung bình gấu trúc cho một hoặc nhiều cột

Làm thế nào để bạn hiển thị một ma trận tương quan trong Python?

Phương pháp 1: Tạo ma trận tương quan bằng thư viện Numpy Numpy Library Sử dụng hàm CorrCoef () trả về ma trận 2 × 2. Ma trận bao gồm các tương quan của x với x (0,0), x với y (0,1), y với x (1,0) và y với y (1,1).Numpy library make use of corrcoef() function that returns a matrix of 2×2. The matrix consists of correlations of x with x (0,0), x with y (0,1), y with x (1,0) and y with y (1,1).

Làm thế nào để bạn hiển thị một ma trận tương quan?

Sử dụng hàm Corrplot (): Vẽ một bản tương quan hàm corrplot (), trong gói cùng tên, tạo một màn hình đồ họa của ma trận tương quan, làm nổi bật các biến tương quan nhất trong bảng dữ liệu.: Draw a correlogram The function corrplot(), in the package of the same name, creates a graphical display of a correlation matrix, highlighting the most correlated variables in a data table.

Làm thế nào để bạn vẽ một ma trận tương quan trong DataFrame Python?

Viết trải nghiệm phỏng vấn ..
Âm mưu ma trận tương quan bằng Python ..
Tạo một ma trận tương quan bằng Python ..
Python |Gấu trúc dataframe.cov ().
Python |Gấu trúc dataframe.corr ().
Python |Gấu trúc dataframe.corrwith ().
Các chức năng thập phân trong Python |Đặt 2 (logical_and (), bình thường hóa (), lượng tử hóa (), xoay ().).

Làm thế nào để bạn hiển thị dữ liệu tương quan?

Biểu đồ hữu ích nhất để hiển thị mối quan hệ giữa hai biến định lượng là một biểu đồ phân tán.a scatterplot.