Skip to content

Instantly share code, notes, and snippets.

@bmander
Created October 8, 2018 05:58
Show Gist options
  • Save bmander/6ad08efeab509a9e51be956b2e213547 to your computer and use it in GitHub Desktop.
Save bmander/6ad08efeab509a9e51be956b2e213547 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# look ma, no sklearn\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2785: DtypeWarning: Columns (13,39,40,41) have mixed types. Specify dtype option on import or set low_memory=False.\n",
" interactivity=interactivity, compiler=compiler, result=result)\n"
]
}
],
"source": [
"df = pd.read_csv(\"data/Train.csv\")\n",
"dftest = pd.read_csv(\"data/test.csv\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the average price of each model."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ModelID\n",
"28 15570.312500\n",
"29 16944.444444\n",
"31 17187.500000\n",
"34 17250.000000\n",
"43 19842.754663\n",
"Name: SalePrice, dtype: float64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model_price = df.groupby(\"ModelID\").SalePrice.mean()\n",
"model_price.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Map the test set's model to the model price."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"yhat = dftest.ModelID.map( model_price )\n",
"yhat = yhat.fillna( yhat.mean() )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That's it."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>SalesID</th>\n",
" <th>SalePrice</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1222837</td>\n",
" <td>73613.924051</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1222839</td>\n",
" <td>72500.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1222841</td>\n",
" <td>40421.487603</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1222843</td>\n",
" <td>15922.619048</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1222845</td>\n",
" <td>44767.533003</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" SalesID SalePrice\n",
"0 1222837 73613.924051\n",
"1 1222839 72500.000000\n",
"2 1222841 40421.487603\n",
"3 1222843 15922.619048\n",
"4 1222845 44767.533003"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"preds = pd.DataFrame( {\"SalesID\": dftest.SalesID, \"SalePrice\": yhat} )\n",
"preds.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"preds.to_csv(\"data/predictions.csv\", index=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Returns 0.321 on the leaderboard."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment