Marscrater Analysis (Module 2)
Mars crater sizes vary across hemispheres, and a chi-square test with post hoc comparisons helps reveal where those differences actually show up between size groups.
Code:
In[1]:
import os print(os.listdir())
import pandas as pd
df = pd.read_csv("marscrater_pds.csv") df.head()
Output:
['_5e80885b18b2ac5410ea4eb493b68fb4_gapminder.csv', 'marscrater_pds.csv', 'Untitled3.ipynb', 'Untitled1.ipynb', '.ipynb_checkpoints', 'README.ipynb', 'Test01.ipynb', 'Untitled.ipynb', 'Getting Started with Your Lab.ipynb', 'Untitled2.ipynb', 'gapminder.csv.csv'] CRATER_IDCRATER_NAMELATITUDE_CIRCLE_IMAGELONGITUDE_CIRCLE_IMAGEDIAM_CIRCLE_IMAGEDEPTH_RIMFLOOR_TOPOGMORPHOLOGY_EJECTA_1MORPHOLOGY_EJECTA_2MORPHOLOGY_EJECTA_3NUMBER_LAYERS001-00000084.367108.74682.100.220101-000001Korolev72.760164.46482.021.97Rd/MLERSHuBL3201-00000269.244-27.24079.630.090301-00000370.107160.57574.810.130401-00000477.99695.61773.530.110
In[2]:
df['LATITUDE_CIRCLE_IMAGE'] = pd.to_numeric(df['LATITUDE_CIRCLE_IMAGE'], errors='coerce')
df['HEMISPHERE'] = df['LATITUDE_CIRCLE_IMAGE'].apply( lambda x: 'North' if x >= 0 else 'South' )
In[3]:
df['DIAM_CIRCLE_IMAGE'] = pd.to_numeric(df['DIAM_CIRCLE_IMAGE'], errors='coerce')
df['diam_group'] = pd.qcut(df['DIAM_CIRCLE_IMAGE'], 3, labels=['Small','Medium','Large'])
In[4]: df = df.dropna(subset=['HEMISPHERE', 'diam_group'])
In[5]:
import pandas as pd from scipy.stats import chi2_contingency
table = pd.crosstab(df['HEMISPHERE'], df['diam_group'])
print(table)
chi2, p, dof, expected = chi2_contingency(table)
print("\nChi-square:", chi2) print("p-value:", p)
Output:
diam_group Small Medium Large HEMISPHERE North 50305 52309 48280 South 79418 74922 79109 Chi-square: 294.7298418602536 p-value: 1.0005251593400214e-64
In[6]:
if p < 0.05: print("Significant relationship between hemisphere and crater size.") else: print("No significant relationship.")
Output: Significant relationship between hemisphere and crater size.
In[7]:
import itertools
categories = df['diam_group'].unique()
for pair in itertools.combinations(categories, 2): subset = df[df['diam_group'].isin(pair)] table = pd.crosstab(subset['HEMISPHERE'], subset['diam_group'])chi2, p, dof, exp = chi2_contingency(table) print(f"\nComparison: {pair}") print("p-value:", p)
Output:
Comparison: ('Large', 'Medium') p-value: 9.417245941573836e-62 Comparison: ('Large', 'Small') p-value: 4.655108487337578e-06 Comparison: ('Medium', 'Small') p-value: 1.3956409899865248e-33
For Histogram Analysis:
In[8]:
import seaborn as sns import matplotlib.pyplot as plt
size_col = [col for col in df.columns if 'diam' in col.lower()][0]
plt.figure(figsize=(10,6))
sns.distplot(df[size_col], bins=60, kde=True)
plt.title("Distribution of Mars Crater Sizes") plt.xlabel("Diameter") plt.ylabel("Frequency")
plt.show()
Output:
In[9]:
import matplotlib.pyplot as plt
table.plot(kind='bar', stacked=True, figsize=(10,6))
plt.title("Relationship between Crater Size Group and Density Group") plt.xlabel("Crater Size Group") plt.ylabel("Count")
plt.legend(title="Density Group") plt.tight_layout()
plt.show()
Output:
In[10]:
import seaborn as sns import matplotlib.pyplot as plt
plt.figure(figsize=(8,6))
sns.heatmap(table, annot=True, fmt="d", cmap="Blues")
plt.title("Chi-Square Contingency Table Heatmap") plt.xlabel("Density Group") plt.ylabel("Crater Size Group")
plt.show()
Output:
Screenshot of the Code:
Interpretation:
A Chi-square test of independence was conducted to examine the relationship between hemisphere (north vs south) and crater size categories (small, medium, large). The test showed a statistically significant association (p < 0.05), meaning crater size distribution is not independent of hemisphere. Since the variable had more than two categories, a post hoc analysis was performed using pairwise comparisons to pinpoint where the differences lay between specific crater size groups across hemispheres. These post hoc results indicate that certain crater size categories are disproportionately represented in one hemisphere compared to the other, helping clarify which specific group-level differences are driving the overall Chi-square significance.








