File size: 5,721 Bytes
495e3b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from holisticai.datasets import load_dataset\n",
"import pandas as pd\n",
"\n",
"from folktables import ACSDataSource, ACSIncome, ACSEmployment, ACSPublicCoverage, ACSMobility, ACSTravelTime\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1458542, 17)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.read_parquet('data/acstraveltime/acstraveltime_dataset.parquet')\n",
"data.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"datasets = [\"acsincome\", \"acspublic\", \"adult\", \"clinical_records\", \"law_school\", \"student\", \"us_crime\", \"german_credit\",\n",
" \"census_kdd\", \"bank_marketing\", \"compas_two_year_recid\", \"compas_is_recid\", \"diabetes\", \"mw_small\", \"mw_medium\"]\n",
"\n",
"tab = pd.DataFrame([], columns=['dataset', 'samples', 'features'])\n",
"for data_name in datasets:\n",
" dataset = load_dataset(data_name)\n",
" samples = dataset['X'].shape[0]\n",
" features = dataset['X'].shape[1]\n",
" new = {\n",
" 'dataset': data_name,\n",
" 'samples': samples,\n",
" 'features': features\n",
" }\n",
" tab = pd.concat([tab, pd.DataFrame([new])])\n",
"\n",
"print(tab.to_latex())"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
"\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
"\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"source": [
"def load_acs_data(path = 'datasets/', target_attr=\"income\", sensitive_attribute=\"sex\", survey_year=\"2018\", states=[\"CA\"], horizon=\"1-Year\",survey='person'):\n",
" data_source = ACSDataSource(survey_year=survey_year, horizon=horizon, survey=survey, root_dir=path)\n",
" data = data_source.get_data(states=states, download=False)\n",
"\n",
" if target_attr == \"acsincome\":\n",
" features, labels, _ = ACSIncome.df_to_pandas(data)\n",
" categorical_features = [\"COW\", \"SCHL\", \"MAR\", \"OCCP\", \"POBP\", \"RELP\", \"WKHP\"]\n",
" elif target_attr == \"acsemployment\":\n",
" features, labels, _ = ACSEmployment.df_to_pandas(data)\n",
" categorical_features = [\"AGEP\", \"SCHL\", \"MAR\", \"RELP\", \"DIS\", \"ESP\", \"CIT\", \"MIG\", \"MIL\", \"ANC\", \"NATIVITY\", \"DEAR\", \"DEYE\", \"DREM\"]\n",
" elif target_attr == \"acspubliccoverage\":\n",
" features, labels, _ = ACSPublicCoverage.df_to_pandas(data)\n",
" categorical_features = ['AGEP','SCHL','MAR','DIS','ESP','CIT','MIG','MIL','ANC','NATIVITY','DEAR','DEYE','DREM','PINCP','ESR','ST','FER']\n",
" elif target_attr == \"acsmobility\":\n",
" features, labels, _ = ACSMobility.df_to_pandas(data)\n",
" categorical_features = ['AGEP','SCHL','MAR','DIS','ESP','CIT','MIL','ANC','NATIVITY','RELP','DEAR','DEYE','DREM','GCL','COW','ESR','WKHP','JWMNP','PINCP']\n",
" elif target_attr == \"acstraveltime\":\n",
" features, labels, _ = ACSTravelTime.df_to_pandas(data)\n",
" categorical_features = ['AGEP','SCHL','MAR','DIS','ESP','MIG','RELP','PUMA','ST','CIT','OCCP','JWTR','POWPUMA','POVPIP']\n",
"\n",
" else:\n",
" print( \"error\" )\n",
" \n",
"\n",
" df = features\n",
" y = labels.astype(np.int32)\n",
" categorical_features.append(\"RAC1P\")\n",
" categorical_features.append(\"SEX\")\n",
"\n",
" X = df\n",
" X[categorical_features] = X[categorical_features].astype(\"string\")\n",
"\n",
"\n",
" # Convert all non-uint8 columns to float32\n",
" string_cols = X.select_dtypes(exclude=\"string\").columns\n",
" X[string_cols] = X[string_cols].astype(\"float32\")\n",
"\n",
" data = pd.concat([X, y], axis=1)\n",
" data.to_parquet(f'datasets/{target_attr}.parquet')\n",
"\n",
"\n",
"states = [\"CA\", \"TX\", \"NY\", \"FL\", \"IL\", \"PA\", \"OH\", \"GA\", \"NC\", \"MI\", \"NJ\", \"VA\", \"WA\", \"AZ\", \"MA\", \"TN\", \"IN\", \"MO\", \"MD\", \"WI\", \"CO\", \"MN\", \"SC\", \"AL\", \"LA\", \"KY\", \"OR\", \"OK\", \"CT\", \"IA\", \"MS\", \"AR\", \"UT\", \"NV\", \"KS\", \"NM\", \"NE\", \"WV\", \"ID\", \"HI\", \"ME\", \"NH\", \"RI\", \"MT\", \"DE\", \"SD\", \"ND\", \"AK\", \"VT\", \"WY\"]\n",
"for target in [\"acstraveltime\"]:\n",
" load_acs_data(target_attr=target, states=states)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|