Chi square test


import numpy as np
from scipy.stats import chi2_contingency

r=int(input("Enter number of rows: "))
c=int(input("Enter number of columns: "))
observed=[]
print("Enter observed values:")
for i in range(r):
    row=list(map(float,input().split()))
    observed.append(row)
observed=np.array(observed)
chi2,p,dof,expected=chi2_contingency(observed)
print("\nChi-Square Value:",chi2)
print("Degrees of Freedom:",dof)
print("Expected Frequencies:\n",expected)
print("P-Value:",p)
alpha=0.05
if p<alpha:
    print("\nConclusion: Reject the Null Hypothesis (Variables are dependent)")
else:
    print("\nConclusion: Fail to Reject the Null Hypothesis (Variables are independent)")

Output:


Enter number of rows: 2
Enter number of columns: 2
Enter observed values:
50 30
20 40
Chi-Square Value: 10.529166666666667
Degrees of Freedom: 1
Expected Frequencies:
 [[40. 40.]
 [30. 30.]]
P-Value: 0.0011750518530845063

Conclusion: Reject the Null Hypothesis (Variables are dependent)

Comments

Popular posts from this blog

Dijkstra's Algorithm Matrix

k means JDK java