|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 32, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import pandas as pd\n", |
| 10 | + "import matplotlib.pyplot as plt\n", |
| 11 | + "import seaborn as sns\n", |
| 12 | + "import numpy as np\n", |
| 13 | + "from sklearn.model_selection import cross_val_score, train_test_split\n", |
| 14 | + "from sklearn.preprocessing import StandardScaler\n", |
| 15 | + "from sklearn.linear_model import RidgeCV, Ridge, LassoCV\n", |
| 16 | + "from scipy.stats import skew" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 42, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "train = pd.read_csv('train.csv')\n", |
| 26 | + "# log transform target\n", |
| 27 | + "train['SalePrice'] = np.log1p(train['SalePrice'])\n", |
| 28 | + "test = pd.read_csv('test.csv')\n", |
| 29 | + "data = pd.concat((train.loc[:,'MSSubClass':'SaleCondition'],test.loc[:,'MSSubClass':'SaleCondition']))\n", |
| 30 | + "# log transform skewed numeric features\n", |
| 31 | + "num_features = data.dtypes[data.dtypes != 'object'].index\n", |
| 32 | + "skewed_feats = train[num_features].apply(lambda x: skew(x.dropna()))\n", |
| 33 | + "skewed_feats = skewed_feats[skewed_feats > 0.75].index\n", |
| 34 | + "data = pd.get_dummies(data)\n", |
| 35 | + "data = data.fillna(data.mean())\n", |
| 36 | + "data[skewed_feats] = np.log1p(data[skewed_feats])\n", |
| 37 | + "Xtrain = data[:train.shape[0]].values\n", |
| 38 | + "Xtest = data[train.shape[0]:].values\n", |
| 39 | + "y = train.SalePrice.values" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 47, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "def rmse_cv(model):\n", |
| 49 | + " rmse = np.sqrt(-cross_val_score(model, Xtrain, y, scoring='neg_mean_squared_error', cv = 5))\n", |
| 50 | + " return(rmse)" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 53, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005], cv = 5).fit(Xtrain, y)\n", |
| 60 | + "preds = np.expm1(model_lasso.predict(Xtest))\n", |
| 61 | + "solution = pd.DataFrame({\"id\":test.Id, \"SalePrice\":preds})\n", |
| 62 | + "solution.to_cs" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": null, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [] |
| 71 | + } |
| 72 | + ], |
| 73 | + "metadata": { |
| 74 | + "kernelspec": { |
| 75 | + "display_name": "Python 3", |
| 76 | + "language": "python", |
| 77 | + "name": "python3" |
| 78 | + }, |
| 79 | + "language_info": { |
| 80 | + "codemirror_mode": { |
| 81 | + "name": "ipython", |
| 82 | + "version": 3 |
| 83 | + }, |
| 84 | + "file_extension": ".py", |
| 85 | + "mimetype": "text/x-python", |
| 86 | + "name": "python", |
| 87 | + "nbconvert_exporter": "python", |
| 88 | + "pygments_lexer": "ipython3", |
| 89 | + "version": "3.7.1" |
| 90 | + } |
| 91 | + }, |
| 92 | + "nbformat": 4, |
| 93 | + "nbformat_minor": 2 |
| 94 | +} |
0 commit comments