Use pd.cut() for income_cat

main
Aurélien Geron 2019-03-15 23:49:03 +08:00
parent e8ed65eac6
commit 5f6be6fa6f
1 changed files with 3 additions and 4 deletions

View File

@ -338,10 +338,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Divide by 1.5 to limit the number of income categories\n",
"housing[\"income_cat\"] = np.ceil(housing[\"median_income\"] / 1.5)\n",
"# Label those above 5 as 5\n",
"housing[\"income_cat\"].where(housing[\"income_cat\"] < 5, 5.0, inplace=True)"
"housing[\"income_cat\"] = pd.cut(housing[\"median_income\"],\n",
" bins=[0., 1.5, 3.0, 4.5, 6., np.inf],\n",
" labels=[1, 2, 3, 4, 5])"
]
},
{