Hướng dẫn how do you visualize a correlation matrix in python? - làm thế nào để bạn hình dung 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 visualize a correlation matrix in python? - làm thế nào để bạn hình dung 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 visualize a correlation matrix in python? - làm thế nào để bạn hình dung một ma trận tương quan trong python?

Tương quan có nghĩa là một liên kết, nó là một thước đo mức độ mà hai biến có liên quan. & NBSP;

1. Tương quan dương: Khi hai biến tăng lên và giảm cùng nhau. Chúng có mối tương quan tích cực. 1 1 là một mối tương quan tích cực hoàn hảo. Ví dụ - nhu cầu và lợi nhuận được tương quan tích cực càng nhiều nhu cầu về sản phẩm, lợi nhuận càng nhiều do đó tương quan tích cực. When two variables increase together and decrease together. They are positively correlated. ‘1’ is a perfect positive correlation. For example – demand and profit are positively correlated the more the demand for the product, the more profit hence positive correlation.

2. Tương quan tiêu cực: Khi một biến tăng và biến khác sẽ giảm cùng nhau và ngược lại. Chúng có mối tương quan tiêu cực. Ví dụ, nếu khoảng cách giữa nam châm tăng sức hút của chúng giảm và ngược lại. Do đó, một mối tương quan tiêu cực. ‘-1, & nbsp; không có mối tương quan When one variable increases and the other variable decreases together and vice-versa. They are negatively correlated. For example, If the distance between magnet increases their attraction decreases, and vice-versa. Hence, a negative correlation. ‘-1’  is no correlation

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

3. Không tương quan (không có mối tương quan): Khi hai biến don don dường như được liên kết. ‘0, là một mối tương quan tiêu cực hoàn hảo. Ví dụ, lượng trà bạn lấy và mức độ thông minh. When two variables don’t seem to be linked at all. ‘0’ is a perfect negative correlation. For Example, the amount of tea you take and level of intelligence.

Âm mưu ma trận tương quan bằng cách sử dụng python

Bước 1: Nhập thư viện.Importing the libraries.

Python3

import sklearn

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

Bước 2: Tìm mối tương quan giữa hai biến.Finding the Correlation between two variables.

Python3

Các

Các

import4

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 import6

import7

Output:

Bước 3: Vẽ đồ thị. Ở đây chúng tôi đang sử dụng các lô phân tán. Một biểu đồ phân tán là một sơ đồ trong đó mỗi giá trị trong tập dữ liệu được biểu thị bằng một dấu chấm. Ngoài ra, nó cho thấy một mối quan hệ giữa hai biến.Plotting the graph. Here we are using scatter plots. A scatter plot is a diagram where each value in the data set is represented by a dot. Also, it shows a relationship between two variables.

Python3

import8

import9

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)
3numpy as np1

numpy as np2numpy as np3

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)
1numpy as np5numpy as np6

Output:

Hãy nhớ những điểm đã được giải thích ở trên. Quan sát cả hai hình ảnh bạn cũng sẽ tìm thấy sự tương đồng, quan sát giá trị của mối tương quan gần với 1, do đó mối tương quan tích cực được phản ánh.

Thêm tiêu đề và nhãn trong biểu đồ

Python3

numpy as np7numpy as np8numpy as np6

import8

import1

numpy as np2import3

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)
3numpy as np1

numpy as np2numpy as np3

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)
1numpy as np5numpy as np6

matplotlib.pyplot as plt1matplotlib.pyplot as plt2numpy as np6

matplotlib.pyplot as plt4matplotlib.pyplot as plt5numpy as np6

Output:

Vẽ bằng cách sử dụng các bản đồ nhiệt

Có nhiều cách bạn có thể vẽ các ma trận tương quan một cách hiệu quả là sử dụng bản đồ nhiệt. Rất dễ hiểu về mối tương quan bằng cách sử dụng HeheatMaps, nó cho biết mối tương quan của một tính năng (biến) với mọi tính năng khác (biến). Nói cách khác, một ma trận tương quan là một dữ liệu bảng đại diện cho các mối tương quan giữa các cặp biến trong một dữ liệu nhất định. heatmaps it tells the correlation of one feature(variable) to every other feature(variable). In other words, A correlation matrix is a tabular data representing the ‘correlations’ between pairs of variables in a given data.

Python3

import matplotlib.pyplot as plt8

matplotlib.pyplot as plt9

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 import1import2numpy as np6

import4

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 import6
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)
1import8numpy as np6

Output:


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 hình dung một ma trận tương quan?

Hàm Corrplot được sử dụng để vẽ đồ thị của ma trận tương quan.... Tương quan: Hình dung ma trận tương quan ..

Làm thế nào để bạn hình dung mối tương quan giữa hai cột trong gấu trúc?

Khởi tạo hai biến, COL1 và COL2 và gán cho chúng các cột mà bạn muốn tìm mối tương quan.Tìm mối tương quan giữa COL1 và COL2 bằng cách sử dụng DF [col1] .corr (df [col2]) và lưu giá trị tương quan trong một biến, corr.print Giá trị tương quan, Corr.df[col1]. corr(df[col2]) and save the correlation value in a variable, corr. Print the correlation value, corr.

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.