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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Simple script to load the Misery Index Dataset using pandas or datasets library.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from typing import Dict, List, Optional
|
| 7 |
+
|
| 8 |
+
def load_misery_dataset(file_path: str = "Misery_Data.csv") -> pd.DataFrame:
|
| 9 |
+
"""
|
| 10 |
+
Load the Misery Index Dataset from CSV file.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
file_path: Path to the CSV file (default: "Misery_Data.csv")
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
pandas.DataFrame with cleaned column names and proper data types
|
| 17 |
+
"""
|
| 18 |
+
df = pd.read_csv(file_path)
|
| 19 |
+
|
| 20 |
+
# Rename columns to be more user-friendly
|
| 21 |
+
column_mapping = {
|
| 22 |
+
"Ep #": "episode",
|
| 23 |
+
"Misery": "scenario",
|
| 24 |
+
"Score": "misery_score",
|
| 25 |
+
"VNTO": "vnto",
|
| 26 |
+
"Reward": "reward",
|
| 27 |
+
"Win": "win",
|
| 28 |
+
"Comments": "comments",
|
| 29 |
+
"question_tag": "question_tag",
|
| 30 |
+
"level": "level"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
df = df.rename(columns=column_mapping)
|
| 34 |
+
|
| 35 |
+
# Convert misery_score to numeric
|
| 36 |
+
df["misery_score"] = pd.to_numeric(df["misery_score"], errors="coerce")
|
| 37 |
+
|
| 38 |
+
# Convert reward to numeric, handling empty values
|
| 39 |
+
df["reward"] = pd.to_numeric(df["reward"], errors="coerce").fillna(0).astype(int)
|
| 40 |
+
|
| 41 |
+
# Clean up string columns
|
| 42 |
+
string_columns = ["episode", "scenario", "vnto", "win", "comments", "question_tag", "level"]
|
| 43 |
+
for col in string_columns:
|
| 44 |
+
if col in df.columns:
|
| 45 |
+
df[col] = df[col].astype(str).str.strip()
|
| 46 |
+
df[col] = df[col].replace("nan", "")
|
| 47 |
+
|
| 48 |
+
return df
|
| 49 |
+
|
| 50 |
+
def get_dataset_statistics(df: pd.DataFrame) -> Dict:
|
| 51 |
+
"""
|
| 52 |
+
Get basic statistics about the dataset.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
df: DataFrame containing the dataset
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
Dictionary with dataset statistics
|
| 59 |
+
"""
|
| 60 |
+
stats = {
|
| 61 |
+
"total_samples": len(df),
|
| 62 |
+
"mean_misery": df["misery_score"].mean(),
|
| 63 |
+
"std_misery": df["misery_score"].std(),
|
| 64 |
+
"min_misery": df["misery_score"].min(),
|
| 65 |
+
"max_misery": df["misery_score"].max(),
|
| 66 |
+
"percentiles": {
|
| 67 |
+
"25th": df["misery_score"].quantile(0.25),
|
| 68 |
+
"50th": df["misery_score"].quantile(0.50),
|
| 69 |
+
"75th": df["misery_score"].quantile(0.75),
|
| 70 |
+
},
|
| 71 |
+
"vnto_types": df["vnto"].value_counts().to_dict() if "vnto" in df.columns else {},
|
| 72 |
+
"episodes": df["episode"].nunique(),
|
| 73 |
+
"question_tags": df["question_tag"].value_counts().to_dict() if "question_tag" in df.columns else {},
|
| 74 |
+
}
|
| 75 |
+
return stats
|
| 76 |
+
|
| 77 |
+
def filter_by_vnto(df: pd.DataFrame, vnto_type: str) -> pd.DataFrame:
|
| 78 |
+
"""
|
| 79 |
+
Filter dataset by VNTO type.
|
| 80 |
+
|
| 81 |
+
Args:
|
| 82 |
+
df: DataFrame containing the dataset
|
| 83 |
+
vnto_type: VNTO type to filter by (T, V, N, O, P)
|
| 84 |
+
|
| 85 |
+
Returns:
|
| 86 |
+
Filtered DataFrame
|
| 87 |
+
"""
|
| 88 |
+
return df[df["vnto"] == vnto_type].copy()
|
| 89 |
+
|
| 90 |
+
def filter_by_misery_range(df: pd.DataFrame, min_score: float = 0, max_score: float = 100) -> pd.DataFrame:
|
| 91 |
+
"""
|
| 92 |
+
Filter dataset by misery score range.
|
| 93 |
+
|
| 94 |
+
Args:
|
| 95 |
+
df: DataFrame containing the dataset
|
| 96 |
+
min_score: Minimum misery score (inclusive)
|
| 97 |
+
max_score: Maximum misery score (inclusive)
|
| 98 |
+
|
| 99 |
+
Returns:
|
| 100 |
+
Filtered DataFrame
|
| 101 |
+
"""
|
| 102 |
+
return df[(df["misery_score"] >= min_score) & (df["misery_score"] <= max_score)].copy()
|
| 103 |
+
|
| 104 |
+
def get_sample_scenarios(df: pd.DataFrame, vnto_type: Optional[str] = None, n: int = 5) -> List[Dict]:
|
| 105 |
+
"""
|
| 106 |
+
Get sample scenarios from the dataset.
|
| 107 |
+
|
| 108 |
+
Args:
|
| 109 |
+
df: DataFrame containing the dataset
|
| 110 |
+
vnto_type: Optional VNTO type to filter by
|
| 111 |
+
n: Number of samples to return
|
| 112 |
+
|
| 113 |
+
Returns:
|
| 114 |
+
List of dictionaries with scenario information
|
| 115 |
+
"""
|
| 116 |
+
if vnto_type:
|
| 117 |
+
df_filtered = filter_by_vnto(df, vnto_type)
|
| 118 |
+
else:
|
| 119 |
+
df_filtered = df
|
| 120 |
+
|
| 121 |
+
samples = df_filtered.sample(n=min(n, len(df_filtered)))
|
| 122 |
+
|
| 123 |
+
return [
|
| 124 |
+
{
|
| 125 |
+
"scenario": row["scenario"],
|
| 126 |
+
"misery_score": row["misery_score"],
|
| 127 |
+
"vnto": row["vnto"],
|
| 128 |
+
"episode": row["episode"]
|
| 129 |
+
}
|
| 130 |
+
for _, row in samples.iterrows()
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
+
def main():
|
| 134 |
+
"""Example usage of the dataset loading functions."""
|
| 135 |
+
|
| 136 |
+
# Load the dataset
|
| 137 |
+
print("Loading Misery Index Dataset...")
|
| 138 |
+
df = load_misery_dataset()
|
| 139 |
+
|
| 140 |
+
# Get basic statistics
|
| 141 |
+
stats = get_dataset_statistics(df)
|
| 142 |
+
print(f"\nDataset Statistics:")
|
| 143 |
+
print(f"Total samples: {stats['total_samples']}")
|
| 144 |
+
print(f"Mean misery score: {stats['mean_misery']:.2f}")
|
| 145 |
+
print(f"Standard deviation: {stats['std_misery']:.2f}")
|
| 146 |
+
print(f"Score range: {stats['min_misery']}-{stats['max_misery']}")
|
| 147 |
+
print(f"Number of episodes: {stats['episodes']}")
|
| 148 |
+
|
| 149 |
+
print(f"\nPercentiles:")
|
| 150 |
+
for p, value in stats['percentiles'].items():
|
| 151 |
+
print(f" {p}: {value:.2f}")
|
| 152 |
+
|
| 153 |
+
print(f"\nVNTO Types distribution:")
|
| 154 |
+
for vnto_type, count in stats['vnto_types'].items():
|
| 155 |
+
percentage = (count / stats['total_samples']) * 100
|
| 156 |
+
print(f" {vnto_type}: {count} ({percentage:.1f}%)")
|
| 157 |
+
|
| 158 |
+
print(f"\nTop Question Tags:")
|
| 159 |
+
for tag, count in list(stats['question_tags'].items())[:5]:
|
| 160 |
+
percentage = (count / stats['total_samples']) * 100
|
| 161 |
+
print(f" {tag}: {count} ({percentage:.1f}%)")
|
| 162 |
+
|
| 163 |
+
# Show some examples
|
| 164 |
+
print(f"\nSample low misery scenarios (< 30):")
|
| 165 |
+
low_misery = filter_by_misery_range(df, 0, 30)
|
| 166 |
+
samples = get_sample_scenarios(low_misery, n=3)
|
| 167 |
+
for sample in samples:
|
| 168 |
+
print(f" Score {sample['misery_score']}: {sample['scenario']}")
|
| 169 |
+
|
| 170 |
+
print(f"\nSample high misery scenarios (> 80):")
|
| 171 |
+
high_misery = filter_by_misery_range(df, 80, 100)
|
| 172 |
+
samples = get_sample_scenarios(high_misery, n=3)
|
| 173 |
+
for sample in samples:
|
| 174 |
+
print(f" Score {sample['misery_score']}: {sample['scenario']}")
|
| 175 |
+
|
| 176 |
+
print(f"\nSample Video scenarios (VNTO=V):")
|
| 177 |
+
video_scenarios = filter_by_vnto(df, "V")
|
| 178 |
+
samples = get_sample_scenarios(video_scenarios, n=3)
|
| 179 |
+
for sample in samples:
|
| 180 |
+
print(f" Score {sample['misery_score']}: {sample['scenario']}")
|
| 181 |
+
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
main()
|