Change language

# Exploring Correlation in Python

Correlation matrix — it is basically a covariance matrix. Also known as autocovariance matrix, variance matrix, variance matrix, or variance-covariance matrix. This is the matrix in which the ij position defines the correlation between i- м and j- м parameters of the given dataset.

When the data points follow an approximately straight line, the variables are said to have an approximately linear relationship. In some cases, the data points are close to a straight line, but more often there is little variation in the points around the straight line. A composite measure called correlation describes the strength of a linear association. Correlation summarizes the strength and direction of a linear (direct) relationship between two quantitative variables. It is denoted by r , takes values ​​from -1 to +1. A positive value for r indicates a positive relationship, and a negative value for r indicates a negative relationship.
The closer r is to 1 , the closer the data points fall on a straight line, thus the linear association is stronger. The closer r is to 0, the weaker the linear association.

 ` import ` ` numpy as np ` ` import ` ` pandas as pd ` ` import ` ` seaborn as sns ` ` import ` ` matplotlib.pyplot as plt ` ` from ` ` scipy. stats ` ` import ` ` norm `

 ` data ` ` = ` ` pd.read_csv (` ` "House Price.csv" ` `) ` ` data.shape `

Output:

` (1460, 81) `

Sell Price Description

` `

` data [ ’ SalePrice’ ]. describe () `

` `

Exit :

` count 1460.000000  mean  180921.195890 std 79442.502883 min 34900.000000 25 % 129975.000000 50% 163000.000000 75% 214000.000000 max 755000.000000 Name: SalePrice, dtype: float64 `

Histogram

 ` plt.figure (figsize ` ` = ` ` (` ` 9 ` `, ` ` 5 ` `) ) ` ` data [` ` ’SalePrice’ ` `] .plot (kind ` ` = ` ` "hist" ` `) `

Exit:

Code # 1: Correlation Matrix

 ` corrmat ` ` = ` ` data.corr () `    ` f, ax ` ` = ` ` plt.subplots (figsize ` ` = ` ` (` ` 9 ` `, ` ` 8 ` `)) ` ` sns.heatmap (corrmat, ax ` ` = ` ` ax, cmap ` ` = ` ` " YlGnBu "` `, linewidths ` ` = ` ` 0.1 ` `) `

Output:

Code # 2: grid correlation matrix

 ` corrmat ` ` = ` ` data.corr () `   ` cg ` ` = ` ` sns.clustermap (corrmat, cmap ` ` = ` ` "YlGnBu" ` `, linewidths ` ` = ` ` 0.1 ` `); ` ` plt.setp (cg.ax_heatmap.yaxis.get_majorticklabels (), rotation ` ` = ` ` 0 ` `) `   ` cg `

Output:

Code # 3: ratio for Saleprice

 ` # Saleprice correlation matrix ` ` # k: number of variables for the heatmap ` ` k ` ` = ` ` 15 `    ` cols ` = ` corrmat.nlargest (k, ` ` ’SalePrice’ ` `) [` `’ SalePrice’ ` `]. index `   ` cm ` ` = ` ` np.corrcoef (data [cols] .values.T) ` ` f, ax ` ` = ` ` plt.subplots (figsize ` ` = ` ` (` ` 12 ` `, ` ` 10 ` `)) `   ` sns.heatmap (cm, ax ` ` = ` ` ax, cmap ` ` = ` `" YlGnBu "` `, ` ` linewidths ` ` = ` ` 0.1 ` `, yticklabels ` ` = ` ` cols.values, ` ` xticklabels ` ` = ` ` cols.values) `

Exit:

## Shop

Learn programming in R: courses

\$FREE

Best Python online courses for 2022

\$FREE

Best laptop for Fortnite

\$399+

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best computer for crypto mining

\$499+

Best laptop for Sims 4

\$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries