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: