PRACTICAL 4: DATA PREPROCESSING - CORRELATION AND CONTINGENCY TABLE

STEPS:
1. Install the libraries:  pip install pandas seaborn matplotlib scipy
2. Load Match.csv and select only the numeric columns.
3. Calculate the correlation matrix and draw it as a heatmap.
4. Create a contingency table of Toss_Decision against Match_Winner_Id.
5. Apply the Chi-square test on the table (statistic, p-value, degrees of freedom, expected frequencies).
6. If p-value < 0.05 the variables are dependent, otherwise they are independent.

CODE:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import chi2_contingency
df=pd.read_csv("Match.csv")
num_df=df.select_dtypes(include=["int64","float64"])
correlation_matrix=num_df.corr()
print("correlation matrix:\n",correlation_matrix)
plt.figure(figsize=(10,8))
sns.heatmap(correlation_matrix,annot=True,cmap="coolwarm",fmt=".2f")
plt.title("correlation matrix")
plt.show()
if 'Toss_Decision' in df.columns:
  contingency_table=pd.crosstab(df['Toss_Decision'],df['Match_Winner_Id'])
  print("Contigency Table:\n",contingency_table)
  chi2,p,dof,expected=chi2_contingency(contingency_table)
  print(f"\nChi-square Statistic: {chi2}")
  print(f"P-value: {p}")
  print(f"Degrees of Freedom: {dof}")
  print("Expected Frequencies:\n",expected)
  if p< 0.05:
    print("\n Variables are dependent (reject H0)")
  else:
    print("\nVariables are independent (fail to reject H0) ")

EXPLANATION:
- corr() finds how strongly every pair of numeric columns is related (+1 strong positive, -1 strong negative, 0 none).
- The heatmap shows the correlation matrix with colours, so the related columns are easy to spot.
- pd.crosstab() builds a contingency table that counts matches for each toss decision and match winner.
- chi2_contingency() tests whether the two categorical variables are related.
- If the p-value is below 0.05 the variables are dependent (H0 rejected), otherwise they are independent.
