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.

To get a link to House_price Data, click here .

Loading libraries

import numpy as np

import pandas as pd

import seaborn as sns

import matplotlib.pyplot as plt

from scipy. stats import norm

Loading data

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

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

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

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


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

Python add suffix / add prefix to strings in a list

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

Python - Move item to the end of the list

Python - Print list vertically